线段树HUD1698:Just a Hook

Posted 听风不成泣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线段树HUD1698:Just a Hook相关的知识,希望对你有一定的参考价值。

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 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <string.h>
 6 using namespace std;
 7 typedef long long ll;
 8 typedef unsigned long long ull;
 9 const int N = 1e5+50;;
10 typedef pair<int,int>pii;
11 typedef pair<ll,int>P;
12 int n,m;
13 int sum[N*4],lazy[N*4],a[N];
14 void pushDown(int rt,int len_l,int len_r){
15     if(lazy[rt]){
16         sum[rt*2]=len_l*lazy[rt];
17         sum[rt*2+1]=len_r*lazy[rt];
18         lazy[rt*2]=lazy[rt];
19         lazy[rt*2+1]=lazy[rt];
20         lazy[rt]=0;
21     }
22 }
23 void pushUp(int rt){
24     sum[rt]=sum[rt*2] + sum[rt*2+1];
25 }
26 void build(int l,int r,int rt){
27     lazy[rt]=0;
28     if(l==r){
29         sum[rt]=a[l];
30         return;
31     }
32     int mid=(l+r)/2;
33     build(l,mid,rt*2);
34     build(mid+1,r,rt*2+1);
35     pushUp(rt);
36 }
37 void update(int L,int R,int val,int l,int r,int rt){
38     if(L<=l && r<=R){
39         sum[rt]=val*(r-l+1);
40         lazy[rt]=val;
41         return;
42     }
43     int mid=(l+r)/2;
44     pushDown(rt,mid-l+1,r-mid);
45     if(L<=mid)update(L,R,val,l,mid,rt*2);
46     if(R>mid) update(L,R,val,mid+1,r,rt*2+1);
47     pushUp(rt);
48 }
49 int query(int L,int R,int l,int r,int rt){
50     if(L<=l && r<=R){
51         return sum[rt];
52     }
53     int mid=(l+r)/2;
54     pushDown(rt,mid-l+1,r-mid);
55     int ret=0;
56     if (L <= mid) ret += query(L,R ,l,mid,rt*2);
57     if (R > mid) ret += query(L,R,mid+1,r,rt*2+1);
58     return ret;
59 }
60 int main() {
61     int T;
62     int l,r,val;
63     cin>> T;
64     for(int j=1;j<=T;j++)
65     {
66         scanf("%d",&n);
67         for(int i=1;i<=n;i++)
68         {
69             a[i]=1;
70         }
71         build(1,n,1);
72         scanf("%d",&m);
73         while(m--)
74         {
75             scanf("%d%d%d",&l,&r,&val);
76             update(l,r,val,1,n,1);
77         }
78         printf("Case %d: The total value of the hook is %d.\n",j,query(1,n,1,n,1));
79     }
80     return 0;
81 }
View Code

 

以上是关于线段树HUD1698:Just a Hook的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1698 Just a Hook (线段树)

hdu 1698 Just a Hook

HDU 1698 Just a Hook(线段树)

hdoj 1698 Just a Hook 线段树 区间更新

hdu 1698 Just a Hook 线段树成段更新

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