线段树经典例题求助大佬

P1253 扶苏的问题

cat_lover1 @ 2023-11-16 00:25:26

思路几乎与第一篇题解相同,得40分,后6个点都TLE了

#define max(a,b) ((a)>(b)?(a):(b))
#define mid l+(r-l>>1)
#define lc h<<1
#define rc h<<1|1
#define IN x<=l&&r<=y
#define OUT (y<l||r<x)
#define LL long long
#define nul 1000000001
tr[4000001],t1[4000001],t2[4000001],n,q,op,x,y,z;
void make_tag1(h,x){
  tr[h]=t1[h]=x,t2[h]=0;
}
void make_tag2(h,x){
  t2[h]+=x,tr[h]+=x;
  t1[h]!=nul&&(t1[h]+=x);
}
void pushup(h){tr[h]=max(tr[lc],tr[rc]);}
void pushdown(h){
  if(t1[h]!=nul){
    make_tag1(lc,t1[h]),make_tag1(rc,t1[h]);
    t1[h]=nul;
  }
  else if(t2[h]){
    make_tag2(lc,t2[h]),make_tag2(rc,t2[h]);
    t2[h]=0;
  }
}
void build(h,l,r){
  if(l^r){
    build(lc,l,mid),
    build(rc,mid+1,r);
    pushup(h);
  }
  else scanf("%d",tr+h);
}
qry(h,l,r){
  //printf("%d: %d %d %d %d %d\n",op,h,l,r,x,y);
  if(IN)return tr[h];
  if(!OUT){
    pushdown(h);
    return max(qry(lc,l,mid),qry(rc,mid+1,r));
  }
  return -nul;
}                            
void upd(h,l,r){
  //printf("%d: %d %d %d %d %d %d\n",op,h,l,r,x,y,z);
  if(IN)op==1?make_tag1(h,z):(make_tag2(h,z));
  else if(!OUT)pushdown(h),upd(lc,l,mid),upd(rc,mid+1,r),pushup(h);
}
void init(){
  for(int i=0;i<4000001;++i)t1[i]=nul;
  scanf("%d%d",&n,&q);
  build(1,1,n);
}
void solve(){
  scanf("%d%d%d",&op,&x,&y);
  if(op^3)scanf("%d",&z),upd(1,1,n);
  else printf("%d\n",qry(1,1,n));
}
main(){
  for(init();q--;)solve();
}

by cat_lover1 @ 2023-11-16 09:46:58

@5t0_0r2 @Buried_Dream 感谢,祝您NOIP rp++!


上一页 |