zstu 3125(线段树)

区间修改,加单点查询和修改。

区间增加某值,又要修改一点,加惰性标记好像是唯一办法了。

修改区间最大值最靠左的一点,先找到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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值