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.
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.
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.
这题是线段树成段更新,看了网上的博客才做了出来。
#include<stdio.h> #include<string.h> #define maxn 100005 struct node { int sum,l,r; }b[4*maxn]; void build(int l,int r,int i) { int mid; b[i].l=l; b[i].r=r; b[i].sum=1; //这里每条线段都要更新为1 if(b[i].l==b[i].r) return; mid=(b[i].l+b[i].r)/2; build(l,mid,i*2); build(mid+1,r,i*2+1); } void update(int l,int r,int value,int i) { int mid; if(b[i].sum==value) //如果价值和原来的相等,就直接返回 return; if(b[i].l==l && b[i].r==r) //如果找到对应的区间,那么直接更新,这里一开始想不明白如果b[i].sum==-1为什么需要更新,后来想想发现这是 { //最新一次的更新,如果之前是-1,那么区间内所有的线段都从杂色变为同一种颜色 b[i].sum=value; return; } if(b[i].sum!=-1) //如果是同一种颜色,因为前面已经判断不是恰好对应整段区间更新,所以这里先把子节点变为父节点的值 { //因为这段变为杂色,所以要对其子节点进行处理 b[i*2].sum=b[i*2+1].sum=b[i].sum; b[i].sum=-1; } mid=(b[i].l+b[i].r)/2; if(l>mid) update(l,r,value,i*2+1); else if(r<=mid) update(l,r,value,i*2); else if(r>mid && l<=mid) { update(l,mid,value,i*2); update(mid+1,r,value,i*2+1); } } int find(int i) { if(b[i].sum!=-1) //如果是纯色,就直接返回,否则继续搜 return (b[i].r-b[i].l+1)*b[i].sum; else return ( find(i*2)+find(i*2+1) ); } int main() { int T,n,m,i,j,c,d,e,h; scanf("%d",&T); for(h=1;h<=T;h++) { scanf("%d",&n); build(1,n,1); scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d%d%d",&c,&d,&e); update(c,d,e,1); } printf("Case %d: The total value of the hook is %d. ",h,find(1)); } return 0; }