求助

P4148 简单题

zsaskk @ 2020-03-15 17:09:48

这个程序的空间怎么救啊

#include<bits/stdc++.h>
using namespace std;
#define reg register
#define chk_digit(c) (c>='0'&&c<='9')
inline int read() {
    reg int x=0,f=1;reg char c=getchar();
    while(!chk_digit(c)) { if(c=='-') f=-1;c=getchar(); }
    while(chk_digit(c)) x=x*10+c-'0',c=getchar();
    return x*f;
}
#define inf (1<<29)
#define maxsize 202000
#define mymin(x,y) (x>=y?y:x)
#define mymax(x,y) (x>=y?x:y)
#define mid (l+r>>1)
#define alpha 0.75
#define ls tr[x].l
#define rs tr[x].r
struct kd{ int d[2],max[2],min[2],l,r,val,size; }tr[maxsize<<1];
int n,las,tot,stac[maxsize],stop,root,ans,now;
inline int New() {
    if(stop) { --stop;return stac[stop]+1; }
    return ++tot;
}
struct node{ int d[2],val; }a[maxsize];
inline bool cmp(node a,node b) { return a.d[now]<b.d[now]; }
inline void update(int x) {
    for(reg int i=0;i<=1;++i) {
        tr[x].max[i]=tr[x].min[i]=tr[x].d[i];
        if(tr[x].l) tr[x].max[i]=mymax(tr[x].max[i],tr[ls].max[i]),tr[x].min[i]=mymin(tr[x].min[i],tr[ls].min[i]);
        if(tr[x].r) tr[x].max[i]=mymax(tr[x].max[i],tr[rs].max[i]),tr[x].min[i]=mymin(tr[x].min[i],tr[rs].min[i]);
    }
    tr[x].size=tr[ls].size+tr[rs].size+1;
}
inline int build(int l,int r,int dim) {
    if(l>r) return 0;
    int x=New();now=dim;
    nth_element(a+l,a+mid,a+r+1,cmp),tr[x].d[0]=a[mid].d[0],tr[x].d[1]=a[mid].d[1],tr[x].val=a[mid].val;
    ls=build(l,mid-1,dim^1),rs=build(mid+1,r,dim^1);
    update(x);
    return x;
}
inline void reset(int x,int y) { a[x].val=tr[y].val,a[x].d[0]=tr[y].d[0],a[x].d[1]=tr[y].d[1]; }
inline void rebuild(int x,int cnt) {
    if(ls) rebuild(ls,cnt);
    reset(cnt+tr[ls].size+1,x),stac[++stop]=x;
    if(rs) rebuild(rs,cnt+tr[ls].size+1); 
}
inline void chk(int &x,int dim) {
    if(tr[x].size*alpha<tr[ls].size||tr[x].size*alpha<tr[rs].size) 
        rebuild(x,0),x=build(1,tr[x].size,dim); 
}
inline void set_tree(node ret,int x) { tr[x].d[0]=ret.d[0],tr[x].d[1]=ret.d[1],tr[x].val=ret.val,update(x); }
inline void insert(node ret,int &x,int dim) {
    if(!x) { x=New();set_tree(ret,x);return; }
    if(ret.d[dim]<=tr[x].d[dim]) insert(ret,ls,dim^1);
    else insert(ret,rs,dim^1);
    update(x),chk(x,dim);
}
inline int caculate(int x,int x1,int x2,int y1,int y2) {
    if(tr[x].min[0]>=x1&&tr[x].max[0]<=x2&&tr[x].min[1]>=y1&&tr[x].min[2]<=y2) return 1;
    if(x2<tr[x].min[0]||y2<tr[x].min[1]||x1>tr[x].max[0]||y1>=tr[x].max[1]) return 0;
    return 2;
}
inline int query(int x,int x1,int x2,int y1,int y2) {
    int ans=0,d;
    if(tr[x].d[0]>=x1&&tr[x].d[0]<=x2&&tr[x].d[1]>=y1&&tr[x].d[1]<=y2) ans+=tr[x].val;
    if(ls) {
        d=caculate(ls,x1,x2,y1,y2);
        if(d==1) ans+=tr[ls].val;
        if(d==2) ans+=query(ls,x1,x2,y1,y2);
    }
    if(rs) {
        d=caculate(rs,x1,x2,y1,y2);
        if(d==1) ans+=tr[rs].val;
        if(d==2) ans+=query(rs,x1,x2,y1,y2);
    }
    return ans;
}
int main() {
    n=read();
    for(;;) {
        int opt=read(),x,y,p,q,val;
        if(opt==3) return 0;
        x=read()^las,y=read()^las;
        if(opt==1) val=read()^las,insert({x,y,val},root,0);
        else p=read()^las,q=read()^las,printf("%d\n",las=query(root,x,p,y,q));
    }
}

|