首页 > 代码库 > 又长又臭又慢的线段树 模板题hdu1698 Just a Hook
又长又臭又慢的线段树 模板题hdu1698 Just a Hook
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30901 Accepted Submission(s):
15221
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.
练练手熟悉一下写法
不过写完才发现这个直接输出val[1]就好了
贴代码
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int t=0,n=0,val[400010],mark[400010],m=0,x=0,y=0,z=0; 7 8 void build(int pos,int ll,int rr){ 9 if(ll==rr){ 10 val[pos]=1; 11 mark[pos]=0; 12 return; 13 } 14 else{ 15 int mid=(ll+rr)/2; 16 build(2*pos,ll,mid); 17 build(2*pos+1,mid+1,rr); 18 val[pos]=val[2*pos]+val[2*pos+1]; 19 } 20 } 21 22 void down(int pos,int num){ 23 if(mark[pos]){ 24 val[2*pos]=mark[pos]*(num-num/2); 25 val[2*pos+1]=mark[pos]*(num/2);//attention! 26 mark[2*pos]=mark[2*pos+1]=mark[pos]; 27 mark[pos]=0; 28 } 29 } 30 31 void change(int pos,int ll,int rr){ 32 if(x>rr||y<ll) return; 33 if(x<=ll&&y>=rr){ 34 val[pos]=z*(rr-ll+1); 35 mark[pos]=z; 36 return; 37 } 38 down(pos,rr-ll+1); 39 int mid=(ll+rr)/2; 40 change(2*pos,ll,mid); 41 change(2*pos+1,mid+1,rr); 42 val[pos]=val[2*pos]+val[2*pos+1]; 43 } 44 45 int main(){ 46 scanf("%d",&t); 47 for(int i=1;i<=t;i++){ 48 memset(val,0,sizeof(val)); 49 memset(mark,0,sizeof(mark)); 50 scanf("%d",&n); 51 build(1,1,n); 52 scanf("%d",&m); 53 for(int j=1;j<=m;j++){ 54 scanf("%d%d%d",&x,&y,&z); 55 change(1,1,n); 56 } 57 printf("Case %d: The total value of the hook is %d.\n",i,val[1]); 58 } 59 return 0; 60 }
又长又臭又慢的线段树 模板题hdu1698 Just a Hook
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。