CSUOJ 1532 JuQueen
阅读原文时间:2023年07月17日阅读:1

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:

  • change X S changes the frequency of core X by S steps
  • groupchange A B S changes the frequency of every core in range [A,B] by S steps
  • state X returns the current state of core X

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; AB). S is an integer number of steps, possibly negative (−NS ≤ +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

解题:线段树,超大。。。注意内存控制

#include
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct node {
int minv,maxv,lazy;
} tree[maxn<<]; void build(int lt,int rt,int v) { tree[v].minv = tree[v].maxv = tree[v].lazy = ; if(lt == rt) return; int mid = (lt + rt)>>;
build(lt,mid,v<<); build(mid+,rt,v<<|); } void pushup(int v) { tree[v].minv = min(tree[v<<].minv+tree[v<<].lazy,tree[v<<|].minv+tree[v<<|].lazy); tree[v].maxv = max(tree[v<<].maxv+tree[v<<].lazy,tree[v<<|].maxv+tree[v<<|].lazy); } void pushdown(int v) { if(tree[v].lazy) { tree[v<<].lazy += tree[v].lazy; tree[v<<|].lazy += tree[v].lazy; tree[v].lazy = ; } } void update(int L,int R,int lt,int rt,int v,int value) { if(L >= lt && R <= rt) { tree[v].lazy += value; tree[v].minv += tree[v].lazy; tree[v].maxv += tree[v].lazy; pushdown(v); return; } pushdown(v); int mid = (L + R)>>;
if(lt <= mid) update(L,mid,lt,rt,v<<,value); if(rt > mid) update(mid+,R,lt,rt,v<<|,value); pushup(v); } int getMin(int L,int R,int lt,int rt,int v) { if(L >= lt && R <= rt) return tree[v].minv + tree[v].lazy; pushdown(v); int ans = INF,mid = (L + R)>>;
if(lt <= mid) ans = min(ans,getMin(L,mid,lt,rt,v<<)); if(rt > mid) ans = min(ans,getMin(mid+,R,lt,rt,v<<|)); pushup(v); return ans; } int getMax(int L,int R,int lt,int rt,int v) { if(L >= lt && R <= rt) return tree[v].maxv + tree[v].lazy; pushdown(v); int ans = -INF,mid = (L + R)>>;
if(lt <= mid) ans = max(ans,getMax(L,mid,lt,rt,v<<)); if(rt > mid) ans = max(ans,getMax(mid+,R,lt,rt,v<<|)); pushup(v); return ans; } int query(int L,int R,int p,int v) { if(L == R) return tree[v].minv + tree[v].lazy; pushdown(v); int mid = (L + R)>>;
int ans = ;
if(p <= mid) ans = query(L,mid,p,v<<); if(p > mid) ans = query(mid+,R,p,v<<|); pushup(v); return ans; } int main() { int N,M,Q,x,y,v; char s[]; while(~scanf("%d %d %d",&N,&M,&Q)) { memset(tree,,sizeof(tree)); while(Q--) { scanf("%s",s); if(s[] == 's') { scanf("%d",&x); printf("%d\n",query(,N,x,)); } else { if(s[] == 'c') { scanf("%d %d",&x,&v); y = x; } else if(s[] == 'g') scanf("%d %d %d",&x,&y,&v); int nv; if(v < ) { int minv = getMin(,N,x,y,); nv = minv + v < ? -minv:v; } else { int maxv = getMax(,N,x,y,); nv = maxv + v > M ? M-maxv:v;
}
update(,N,x,y,,nv);
printf("%d\n",nv);
}
}
}
return ;
}