Level up is the task of all online games. It's very boooooooooring. There is only level up in those games, except level up.
In a online game, there are N heroes numbered id from 1 to N, each begins with level 1 and 0 Experience. They need to kill monsters to get Exp and level up.
There are many waves of monsters, each wave, the heroes with id from li to ri will come to kill monsters and those hero with level k will get ei*k Exp. If one hero's Exp reach Needk then the hero level up to level k immediately.
After some waves, I will query the maximum Exp from li to ri.
Now giving the information of each wave and Needk, please tell me the answer of my query.
In a online game, there are N heroes numbered id from 1 to N, each begins with level 1 and 0 Experience. They need to kill monsters to get Exp and level up.
There are many waves of monsters, each wave, the heroes with id from li to ri will come to kill monsters and those hero with level k will get ei*k Exp. If one hero's Exp reach Needk then the hero level up to level k immediately.
After some waves, I will query the maximum Exp from li to ri.
Now giving the information of each wave and Needk, please tell me the answer of my query.
The first line of each case contains three integers N(1<=N<=10000), K(2<=K<=10) and QW(1<=QW<=10000)each represent hero number, the MAX level and querys/waves number.
Then a line with K -1 integers, Need2, Need3...Needk.(1 <= Need2 < Need3 < ... < Needk <= 10000).
Then QW lines follow, each line start with 'W' contains three integers li ri ei (1<=li<=ri<=N , 1<=ei<=10000); each line start with 'Q' contains two integers li ri (1<=li<=ri<=N).
For each query, output the maximum Exp from li to ri.
Output a black line after each case.
2 3 3 5 1 2 W 1 1 1 W 1 2 1 Q 1 3 W 1 3 1 Q 1 3 5 5 8 2 10 15 16 W 5 5 9 W 3 4 5 W 1 1 2 W 2 3 2 Q 3 5 W 1 3 8 Q 1 2 Q 3 5
Case 1:
3
6
Case 2:
9
18
25
Case 1: At first ,the information of each hero is 0(1),0(1),0(1) [Exp(level)] After first wave, 1(2),0(1),0(1); After second wave, 3(3),1(2),0(1); After third wave, 6(3),3(3),1(2); Case 2: The information of each hero finally: 18(5) 18(5) 25(5) 5(2) 9(2)
题解:k值很小,所以可以在hero未升级处进行延迟标记,一旦有hero可以升级便更新。
#include<iostream>
#include<stdio.h>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=10005;
int cas,n,k,t;
ll need[15];
struct hero{
ll lev,gra,need,flag;
void init(){
lev=1;
gra=0;
flag=0;
}
void fun(ll val){
gra+=lev*val;
need-=val;
flag+=val;
}
}node[maxn<<2];
void pushdown(int rt){
if(node[rt].flag) {
node[rt<<1].fun(node[rt].flag);
node[rt<<1|1].fun(node[rt].flag);
node[rt].flag=0;
}
}
void pushup(int rt){
node[rt].gra=max(node[rt<<1].gra,node[rt<<1|1].gra);
node[rt].lev=max(node[rt<<1].lev,node[rt<<1|1].lev);
node[rt].need=min(node[rt<<1].need,node[rt<<1|1].need);
}
void build(int l,int r,int rt){
node[rt].init();
node[rt].need=need[2];
if(l==r) return;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void updata(int L,int R,int val,int l,int r,int rt){
if(L<=l&&r<=R){
if(val>=node[rt].need){
if(l==r){
ll &now=node[rt].lev;
node[rt].gra+=now*val;
while(node[rt].gra>=need[now+1]) now++;
ll tmp=need[now+1]-node[rt].gra;
node[rt].need=tmp/now+(tmp%now!=0);
}
else{
pushdown(rt);
int m=(l+r)>>1;
if(L<=m) updata(L,R,val,lson);
if(R>m) updata(L,R,val,rson);
pushup(rt);
}
}
else node[rt].fun(val);
return;
}
pushdown(rt);
int m=(l+r)>>1;
if(L<=m) updata(L,R,val,lson);
if(R>m) updata(L,R,val,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) return node[rt].gra;
pushdown(rt);
int m=(l+r)>>1;
ll ret=0;
if(L<=m) ret=max(ret,query(L,R,lson));
if(R>m) ret=max(ret,query(L,R,rson));
return ret;
}
int main(){
scanf("%d",&cas);
for(int j=1;j<=cas;j++){
printf("Case %d:\n",j);
scanf("%d%d%d",&n,&k,&t);
for(int tmp,i=2;i<=k;i++) scanf("%I64d",&need[i]);
need[k+1]=inf;
build(1,n,1);
while(t--){
char c;cin>>c;
int li,ri,ei;
if(c=='W'){
scanf("%d%d%d",&li,&ri,&ei);
updata(li,ri,ei,1,n,1);
}
else{
scanf("%d%d",&li,&ri);
printf("%I64d\n",query(li,ri,1,n,1));
}
}
printf("\n");
}
}