Problem H
JuQueen
JuQueen is the super computer with the best performance allover Germany. It is on rank 8 in the famous top500 list with its 458752 cores. It draws a lot of energy (up to 2301 kW), so we want to reduce that by underclocking the unused cores.
The cluster scheduling algorithm which is in charge of distributing jobs over the nodes and cores of a cluster will issue the following speedstepping commands:
To be safe for the future, your program should be able to handle 4587520 cores. The initial frequency for each core is 0.
Input
The input contains a single test case. It starts with a line containing three integers C, N, and O, where C is the number of cores (1 ≤ C ≤ 4587520) to manage, N is the number of frequency steps for each core (1 ≤ N ≤ 10000) and O is the number of operations in the test program (1 ≤ O ≤ 50000). Then follow O lines, each containing one command as described above. X, A and B are 0-based IDs of the cores (0 ≤ A,B,X < C; A ≤ B). S is an integer number of steps, possibly negative (−N ≤ S ≤ +N).
Both, the change and the groupchange command will increase (or decrease) in single steps and stop as soon as one core in the group reaches the minimal (0) or maximal frequency (N).
Output
Output one line for every operation in the input. For change and groupchange print the changed number of steps, for state print the current state.
Sample Input I |
Sample Output I |
10 10 5 state 0 groupchange 2 9 7 state 9 groupchange 0 2 10 change 0 -5 |
0 7 7 3 -3 |
Sample Input II |
Sample Output II |
4587520 10000 5 groupchange 0 4587010 9950 groupchange 23 4587000 42 groupchange 4710 4587001 -1000 state 1234560 groupchange 6666 3060660 10000 |
9950 42 -1000 8992 1008 |
解题:线段树,超大。。。注意内存控制
1 #include <bits/stdc++.h> 2 #define INF 0x3f3f3f3f 3 using namespace std; 4 const int maxn = 10000000; 5 struct node { 6 int minv,maxv,lazy; 7 } tree[maxn<<2]; 8 void build(int lt,int rt,int v) { 9 tree[v].minv = tree[v].maxv = tree[v].lazy = 0; 10 if(lt == rt) return; 11 int mid = (lt + rt)>>1; 12 build(lt,mid,v<<1); 13 build(mid+1,rt,v<<1|1); 14 } 15 void pushup(int v) { 16 tree[v].minv = min(tree[v<<1].minv+tree[v<<1].lazy,tree[v<<1|1].minv+tree[v<<1|1].lazy); 17 tree[v].maxv = max(tree[v<<1].maxv+tree[v<<1].lazy,tree[v<<1|1].maxv+tree[v<<1|1].lazy); 18 } 19 void pushdown(int v) { 20 if(tree[v].lazy) { 21 tree[v<<1].lazy += tree[v].lazy; 22 tree[v<<1|1].lazy += tree[v].lazy; 23 tree[v].lazy = 0; 24 } 25 } 26 void update(int L,int R,int lt,int rt,int v,int value) { 27 if(L >= lt && R <= rt) { 28 tree[v].lazy += value; 29 tree[v].minv += tree[v].lazy; 30 tree[v].maxv += tree[v].lazy; 31 pushdown(v); 32 return; 33 } 34 pushdown(v); 35 int mid = (L + R)>>1; 36 if(lt <= mid) update(L,mid,lt,rt,v<<1,value); 37 if(rt > mid) update(mid+1,R,lt,rt,v<<1|1,value); 38 pushup(v); 39 } 40 int getMin(int L,int R,int lt,int rt,int v) { 41 if(L >= lt && R <= rt) 42 return tree[v].minv + tree[v].lazy; 43 pushdown(v); 44 int ans = INF,mid = (L + R)>>1; 45 if(lt <= mid) ans = min(ans,getMin(L,mid,lt,rt,v<<1)); 46 if(rt > mid) ans = min(ans,getMin(mid+1,R,lt,rt,v<<1|1)); 47 pushup(v); 48 return ans; 49 } 50 int getMax(int L,int R,int lt,int rt,int v) { 51 if(L >= lt && R <= rt) 52 return tree[v].maxv + tree[v].lazy; 53 pushdown(v); 54 int ans = -INF,mid = (L + R)>>1; 55 if(lt <= mid) ans = max(ans,getMax(L,mid,lt,rt,v<<1)); 56 if(rt > mid) ans = max(ans,getMax(mid+1,R,lt,rt,v<<1|1)); 57 pushup(v); 58 return ans; 59 } 60 int query(int L,int R,int p,int v) { 61 if(L == R) 62 return tree[v].minv + tree[v].lazy; 63 pushdown(v); 64 int mid = (L + R)>>1; 65 int ans = 0; 66 if(p <= mid) ans = query(L,mid,p,v<<1); 67 if(p > mid) ans = query(mid+1,R,p,v<<1|1); 68 pushup(v); 69 return ans; 70 } 71 int main() { 72 int N,M,Q,x,y,v; 73 char s[30]; 74 while(~scanf("%d %d %d",&N,&M,&Q)) { 75 memset(tree,0,sizeof(tree)); 76 while(Q--) { 77 scanf("%s",s); 78 if(s[0] == 's') { 79 scanf("%d",&x); 80 printf("%d\n",query(0,N,x,1)); 81 } else { 82 if(s[0] == 'c') { 83 scanf("%d %d",&x,&v); 84 y = x; 85 } else if(s[0] == 'g') scanf("%d %d %d",&x,&y,&v); 86 int nv; 87 if(v < 0) { 88 int minv = getMin(0,N,x,y,1); 89 nv = minv + v < 0 ? -minv:v; 90 } else { 91 int maxv = getMax(0,N,x,y,1); 92 nv = maxv + v > M ? M-maxv:v; 93 } 94 update(0,N,x,y,1,nv); 95 printf("%d\n",nv); 96 } 97 } 98 } 99 return 0; 100 }