原题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1698
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17134 Accepted Submission(s): 8555
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
分析:用线段树,区间更新。
#include<cstdio> #include<cstring> #include<iostream> using namespace std; #define MAXN 100010 int sum[4*MAXN],lazy[4*MAXN]; int t,n,op,x,y,z,total; void build(int r,int L,int R){ if(L==R) sum[r] = 1; else{ int M = (L+R)/2; build(r*2,L,M); build(r*2+1,M+1,R); sum[r] = sum[r*2] + sum[r*2+1]; } } void push_up(int r,int L,int R) { if(lazy[r]>0) sum[r] = lazy[r]*(R-L+1); else sum[r] = sum[r*2] + sum[r*2+1]; } void push_down(int r) { if(lazy[r]>0) { lazy[r*2] = lazy[r*2+1] = lazy[r]; lazy[r] = 0; } } void update(int r,int L,int R,int x,int y,int z) { if(x<=L&&R<=y) { lazy[r] = z; sum[r] = z*(R-L+1); } else { push_down(r); int M = (L+R)/2; if(x<=M) update(r*2,L,M,x,y,z); else push_up(r*2,L,M); if(y>M) update(r*2+1,M+1,R,x,y,z); else push_up(r*2+1,M+1,R); push_up(r,L,R); } } void query(int r, int L,int R,int ql,int qr) { if(lazy[r]>0) total+=lazy[r]*(min(qr,R)-max(ql,L)+1); else if(ql<=L&&R<=qr) total += sum[r]; else { int M = (L+R)/2; if(ql<=M) query(r*2,L,M,ql,qr); if(qr>M) query(r*2+1,M+1,R,ql,qr); } } int main() { scanf("%d",&t); int k = 1; while(t--) { memset(lazy,0,sizeof(lazy)); lazy[1] = 1; total = 0; scanf("%d%d",&n,&op); build(1,1,n); while(op--) { scanf("%d%d%d",&x,&y,&z); update(1,1,n,x,y,z); } query(1,1,n,1,n); printf("Case %d: The total value of the hook is %d.\n",k++,total); } return 0; }
相关推荐
现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。 Input 本题目包含多组测试数据,请处理到文件结束。 每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。 Output 请在一行里面...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu_2102_passed_sorce
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
acm hdu as easy as a+b
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
自己做的HDU ACM已经AC的题目
hdu动态规划算法集锦