Just a HookCrawling in process...Crawling failedTime Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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>
int n;
struct tree{
int l,r,m,v;
}t[400010];
void pushdown(int k){//向下传递,但是并不是加上而是直接改变当前的值
if(t[k].m){
t[k*2].m=t[k].m;
t[k*2].v=(t[k*2].r-t[k*2].l+1)*t[k].m;
t[k*2+1].m=t[k].m;
t[k*2+1].v=(t[k*2+1].r-t[k*2+1].l+1)*t[k].m;
t[k].m=0;
}
}
void pushup(int k){//向上传递
t[k].v=t[k*2].v+t[k*2+1].v;
}
void build(int k,int l,int r){//构建线段树
t[k].l=l,t[k].r=r,t[k].m=0;
if(t[k].r==t[k].l){
t[k].v=1;
return ;
}
int mid=(l+r)/2;
if(mid<l) build(k*2+1,l,r);
else if(mid>=r) build(k*2,l,r);
else{
build(k*2,l,mid);
build(k*2+1,mid+1,r);
}
}
int query(int k,int l,int r){//查询线段树区间
if(t[k].l>=l&&t[k].r<=r) return t[k].v;
int mid=(t[k].r+t[k].l)/2;
if(mid<l) return query(k*2+1,l,r);
else if(mid>=r) return query(k*2,l,r);
else return query(k*2,l,mid)+query(k*2+1,mid+1,r);
}
void updata(int k,int l,int r,int add){//更新线段树区间
if(t[k].l>=l&&t[k].r<=r){
t[k].m=add;
t[k].v=add*(t[k].r-t[k].l+1);
return ;
}
pushdown(k);
int mid=(t[k].r+t[k].l)/2;
if(mid<l) updata(k*2+1,l,r,add);
else if(mid>=r) updata(k*2,l,r,add);
else{
updata(k*2,l,mid,add);
updata(k*2+1,mid+1,r,add);
}
pushup(k);
}
int main(){
int m,ca,num=1;
scanf("%d",&m);
while(m--){
scanf("%d%d",&n,&ca);
build(1,1,n);
while(ca--){
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
updata(1,l,r,x);
}
printf("Case %d: The total value of the hook is %d.\n",num++,query(1,1,n));
}
return 0;
}