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

HDU 1698 Just a Hook (线段树,区间更新)

时间:2014-08-09 21:25:09      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17214    Accepted Submission(s): 8600


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.

bubuko.com,布布扣


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 1 5 2 5 9 3
 

 

Sample Output
Case 1: The total value of the hook is 24.
 

 

Source
 

 

Recommend
wangye
 
一道线段树区间更新的题目
这种题目,刚开始做的时候想单点修改,结果单点修改会超时。所以采用了区间修改。
这里用了延时标记,也就是说,当我查询的区间满足当前区间的条件的我就只更新我当前的区间,给子区间打标记,当下次询问时再更新,否则每次更新区间遍历整棵树会超时。
bubuko.com,布布扣
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<stdlib.h>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN=100000+10;
 7 struct node
 8 {
 9     int l,r;
10     int num;
11     int col;
12     int mid()
13     {
14         return (l+r)/2;
15     }
16 }a[MAXN*4];
17 
18 void pushup(int step)
19 {
20     a[step].num=a[step*2].num+a[step*2+1].num;
21 }
22 
23 int pushdown(int x,int step)
24 {
25     if(a[step].col)
26     {
27         a[step*2].col=a[step*2+1].col=a[step].col;
28         a[step*2].num=(x-x/2)*a[step].col;
29         a[step*2+1].num=(x/2)*a[step].col;
30         a[step].col=0;
31     }
32 }
33 
34 void btree(int l,int r,int step)
35 {
36     a[step].l=l;
37     a[step].r=r;
38     a[step].col=0;
39     if(l==r)
40     {
41         a[step].num=1;
42         return ;
43     }
44     int mid=a[step].mid();
45     btree(l,mid,step*2);
46     btree(mid+1,r,step*2+1);
47     pushup(step);
48 }
49 
50 void ptree(int l,int r,int val,int step)
51 {
52     if(l<=a[step].l&&a[step].r<=r)
53     {
54         a[step].col=val;
55         a[step].num=(a[step].r-a[step].l+1)*val;
56         return ;
57     }
58     pushdown(a[step].r-a[step].l+1,step);
59     int mid=a[step].mid();
60     if(l>mid)
61         ptree(l,r,val,step*2+1);
62     else if(r<=mid)
63         ptree(l,r,val,step*2);
64     else
65     {
66         ptree(l,r,val,step*2);
67         ptree(l,r,val,step*2+1);
68     }
69     pushup(step);
70 }
71 int main()
72 {
73     int kase,cnt=0,ans;
74     scanf("%d",&kase);
75     while(kase--)
76     {
77         int n,Q;
78         scanf("%d %d",&n,&Q);
79         btree(1,n,1);
80         while(Q--)
81         {
82             int x,y,z;
83             scanf("%d %d %d",&x,&y,&z);
84             ptree(x,y,z,1);
85         }
86         printf("Case %d: The total value of the hook is %d.\n",++cnt,a[1].num);
87     }
88     return 0;
89 }
View Code

 

HDU 1698 Just a Hook (线段树,区间更新),布布扣,bubuko.com

HDU 1698 Just a Hook (线段树,区间更新)

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/clliff/p/3901544.html

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