hdu_1698Just a Hook线段树_区间修改

来源:互联网 发布:java内部类 编辑:程序博客网 时间:2024/06/06 23:18

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18044    Accepted Submission(s): 9033


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
11021 5 25 9 3
 

Sample Output
Case 1: The total value of the hook is 24.
 
方法一:建议两个线段树,一个保存子叶结点值,另一个保存结点的和,因此求区间和时只需输出根结点即可。

#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define mem0(a) memset(a,0,sizeof(a))#define maxn  100000+10int a[maxn<<2],sum[maxn<<2];//a数组用于线段树标记,sum用于求和void Getsum(int cur){    sum[cur] = sum[cur<<1]+sum[cur<<1|1];}void build(int l,int r ,int cur){    a[cur] = 0;//建树时初始结点都未标记    //标记的意思是是否需要修改该区间    if(l == r){        sum[cur]=1;//        printf("%d %d %d\n",l,cur,sum[cur]);        return ;    }    int mid =  ( l + r ) >> 1;    build(l,mid,cur<<1); //建立左子树    build(mid+1,r,cur<<1|1);//建立右子树    Getsum(cur);}void pushDown(int cur,int len){//维护结点cur,对应区间是cur<<1,cur<<1|1    if(a[cur]){//本结点有标记才传递。        a[cur<<1]=a[cur<<1|1]=a[cur];  //标记结点向左右子树传递        sum[cur<<1]=a[cur]*(len-(len>>1));//左子树区间增加        sum[cur<<1|1]=a[cur]*(len>>1);//右子树区间段增加        a[cur]= 0 ;//清除标记    }}void update(int x,int y,int z,int l,int r,int cur){    if( x <= l && y >= r){        a[cur]=z;  //改变边界的值        sum[cur] = z*(r - l + 1);//计算区间和        return ;    }    pushDown(cur,r-l+1);    int mid = ( l + r )>> 1;    if(x <= mid ) update(x,y,z,l,mid,cur<<1);    if(y > mid )  update(x,y,z,mid+1,r,cur<<1|1);    Getsum(cur);   //递归结束前重新计算本结点的附加信息}int main(){    int T,n,m,t=1,x,y,z;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        build(1,n,1);        scanf("%d",&m);        while(m--){            scanf("%d %d %d",&x,&y,&z);            update(x,y,z,1,n,1);        }//    printf("%d %d %d %d %d %d %d %d %d %d\n",sum[16],sum[17],sum[9],sum[10],sum[11],sum[24],sum[25],sum[14],sum[15]);    printf("Case %d: The total value of the hook is %d.\n",t++,sum[1]);    }    return 0;}

方法二:只建立一个线段树,在求区间和时需要调用函数分别累加区间值
#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define mem0(a) memset(a,0,sizeof(a))#define maxn  100000+10int a[maxn<<2];void build(int l,int r ,int cur){    a[cur] = 0;//建树时初始结点都未标记    if(l == r){        a[cur]=1;        return ;    }    int mid =  ( l + r ) >> 1;    build(l,mid,cur<<1); //建立左子树    build(mid+1,r,cur<<1|1);//建立右子树}void pushDown(int cur){//维护结点cur,对应区间是cur<<1,cur<<1|1    if(a[cur]){//本结点有标记才传递。        a[cur<<1]=a[cur<<1|1]=a[cur];        a[cur]= 0 ;//清除标记    }}void update(int x,int y,int z,int l,int r,int cur){    if( x <= l && y >= r){        a[cur] = z;        return ;    }    pushDown(cur);    int mid = ( l + r )>> 1;    if(x <= mid ) update(x,y,z,l,mid,cur<<1);    if(y > mid )  update(x,y,z,mid+1,r,cur<<1|1);}int query(int l,int r,int cur){    if(l == r )        return a[cur];    pushDown(cur);    int mid = (l + r )>>1 ;    return query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1);}int main(){    int T,n,m,t=1,x,y,z;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        build(1,n,1);        scanf("%d",&m);        while(m--){            scanf("%d %d %d",&x,&y,&z);            update(x,y,z,1,n,1);        }    printf("Case %d: The total value of the hook is %d.\n",t++,query(1,n,1));    }    return 0;}



0 0