Just a Hook(线段树区间更新)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
OutputFor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.

题目大意:输入t,有t组样例,输入n,m,代表n个钩子,m次操作,刚开始钩子的价值为1,通过m来改变区间里每个钩子的价值
个人思路:线段树区间更新的裸题,用到延迟标记,还要用scanf printf 不然会超时,还有注意的是数组要开大4倍,证明的话自己百度
具体思路看代码
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=100+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int sum[maxn<<2];
int lazy[maxn<<2];
void Pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void Pushdown(int rt,int c,int m)
{
    lazy[rt<<1]=c;//向下传递
    lazy[rt<<1|1]=c;
    lazy[rt]=0;//取消延迟标记,因为已经向下传递了
    sum[rt<<1]=(m-(m>>1))*c;
    sum[rt<<1|1]=(m>>1)*c;
}
void Build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=1;
        return ;
    }
    int mid=(l+r)>>1;
    if(l<=mid) Build(Lson);
    if(r>mid) Build(Rson);
    Pushup(rt);
}
void Updata(int l,int r,int rt,int L,int R,int c)
{
    if(L<=l&&r<=R)
    {
        lazy[rt]=c;// 延迟标记,不必现在就把下面的值赋值上去,不然难免浪费时间
        sum[rt]=(r-l+1)*c;
        return ;
    }
    if(lazy[rt])
        Pushdown(rt,lazy[rt],r-l+1);//需要用到的话就判断是否有延迟标记
    int mid=(l+r)>>1;
    if(L<=mid) Updata(Lson,L,R,c);
    if(R>mid) Updata(Rson,L,R,c);
    Pushup(rt);
}
int main()
{
    int t,ca=1;
   // cin>>t;
   scanf("%d",&t);
    while(t--)
    {
        memset(lazy,0,sizeof(lazy));
        int n,m;
       // cin>>n>>m;
       scanf("%d%d",&n,&m);
        Build(1,n,1);
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            //cin>>a>>b>>c;
            scanf("%d%d%d",&a,&b,&c);
            Updata(1,n,1,a,b,c);
        }
        printf("Case %d: The total value of the hook is %d.
",ca++,sum[1]);
    }
    return 0;
}
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/9402593.html