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

魔兽世界---屠夫(Just a Hook)

时间:2015-09-16 23:17:27      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

        Just a Hook

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


技术分享

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

 

Sample Output
Case 1: The total value of the hook is 24.
题解:本来屠夫的钩子是铜的价值是1,然后每次使x到y间的钩子变为银2,或金3;线段树;
错了很多次,原来想着把z设为全局变量,最后答案一直不对,经历了各种曲折。。。
代码:
 1 #include<stdio.h>
 2 const int MAXN=100010;
 3 struct Node{
 4     int l,r;
 5     int sum,lazy,val;//val不能全局变量。。。
 6 };
 7 Node tree[MAXN<<2];
 8 #define NOW tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum
 9 #define lson root<<1,tree[root].l,mid
10 #define rson root<<1|1,mid+1,tree[root].r
11 void build(int root,int l,int r){
12     tree[root].l=l;
13     tree[root].r=r;
14     tree[root].lazy=0;
15     tree[root].val=0;
16     if(l==r)tree[root].sum=1;
17     else{
18             int mid=(l+r)>>1;
19         build(lson);
20         build(rson);
21         NOW;
22     }
23 }
24 void update(int root,int l,int r,int v){
25     if(l==tree[root].l&&r==tree[root].r){
26             tree[root].lazy=1;
27             tree[root].val=v;
28             tree[root].sum=(r-l+1)*v;
29     }
30     else{
31         int mid=(tree[root].l+tree[root].r)>>1;
32         if(tree[root].lazy==1){
33             tree[root].lazy=0;
34             update(lson,tree[root].val);
35             update(rson,tree[root].val);
36             tree[root].val=0;
37         }
38         if(r<=mid)update(root<<1,l,r,v);
39         else if(l>mid)update(root<<1|1,l,r,v);
40         else{//这个不能少了,代表l,r在tree的两个孩子节点内,也就是找到了l,r的区间;
41             update(root<<1,l,mid,v);
42             update(root<<1|1,mid+1,r,v);
43         }
44         NOW;
45     }
46 }
47 int main(){
48     int T,N,Q,flot=0;
49     scanf("%d",&T);
50     while(T--){
51             scanf("%d",&N);
52         scanf("%d",&Q);
53         build(1,1,N);
54         while(Q--){
55             int x,y,z;
56             scanf("%d%d%d",&x,&y,&z);
57             update(1,x,y,z);
58         }
59         printf("Case %d: The total value of the hook is %d.\n",++flot,tree[1].sum);
60     }
61     return 0;
62 }

 

魔兽世界---屠夫(Just a Hook)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4814792.html

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