码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1398:Just a Hook(线段树区间更新)

时间:2016-07-09 23:45:18      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.hdu.edu.cn/showproblem.php?pid=1698

 

Just a Hook



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.
 
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.
 
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
5 2
5 9 3
 
Sample Output
 
Case 1: The total value of the hook is 24.
 
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 #define N 1000010
 7 #define lson L,m,rt<<1
 8 #define rson m+1,R,rt<<1|1
 9 int sum[N<<2],add[N<<2];
10 struct node
11 {
12     int l,r;
13     int value;
14 }tree[N<<2];
15 /*
16 Update 区间更新
17 Query 直接求根节点的sum
18 使用Lazy标记
19 Lazy标记的学习:http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html
20 */
21 void PushUp(int rt)
22 {
23 //自底向上更新
24     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
25 }
26 
27 void BuildTree(int L,int R,int rt)
28 {
29     add[rt]=0;
30     if(L==R){
31         sum[rt]=1;
32         return ;
33     }
34     int m=(L+R)>>1;
35     BuildTree(lson);
36     BuildTree(rson);
37     PushUp(rt);
38 }
39 
40 void PushDown(int rt,int m)
41 {
42 //处理lazy标记,自顶向下更新
43     if(add[rt]){
44         add[rt<<1]=add[rt];
45         add[rt<<1|1]=add[rt];
46         sum[rt<<1]=add[rt]*(m-(m>>1));
47         sum[rt<<1|1]=add[rt]*(m>>1);
48         add[rt]=0;
49     }
50 }
51 
52 void Update(int l,int r,int c,int L,int R,int rt)
53 {
54     if(add[rt]==c) return ;
55     if(L>=l&&R<=r){
56         add[rt]=c;
57         sum[rt]=c*(R-L+1);
58         return ;
59     }
60     PushDown(rt,R-L+1);
61     int m=(R+L)>>1;
62     if(l<=m) Update(l,r,c,lson);
63     if(r>m)  Update(l,r,c,rson);
64     PushUp(rt);
65 }
66 
67 int main()
68 {
69     int cas=0;
70     int t;
71     scanf("%d",&t);
72     while(t--){
73         int n,q;
74         scanf("%d%d",&n,&q);
75         BuildTree(1,n,1);
76         while(q--){
77             int a,b,c;
78             scanf("%d%d%d",&a,&b,&c);
79             Update(a,b,c,1,n,1);
80         }
81         printf("Case %d: The total value of the hook is %d.\n",++cas,sum[1]);
82     }
83     return 0;
84 }

 

HDU 1398:Just a Hook(线段树区间更新)

标签:

原文地址:http://www.cnblogs.com/fightfordream/p/5656739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!