求助!20分,TLE!

P4148 简单题

_QyGyQ_ @ 2023-08-07 14:49:21

#include<bits/stdc++.h>
#define lc t[u].ls
#define rc t[u].rs
using namespace std;
const int N=2e6+7;
const double alpha=0.75;
using ll=long long;
struct Point{
    int dim[2],val;
    Point(){};
    Point(int x,int y,int vall){dim[0]=x,dim[1]=y,val=vall;}
};
Point order[N];
int cnt;
struct kd_tree{
    int ls,rs;
    int mi[2],ma[2];
    int sum;
    int size;
    Point p;
}t[N];
int tot,root;
int top,tree_stack[N];
int now;
bool cmp(Point a,Point b){
    return a.dim[now]<b.dim[now];
}
void update(int u){
    for(int i=0;i<2;i++){
        t[u].mi[i]=t[u].ma[i]=t[u].p.dim[i];
        if(lc){
            t[u].mi[i]=min(t[u].mi[i],t[lc].mi[i]);
            t[u].ma[i]=max(t[u].ma[i],t[lc].ma[i]);
        }
        if(rc){
            t[u].mi[i]=min(t[u].mi[i],t[rc].mi[i]);
            t[u].ma[i]=max(t[u].ma[i],t[rc].ma[i]);
        }
    }
    t[u].sum=t[lc].sum+t[u].p.val+t[rc].sum;
    t[u].size=t[lc].size+t[rc].size+1;
}
void slap(int u){
    if(!u) return;
    slap(lc);
    order[++cnt]=t[u].p;
    tree_stack[++top]=u;
    slap[rc];
}
int build(int l,int r,int d){
    if(l>r) return 0;
    int u;
    if(top) u=tree_stack[top--];
    else u=++tot;
    int mid=(l+r)>>1;
    now=d;
    nth_element(order+1,order+mid,order+r+1,cmp);
    t[u].p=order[mid];
    lc=build(l,mid-1,d^1);
    rc=build(mid+1,r,d^1);
    update(u);
    return u;
}
bool notbalance(int u){
    if(t[lc].size>alpha*t[u].size||t[rc].size>alpha*t[u].size){
        return true;
    }
    return false;
}
void Insert(int &u,Point now,int d){
    if(!u){
        if(top) u=tree_stack[top--];
        else u=++tot;
        lc=rc=0;
        t[u].p=now;
        update(u);
        return ;
    }
    if(now.dim[d]<=t[u].p.dim[d]) Insert(lc,now,d^1);
    else Insert(rc,now,d^1);
    update(u);
    if(notbalance(u)){
        cnt=0;
        slap(u);
        u=build(1,t[u].size,d);
    }
}
int query(int u,int x1,int y1,int x2,int y2){
    if(!u) return 0;
    int X1=t[u].mi[0],Y1=t[u].mi[1],X2=t[u].ma[0],Y2=t[u].ma[1];
    if(x1<=X1&&x2>=X2&&y1<=Y1&&x2>=X2) return t[u].sum;
    if(x1>X2||x2<X1||y1>Y2||y2<Y1) return 0;
    int ans=0;
    X1=X1=t[u].p.dim[0];
    Y1=Y2=t[u].p.dim[1];
    if(x1<=X1&&x2>=X2&&y1<=Y1&&x2>=X2) ans+=t[u].p.val;
    ans+=query(lc,x1,y1,x2,y2);+query(rc,x1,y1,x2,y2);
    return ans;
}
int main(){
    int n;
    cin>>n;
    int ans=0;
    for(;;){
        int opt;
        cin>>opt;
        if(opt==1){
            int x,y,val;
            scanf("%d%d%d",&x,&y,&val);
            x^=ans,y^=ans,val^=ans;
            Insert(root,Point(x,y,val),0);
        }
        if(opt==2){
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            x1^=ans,x2^=ans;
            y1^=ans,y2^=ans;
            ans=query(root,x1,y1,x2,y2);
            printf("%d\n",ans);
        }
        if(opt==3) break;
    }
    return 0;
}

by shengxuanyi2 @ 2024-04-07 20:59:00

@meng_cen tmd你k-d tree学完了吗


by _QyGyQ_ @ 2024-04-08 16:27:45

@shengxuanyi2 @Shengxuanyi01 你小号怎么这么多


by _QyGyQ_ @ 2024-04-08 16:27:53

@Shengxuanyi01


by _QyGyQ_ @ 2024-04-08 16:28:26

@Shengxuanyi1


by shengxuanyi2 @ 2024-04-08 16:45:09

@meng_cen

写你大坝根号重构。二进制分组多好啊


by _QyGyQ_ @ 2024-04-08 16:58:18

@shengxuanyi2 不会


by shengxuanyi2 @ 2024-04-08 20:02:49

@meng_cen 菜就多练


|