区间修改,加单点查询和修改。
区间增加某值,又要修改一点,加惰性标记好像是唯一办法了。
修改区间最大值最靠左的一点,先找到logn个划分区间最左边内个包含最大值的区间,然后对该区间就比较好查了。
下面代码,并未提交,zstu 系统崩溃ing
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N = 1e5+10000;
int Max[N<<2],col[N<<2],n;
void build(int l,int r,int rt){
Max[rt] = col[rt] = 0;
if(l==r) return ;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void push_up(int l,int r,int rt){
Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void push_down(int l,int r,int rt){
if(col[rt]!=0){
col[rt<<1]+=col[rt];
col[rt<<1|1]+=col[rt];
Max[rt<<1]+=col[rt];
Max[rt<<1|1]+=col[rt];
col[rt]=0;
}
}
void update(int l,int r,int rt,int L,int R,int v){
if(L<=l&&r<=R){
col[rt]+=v;
Max[rt]+=v;
return ;
}
push_down(l,r,rt);
int m=(l+r)>>1;
if(L<=m) update(lson,L,R,v);
if(R> m) update(rson,L,R,v);
push_up(l,r,rt);
}
int pl,pr,ans;
int query(int l,int r,int rt,int L,int R){
if(l==r){
if(Max[rt] > ans) {ans = Max[rt]; pl=l; pr=r;}
return Max[rt];
}
push_down(l,r,rt);
int m=(l+r)>>1;
int res=0;
if(L<=m) res=query(lson,L,R);
if(R>m) res=max(res,query(rson,L,R));
return res;
}
void upd_one(int l,int r,int rt,int L,int R){
if(L<=l&&r<=R){
if(l==r){
Max[rt] = 0;
return ;
}
else {
push_down(l,r,rt);
int m=(l+r>>1);
if(Max[rt<<1]>=Max[rt<<1|1]) upd_one(lson,L,R);
else upd_one(rson,L,R);
}
}
else {
int m=(l+r)>>1;
push_down(l,r,rt);
if(L<=m) upd_one(lson,L,R);
else upd_one(rson,L,R);
}
push_up(l,r,rt);
}
int Query(int L,int R){
ans = -1;
query(1,n,1,L,R);
upd_one(1,n,1,pl,pr);
return ans;
}
int m;
int main()
{
while(scanf("%d %d",&n,&m)==2 && n){
while(m--){
char cmd[3];
int x,y,z;
scanf("%s",cmd);
if(cmd[0]=='I'){
scanf("%d %d %d",&x,&y,&z);
update(1,n,1,x,y,z);
}
else{
scanf("%d %d",&x,&y);
printf("%d\n",Query(x,y));
}
}
}
return 0;
}