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

hdu 1698 Just a Hook(线段树区间修改)

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

标签:

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.

Dota里面的屠夫的钩子由n个小木棍组成,这些小木棍一开始都是铜的,现在对指定区间的木棍进行修改。铜--1,银--2,金--3(材质以及对应相应的价值)
让你计算整个钩子的价值。
代码如下:
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 #define maxn 100100
 5 int n,t,q,cas=0;
 6 struct segTree
 7 {
 8     int sum[maxn<<2],setv[maxn<<2];
 9     void buildTree (int now ,int l,int r)
10     {
11         setv[now]=0;
12         if (l==r)
13         {
14             sum[now]=1;
15             return ;
16         }
17         int mid=(l+r)>>1;
18         buildTree(now<<1,l,mid);
19         buildTree(now<<1|1,mid+1,r);
20         maintain(now,l,r);
21     }
22     void maintain (int now,int l,int r)
23     {
24         if (r>l)
25         sum[now]=sum[now<<1]+sum[now<<1|1];
26         if (setv[now])
27         sum[now]=(r-l+1)*setv[now];
28     }
29     void pushDown (int now,int l,int r)
30     {
31         if (setv[now])
32         {
33             setv[now<<1]=setv[now];
34             setv[now<<1|1]=setv[now];
35             setv[now]=0;
36         }
37     }
38     void upDate (int now,int l,int r,int q1,int q2,int v)
39     {
40         if (q1<=l&&r<=q2)
41         setv[now]=v;
42         else
43         {
44             pushDown(now,l,r);
45             int mid=(l+r)>>1;
46             if (q1<=mid)
47             upDate(now<<1,l,mid,q1,q2,v);
48             else
49             maintain(now<<1,l,mid);
50             if (q2>mid)
51             upDate(now<<1|1,mid+1,r,q1,q2,v);
52             else
53             maintain(now<<1|1,mid+1,r);
54         }
55         maintain(now,l,r);
56     }
57 }tree;
58 int main()
59 {
60     //freopen("de.txt","r",stdin);
61     scanf("%d",&t);
62     while (t--)
63     {
64         scanf("%d",&n);
65         tree.buildTree(1,1,n);
66         scanf("%d",&q);
67         while (q--)
68         {
69             int x,y,z;
70             scanf("%d%d%d",&x,&y,&z);
71             tree.upDate(1,1,n,x,y,z);
72         }
73         printf("Case %d: The total value of the hook is %d.\n",++cas,tree.sum[1]);
74     }
75     return 0;
76 }

 

 

hdu 1698 Just a Hook(线段树区间修改)

标签:

原文地址:http://www.cnblogs.com/agenthtb/p/5883469.html

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