hdu1698 Just a Hook

来源:互联网 发布:幻萌网络知乎 编辑:程序博客网 时间:2024/05/16 08:55

Problem Description
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.**

大致题意是有n条边,一开始颜色全部为一。让你染色,染完后计算颜色编号的总和。

线段树

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;struct node{    int l,r,lc,rc,c;}tr[210000];int len,ans;void bt(int l,int r){    int now=++len;    tr[now].l=l;tr[now].r=r;tr[now].c=1;    tr[now].lc=tr[now].rc=-1;    if(l<r)    {        int mid=(l+r)/2;        tr[now].lc=len+1;bt(l,mid);        tr[now].rc=len+1;bt(mid+1,r);    }}void change(int now,int l,int r,int k)//change(改)函数的功能 :在now的管理范围内,把[l,r]改为第k种颜色{    if( tr[now].c==k) return ;//如果now管理的范围颜色统一,并且本来就是k,那么什么都不要做    if( tr[now].l==l&& r==tr[now].r) { tr[now].c=k;return ;} //如果刚好now的管理范围就是[l,r],那么不管now管理范围原来是什么颜色统一改就行    int lc= tr[now].lc, rc=tr[now].rc;    int mid=( tr[now].l+ tr[now].r)/2;    if(tr[now].c>0)//如果原来now的管理范围颜色统一,那么现在要改now的管理范围中的部分范围的颜色了                   //此时now就想:我的把我原来的颜色先传给我的左右副总,因为他们还不知道他们现在的颜色(在这次改之前的颜色)    {              //这个步骤我们称为:继承        tr[lc].c= tr[now].c;        tr[rc].c= tr[now].c;    }    if( r<=mid)         change(lc,l,r,k); //如果[l,r]在左副总的管理范围中,那么这件事情就交给左副总    else if( mid+1<=l)   change(rc,l,r,k); //如果[l,r]在右副总的管理范围中,那么这件事情就交给右副总    else    {       change(lc,  l    , mid , k );       change(rc, mid+1 ,  r  , k );    }    //注意:有 修改 就配带有 维护    if( tr[lc].c==tr[rc].c&& tr[lc].c>0  )tr[now].c= tr[ lc ] .c ;    else tr[now].c=-1;}void findsum(int now,int l,int r){    if(tr[now].c>0){ans+=(tr[now].r-tr[now].l+1)*tr[now].c;return ;}    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;    if(r<=mid)  findsum(lc,l,r);     else if(mid+1<=l)  findsum(rc,l,r);    else       {        findsum(lc,l,mid);        findsum(rc,mid+1,r);    }}int main(){    int t;    scanf("%d",&t);    for(int ti=1;ti<=t;ti++)    {        int n,m;        scanf("%d",&n);        len=0;bt(1,n);        scanf("%d",&m);        for(int i=1;i<=m;i++)        {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            change(1,x,y,z);            }           ans=0;findsum(1,1,n);        printf("Case %d: The total value of the hook is %d.\n",ti,ans);    }    return 0;}

by_lmy

1 0