【hdu1698-Just aHook】(线段树成段更新)

来源:互联网 发布:java 泛型 类型转换 编辑:程序博客网 时间:2024/06/06 08:58

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.
解析:这个主要是一个区间一个区间的去更新值,我们一直学的是把一个点更新,没有学区间更新,我就一直在想怎么更新,总是找不到一个条件。就是在更新的时候,怎么再找到这个点的区间包含的其他点,把其他点的值也更新,直到看到了大神的代码,彻底跪了QAQ……
AC代码:

#include<cstdio>#include<algorithm>using namespace std;int N;struct Tree{    int l,r,sum,tag,condition;//tag用来记录输入的Z,condition用来当做条件 }tree[100000<<2];void PushUp(int o){    tree[o].sum = tree[o*2].sum + tree[o*2+1].sum ;}void Bulid(int o,int l,int r){    tree[o].l = l;    tree[o].r = r;    tree[o].condition = 0;    tree[o].tag = 0;    if(l == r)    {        tree[o].sum = 1;        return ;    }    int mid=(l+r)>>1;    Bulid(o*2,l,mid);    Bulid(o*2+1,mid+1,r);    PushUp(o);}void Change(int o,int l,int r,int z)//成段更新 {    if(tree[o].l ==l&&tree[o].r ==r)    {        tree[o].condition =1;//说明O点包括了从L到R的所有数的和,用来找到o所包含的点并更新         tree[o].tag =z;        tree[o].sum = z*(r-l+1);        return ;    }    int mid = (tree[o].l + tree[o].r )>>1;    if(tree[o].condition == 1)    {        Change(o*2,tree[o].l, mid, tree[o].tag );        Change(o*2+1,mid+1, tree[o].r ,tree[o].tag );        tree[o].condition =0;        tree[o].tag =0;    }    if(l>mid) Change(o<<1|1,l,r,z);       else if(r<=mid) Change(o<<1,l,r,z);           else             {                  Change(o<<1,l,mid,z);                  Change(o<<1|1,mid+1,r,z);             }        PushUp(o);}int main(){    int T,Q,x,y,z,Case=1;    scanf("%d",&T);    while(T--)    {        scanf("%d",&N);        Bulid(1,1,N);        scanf("%d",&Q);        while(Q--)        {            scanf("%d%d%d",&x,&y,&z);            Change(1,x,y,z);        }        printf("Case %d: The total value of the hook is %d.\n",Case++,tree[1].sum );        }    return 0;}
原创粉丝点击