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.hdu1
题意:就是区间又该为一样东西,不是加加减减之类的东西,而是,直接将一段区间改为一个数。
题解:就是可以将整一段标记为一个值,或者-1,-1表示该区间内有多个不同的值。
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cmath> 5 #include<cstring> 6 const int MAXN=100007; 7 char s[20],c; 8 long long tree[MAXN*4]={0},add[MAXN*4]={0}; 9 int t,time,num,x,n,y,m,z,q,a[MAXN]={0}; 10 using namespace std; 11 long long query(int l,int r,int p,int x,int y) 12 { 13 int res=0; 14 if (l>r) exit; 15 if (l==x&&r==y&&tree[p]!=-1) res+=tree[p]*(r-l+1); 16 else 17 { 18 int mid=(l+r)>>1; 19 if (y<=mid) res+=query(l,mid,p*2,x,y); 20 else if(x>=mid+1) res+=query(mid+1,r,p*2+1,x,y); 21 else res+=query(l,mid,p*2,x,mid)+query(mid+1,r,p*2+1,mid+1,y); 22 } 23 return res; 24 } 25 void change(int l,int r,int p,int x,int y,int zhi) 26 { 27 if (l>r) exit; 28 if (l==x&&r==y) tree[p]=zhi; 29 else 30 { 31 if (tree[p]!=-1) 32 { 33 tree[p*2]=tree[p*2+1]=tree[p]; 34 tree[p]=-1; 35 } 36 int mid=(l+r)>>1; 37 if (y<=mid) change(l,mid,p*2,x,y,zhi); 38 else if (x>=mid+1) change(mid+1,r,p*2+1,x,y,zhi); 39 else 40 { 41 change(l,mid,p*2,x,mid,zhi); 42 change(mid+1,r,p*2+1,mid+1,y,zhi); 43 } 44 } 45 } 46 int main() 47 { 48 scanf("%d",&time); 49 t=time; 50 while (time--) 51 { 52 cout<<"Case "<<t-time<<": The total value of the hook is "; 53 scanf("%d%d",&n,&q); 54 memset(tree,-1,sizeof(tree)); 55 tree[1]=1; 56 for (int i=1;i<=q;i++) 57 { 58 scanf("%d%d%d",&x,&y,&z); 59 change(1,n,1,x,y,z); 60 } 61 printf("%d. ",query(1,n,1,1,n)); 62 } 63 }