kuangbin专题七 HDU1698 Just a Hook (set线段树)
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. 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 <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <algorithm> 12 #include <sstream> 13 #include <stack> 14 using namespace std; 15 #define FO freopen("in.txt","r",stdin); 16 #define rep(i,a,n) for (int i=a;i<n;i++) 17 #define per(i,n) for (int i=n-1;i>=a;i--) 18 #define pb push_back 19 #define mp make_pair 20 #define all(x) (x).begin(),(x).end() 21 #define fi first 22 #define se second 23 #define SZ(x) ((int)(x).size()) 24 #define debug(x) cout << "&&" << x << "&&" << endl; 25 #define lowbit(x) (x&-x) 26 #define mem(a,b) memset(a,b,sizeof(a)); 27 typedef vector<int> VI; 28 typedef long long ll; 29 typedef pair<int,int> PII; 30 const ll mod=1000000007; 31 const int inf = 0x3f3f3f3f; 32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} 33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} 34 //head 35 36 const int maxn=100010; 37 int sum[maxn<<2],lazy[maxn<<2],_,n,q,l,r,val; 38 39 void Pushup(int rt) { 40 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 41 } 42 43 void Pushdown(int rt,int x) { 44 if(lazy[rt]) {//!!! 45 lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt]; 46 sum[rt<<1]=(x-(x>>1))*lazy[rt];//!!! = 47 sum[rt<<1|1]=(x>>1)*lazy[rt]; 48 lazy[rt]=0; 49 } 50 } 51 52 void build(int rt,int L,int R) { 53 lazy[rt]=0; 54 if(L==R) { 55 sum[rt]=1; 56 return; 57 } 58 int mid=(L+R)>>1; 59 build(rt<<1,L,mid); 60 build(rt<<1|1,mid+1,R); 61 Pushup(rt); 62 } 63 64 void Updata(int rt,int R,int l,int r) { 65 if(L>=l&&R<=r) {//核心 66 lazy[rt]=val; 67 sum[rt]=(R-L+1)*val; 68 return; 69 } 70 Pushdown(rt,R-L+1); 71 int mid=(L+R)>>1; 72 if(l<=mid) Updata(rt<<1,mid,r); 73 if(r>mid) Updata(rt<<1|1,R,r); 74 Pushup(rt); 75 } 76 77 int Query(int rt,int r) { 78 if(L>=l&&R<=r) return sum[rt]; 79 Pushdown(rt,R-L+1); 80 int ans=0; 81 int mid=(L+R)>>1; 82 if(l<=mid) ans+=Query(rt<<1,r); 83 if(r>mid) ans+=Query(rt<<1|1,r); 84 Pushup(rt); 85 return ans; 86 87 } 88 89 int main() { 90 int cur=1; 91 for(scanf("%d",&_);_;_--) { 92 scanf("%d",&n); 93 build(1,1,n); 94 scanf("%d",&q); 95 while(q--) { 96 scanf("%d%d%d",&l,&r,&val); 97 Updata(1,r); 98 } 99 printf("Case %d: The total value of the hook is %d.n",cur++,Query(1,1,n)); 100 } 101 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |