G. 区间翻倍 DEBUG

· · 个人记录

先存rs (右子节点的起点s) 或者先update右/query右 都可以

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lc (p<<1)
#define rc ((p<<1)|1)
const int N=1e5+10,M=1e6+10,MOD=1e9+7;
struct node{// sum:数量 mx:最大值  mul:乘法 懒标记   
    int l,r,sum,mx,mul;
}tr[N*4];
int n,m;// p:tr中的下标  
void build(int p,int l,int r){//p:在一维数组中的下标  
    tr[p]=node{l,r,1,1,1};
    if(l==r) return;
    int mid=(l+r)/2;
    build(lc,l,mid);
    build(rc,mid+1,r);
    tr[p].sum=tr[lc].sum+tr[rc].sum;
}
void pushdown(int p){
    tr[lc].mul*=tr[p].mul;
    tr[lc].sum*=tr[p].mul;
    tr[lc].mx*=tr[p].mul;

    tr[rc].mul*=tr[p].mul;
    tr[rc].sum*=tr[p].mul;
    tr[rc].mx*=tr[p].mul;

    tr[p].mul=1;
}
int query(int p,int s,int x,int y){ 
    int l=s,r=s+tr[p].sum-1;
    if(x<=l&&r<=y){
        return tr[p].mx;    
    } 
    if(tr[p].l==tr[p].r){//叶结点 
        int L=max(l,x),R=min(r,y);
        return (R-L+1);
    }
    pushdown(p);
    int mid=s+tr[lc].sum-1;
    int rs=s+tr[lc].sum;
    int maxx=0;
    if(x<=mid) maxx=max(maxx,query(lc,s,x,y));
    if(y>mid) maxx=max(maxx,query(rc,rs,x,y));
    return maxx;
} //                 值区间 [x,y]
void update(int p,int s,int x,int y){
    int l=s,r=s+tr[p].sum-1;
    if(x<=l&&r<=y){
        tr[p].sum*=2;
        tr[p].mul*=2; 
        tr[p].mx*=2;
        return;
    }
    if(tr[p].l==tr[p].r){//叶结点 
        int L=max(l,x),R=min(r,y);
        tr[p].sum+=(R-L+1);
        tr[p].mx=tr[p].sum;
        return;
    }
    pushdown(p);
    int mid=s+tr[lc].sum-1;
    int rs=s+tr[lc].sum;//先存rs  
    if(x<=mid) update(lc,s,x,y);
    if(y>mid) update(rc,rs,x,y);//不然更新了左 这里rs会改 
    tr[p].sum=tr[lc].sum+tr[rc].sum;
    tr[p].mx=max(tr[lc].mx , tr[rc].mx);
}
signed main(){
    int T; cin>>T; 
    for(int tt=1;tt<=T;tt++){
        cout<<"Case #"<<tt<<":\n";
        cin>>n>>m;
        build(1,1,n);
        while(m--){
            char type; int x,y;
            cin>>type>>x>>y;
            if(type=='D'){//操作 
                update(1,1,x,y);
            }else{//查询  
                cout<<query(1,1,x,y)<<endl;
            }
//          for(int i=1;i<=9;i++){
//              cout<<tr[i].sum<<" ";
//          }
        }
    }
    return 0;
}

//1 2 3 4 5
//1 2 3 4 55
//12233 4 55
//1122233455