hdu 1698 Just a Hook
Posted lucky_少哖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1698 Just a Hook相关的知识,希望对你有一定的参考价值。
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25414 Accepted Submission(s):
12686
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.
Source
Recommend
还是区域更新的线段树,感觉方法非常巧妙,根节点记录子节点是否为同一种颜色,以此减少计算时间
题意: 有一个棍子很长 可以分为很多节 每节都有不同的材料 价值为1 2 3 组成 初始化状态认为全部节价值为用价值为1的材料组成
现在输入 第一个数表示case 第二个数是节数 第三个是操作数 每个操作有3个数 a b c 即从a到b 的节材料改成价值为c的材料
附上代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #define M 100010 5 using namespace std; 6 struct node 7 { 8 int l,r,s; 9 }ss[M*3]; 10 11 void build(int l,int r,int k) 12 { 13 ss[k].l=l; 14 ss[k].r=r; 15 ss[k].s=1; 16 if(l==r) return; 17 int mid=(l+r)/2; 18 build(l,mid,k*2); 19 build(mid+1,r,k*2+1); 20 } 21 22 void insert(int l,int r,int x,int k) 23 { 24 if(ss[k].s==x) return; //颜色相同,则不用修改 25 if(ss[k].l==l&&ss[k].r==r) 26 { 27 ss[k].s=x; 28 return; 29 } 30 if(ss[k].s!=-1) //如果只有一种颜色,又与添加的颜色不同,则此区间的颜色变为杂色 31 { 32 ss[k*2].s=ss[k*2+1].s=ss[k].s; //所有子节点变为父节点的颜色 33 ss[k].s=-1; 34 } 35 int mid=(ss[k].l+ss[k].r)/2; 36 if(mid>=r) insert(l,r,x,2*k); 37 else if(mid<l) insert(l,r,x,2*k+1); 38 else 39 { 40 insert(l,mid,x,k*2); 41 insert(mid+1,r,x,k*2+1); 42 } 43 } 44 45 int search(int k) 46 { 47 if(ss[k].s!=-1) //如果此区间只有一种颜色,直接计算 48 return (ss[k].r-ss[k].l+1)*ss[k].s; 49 else 50 return search(k*2)+search(k*2+1); 51 } 52 int main() 53 { 54 int T,i,j,n,m,Case; 55 scanf("%d",&T); 56 for(Case=1;Case<=T;Case++) 57 { 58 scanf("%d%d",&n,&m); 59 build(1,n,1); 60 int a,b,c; 61 while(m--) 62 { 63 scanf("%d%d%d",&a,&b,&c); 64 insert(a,b,c,1); 65 } 66 printf("Case %d: The total value of the hook is %d.\n",Case,search(1)); 67 } 68 return 0; 69 }
以上是关于hdu 1698 Just a Hook的主要内容,如果未能解决你的问题,请参考以下文章