Just a Hook HDU

来源:互联网 发布:小说改编都市网络剧 编辑:程序博客网 时间:2024/06/06 03:55

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.
Input
The 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.
Output
For 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.

今天要说的线段树的标记问题,这个是为更新区间的而产生的概念,也是线段树的精华。
标记分两种,绝对标记和相对标记,比如在一个区间上在各自基础上都加上某个数,这是相对标记,因为这是相对原来的数值来讲,在一个区间上的所有的数值都变成某个值,这绝对标志,因为这个和原来的值没有关系,它只关心现在的当前情况。
这道题就是绝对标记的情况。(这题真坑,输出有一个”.”,没有注意,一直WA,崩溃了。。。

其实标记的作用就是减少了不必要的冗余更新(懒惰意思由来),在绝对标记中,标记会覆盖,即前一个标记失效,就是说以当前标记为准,而相对标记则会叠加。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define maxn 100000using namespace std;int sum[maxn<<2+5];short mark[maxn<<2+5];//void build(int s,int l,int r){    mark[s]=0;//说明没有需要向下子节点点的更新    if(l==r)    {        sum[s]=1;        return;    }    int m=(l+r)>>1;    build(s<<1,l,m);    build(s<<1|1,m+1,r);    sum[s]=sum[s<<1]+sum[s<<1|1];}void change(int s,int l,int r,int L,int R, int c){    if(L<=l&&r<=R)    {        sum[s]=(r-l+1)*c;//改变了这个点值        mark[s]=(short)c;//说明下面的字节点都行需要更新为c        return;    }    int m=(l+r)>>1;    if(mark[s]!=0)//执行到这里是,说明,我需要去访问子节点了,那么之前更新的值(如果有),就需要更新下去    {        sum[s<<1]=(m-l+1)*mark[s];        sum[s<<1|1]=(r-m)*mark[s];        mark[s<<1]=mark[s];        mark[s<<1|1]=mark[s];        mark[s]=0;    }    if(m>=L)change(s<<1,l,m,L,R,c);    if(m<R)change(s<<1|1,m+1,r,L,R,c);    sum[s]=sum[s<<1]+sum[s<<1|1];//子节点都更新完了,就可以更新当前节点了}//下面的主函数就按题意写就是了int main(){    int t,n;    int l;    int r;    cin>>t;    for(int i=1;i<=t;i++)    {        cin>>n;        build(1,1,n);        //cout<<sum[1];        int m;        int k;        cin>>m;        while(m--)        {            scanf("%d%d%d",&l,&r,&k);            change(1,1,n,l,r,k);            //cout<<sum[1]<<endl;        }        printf("Case %d: The total value of the hook is %d.\n",i,sum[1]);    }    return 0;}