HDU - 1698 Just a Hook
Posted 西北会法语
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 1698 Just a Hook相关的知识,希望对你有一定的参考价值。
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.
InputThe 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.
OutputFor 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<cstdio> 3 #include<cstring> 4 #define ls o<<1 5 #define rs o<<1|1 6 #define lson L,mid,ls 7 #define rson mid+1,R,rs 8 #define middnf int mid=(L+R)>>1; 9 #define ll long long 10 11 using namespace std; 12 13 const int maxn=100005; 14 ll mx[maxn<<2],lazy[maxn<<2]; 15 int n,m,x,y,z; 16 17 void pushup(int o) 18 { 19 mx[o]=mx[ls]+mx[rs]; 20 } 21 22 void pushdown(int o,int m) 23 { 24 if(!lazy[o]) return; 25 lazy[ls] = lazy[o]; 26 lazy[rs] = lazy[o]; 27 mx[ls]=(m-(m>>1))*lazy[o]; 28 mx[rs]=(m>>1)*lazy[o]; 29 lazy[o]= 0; 30 } 31 32 void build(int L,int R,int o) 33 { 34 if(L==R) 35 { 36 mx[o]=1;; 37 return ; 38 } 39 middnf; 40 build(lson); 41 build(rson); 42 pushup(o); 43 //cout<<o<<" "<<mx[o]<<endl; 44 } 45 46 void update(int l,int r,int L,int R,int o,int v){//区间更新 47 if(l<=L&&R<=r) 48 { 49 mx[o] = v*(R-L+1); 50 lazy[o] = v; 51 return; 52 } 53 pushdown(o,R-L+1); 54 middnf; 55 if(l<=mid) update(l,r,lson,v); 56 if(r>mid) update(l,r,rson,v); 57 pushup(o); 58 } 59 60 int main() 61 { 62 char c[5]; 63 int t,s=1; 64 scanf("%d",&t); 65 while(t--) 66 { 67 memset(mx,0,sizeof(mx)); 68 memset(lazy,0,sizeof(lazy)); 69 scanf("%d%d",&n,&m); 70 build(1,n,1); 71 for(int i=0;i<m;i++) 72 { 73 scanf("%d%d%d",&x,&y,&z); 74 update(x,y,1,n,1,z); 75 } 76 printf("Case %d: The total value of the hook is %d.\n",s++,mx[1]); 77 } 78 79 80 return 0; 81 }
以上是关于HDU - 1698 Just a Hook的主要内容,如果未能解决你的问题,请参考以下文章