Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=3954, a relatively good line tree problem, is worth doing.
The topic is notonlysuccess big God out, take this question to worship a big God, after all, I learned is notonlysuccess line segment tree, ORZ.
This problem is more complex, how to determine whether a wave of experience after the hero needs to upgrade, if the upgrade needs to deal with, how to maintain the value of exp interval, are the difficulty of this problem.
I am on the internet Baidu has the other people's solution to receive the inspiration, the concrete method is as follows:
Three arrays on the segment tree: level[] represents the interval maximum of the rank; exp[] represents the interval maximum of experience; the min tag indicates the minimum number of empirical cardinality required to make the interval a hero can be upgraded (the experience of the Spawner = e * level, E is the experience base).
It is known that the highest level hero of each interval has the highest experience value, so for each update, EXP[RT] + = level[rt] * E. For each update, if the current interval has a hero can be upgraded, that is, e >= min[rt], then the interval implementation point update, enumeration of each leaf node to check whether the hero needs to upgrade, the Hero upgrade to update the Min tag, if there is no hero upgrade, the normal interval update ( Update min tag, update exp value).
#include <iostream>#include<cstdio>#include<vector>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64#defineEPS 1e-8#defineINF Int_max#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1 | 1Const intMOD =10000007; Const intMAXN =10000+5;Const intN = A;intLEVEL[MAXN <<2], EXP[MAXN <<2], COL[MAXN <<2];DoubleMIN[MAXN <<2];intNeed[n];voidPushup (intRT) {Level[rt]= Max (Level[rt <<1], Level[rt <<1|1]); EXP[RT]= Max (Exp[rt <<1], Exp[rt <<1|1]); MIN[RT]= Min (min[rt <<1], Min[rt <<1|1]);}voidPushdown (intRT) { if(Col[rt]) {col[rt<<1] +=Col[rt]; Col[rt<<1|1] +=Col[rt]; Min[rt<<1] -=Col[rt]; Min[rt<<1|1] -=Col[rt]; Exp[rt<<1] + = Level[rt <<1] *Col[rt]; Exp[rt<<1|1] + = Level[rt <<1|1] *Col[rt]; COL[RT]=0; }}voidBuildintLintRintRT) { if(L = =R) {Level[rt]=1; EXP[RT]=0; MIN[RT]= need[2] *1.0; return; } Col[rt]=0; intm = (L + r) >>1; Build (Lson); Build (Rson); Pushup (RT);}voidLEVEL_UP (intEintLintRintRT) { if(L = = r) {//Update to leaf nodeEXP[RT] + = e *Level[rt]; while(Exp[rt] >= Need[level[rt] +1]) Level[rt]++;//Upgrade HeroesMIN[RT] = (Need[level[rt] +1]-EXP[RT]) *1.0/LEVEL[RT];//Update min Mark return; } pushdown (RT); intm = (L + r) >>1; Level_up (E, Lson); Level_up (E, Rson); Pushup (RT);}voidUpdateintLintRintEintLintRintRT) { if(L = =R) {Exp[rt]+ = e *Level[rt]; while(Exp[rt] >= Need[level[rt] +1]) Level[rt]++; MIN[RT]= (Need[level[rt] +1]-EXP[RT]) *1.0/Level[rt]; return; } if(l <= l && R >=r) {if(Min[rt]-e >0.00) {//No hero UpgradesMIN[RT]-=e; COL[RT]+=e; EXP[RT]+ = Level[rt] *e; } Else{//There's a hero upgradepushdown (RT); Level_up (E, L, R, RT); Pushup (RT); } return; } pushdown (RT); intm = (L + r) >>1; if(L >m) Update (L, R, E, Rson); Else if(R <=m) Update (L, R, E, Lson); Else{update (L, R, E, Lson); Update (L, R, E, Rson); } pushup (RT);}intQueryintLintRintLintRintRT) { if(l <= l && R >=r) {returnExp[rt]; } pushdown (RT); intm = (L + r) >>1; if(L >m)returnquery (L, R, Rson); Else if(R <=m)returnquery (L, R, Lson); Else returnMax (Query (L, R, Lson), query (L, R, Rson));}intMain () {intT, K, N, M, I, A, B, C; Charch[3]; CIN>>T; for(intK =1; K <= T; k++) {printf ("Case %d:\n", K); scanf (" %d%d%d", &n, &k, &m); for(i =2; I <= K; i++) scanf ("%d", &Need[i]); Need[i]=INF; Build (1N1); while(m--) {scanf ("%s", CH); if(ch[0] =='W') {scanf (" %d%d%d", &a, &b, &c); Update (A, B, C,1N1); } Else if(ch[0] =='Q') {scanf ("%d%d", &a, &b); printf ("%d\n", query (A, B,1N1)); }} puts (""); } return 0;}
HDU3954 segment Tree (interval update + point update)