TLE60分求助!
  • 板块P4148 简单题
  • 楼主hexz01
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/6/12 20:32
  • 上次更新2024/6/13 08:16:53
查看原帖
TLE60分求助!
727076
hexz01楼主2024/6/12 20:32
#include <iostream>
#include <algorithm>
using namespace std;
const int N=2e5+7;
const double W=0.75;
int n, tot=0, root=0;
struct node{
	int x[2], val;
	node(){}
	node(int xx, int y, int vall){
		x[0]=xx, x[1]=y, val=vall;
	}
};
struct tnode{
	node x;
	int sum, sz, maxn[2], minn[2];
	int lc, rc;
};
tnode tree[N];
node tool[N];int retot=0;
int id[N], idtot=0;
int newnode(){
	return (idtot?id[idtot--]:++tot);
}
bool cmp0(node x, node y){
	return x.x[0]<y.x[0];
}
bool cmp1(node x, node y){
	return x.x[1]<y.x[1];
}
void pushup(int x){
	tree[x].maxn[0]=tree[x].minn[0]=tree[x].x.x[0];
	tree[x].maxn[1]=tree[x].minn[1]=tree[x].x.x[1];
	if(tree[x].lc){
		tree[x].maxn[0]=max(tree[x].maxn[0], tree[tree[x].lc].maxn[0]);
		tree[x].minn[0]=min(tree[x].minn[0], tree[tree[x].lc].minn[0]);
		tree[x].maxn[1]=max(tree[x].maxn[1], tree[tree[x].lc].maxn[1]);
		tree[x].minn[1]=min(tree[x].minn[1], tree[tree[x].lc].minn[1]);
	}
	if(tree[x].rc){
		tree[x].maxn[0]=max(tree[x].maxn[0], tree[tree[x].rc].maxn[0]);
		tree[x].minn[0]=min(tree[x].minn[0], tree[tree[x].rc].minn[0]);
		tree[x].maxn[1]=max(tree[x].maxn[1], tree[tree[x].rc].maxn[1]);
		tree[x].minn[1]=min(tree[x].minn[1], tree[tree[x].rc].minn[1]);
	}
	tree[x].sum=tree[tree[x].lc].sum+tree[tree[x].rc].sum+tree[x].x.val;
	tree[x].sz=tree[tree[x].lc].sz+tree[tree[x].rc].sz+1;
}
void addall(int x){
	if(x){
		id[++idtot]=x;
		tool[++retot]=tree[x].x;
		addall(tree[x].lc);
		addall(tree[x].rc);
	}
}
void build(int &x, int l, int r, int d){
	if(l<=r){
		int mid=(l+r)>>1;
		x=newnode();
		nth_element(tool+l, tool+mid, tool+r+1, d?cmp1:cmp0);
		tree[x].x=tool[mid];
		build(tree[x].lc, l, mid-1, !d);
		build(tree[x].rc, mid+1, r, !d);
		pushup(x);
	}else{
		x=0;
	}
}
void check(int &x){
	if(tree[tree[x].lc].sz<W*tree[tree[x].rc].sz || tree[tree[x].rc].sz<W*tree[tree[x].lc].sz){
		retot=idtot=0;
		addall(x);
		build(x, 1, retot, 0);
	}
}
void insert(int &x, node now, int d){
	if(!x){
		x=newnode();
		tree[x].x=now;
		pushup(x);
	}else{
		if(now.x[d]<=tree[x].x.x[d])
			insert(tree[x].lc, now, d);
		else
			insert(tree[x].rc, now, d);
		pushup(x);
		check(x);
	}
}
bool in(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
	return (x1<=x3 && y1<=y3 && x2>=x4 && y2>=y4);
}
bool out(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
	return (x1>x4 || x2<x3 || y1>y4 || y2<y3);
}
int query(int x, int x1, int y1, int x2, int y2){
	if(!x)
		return 0;
	if(in(x1, y1, x2, y2, tree[x].minn[0], tree[x].minn[1], tree[x].maxn[0], tree[x].maxn[1])){
		return tree[x].sum;
	}
	if(out(x1, y1, x2, y2, tree[x].minn[0], tree[x].minn[1], tree[x].maxn[0], tree[x].maxn[1]))
		return 0;
	int ans=0;
	if(in(x1, y1, x2, y2, tree[x].x.x[0], tree[x].x.x[1], tree[x].x.x[0], tree[x].x.x[1]))
		ans+=tree[x].x.val;
	ans+=query(tree[x].lc, x1, y1, x2, y2);
	ans+=query(tree[x].rc, x1, y1, x2, y2);
	return ans;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n;
	int lstans=0;
	while(1){
		int c;
		cin>>c;
		if(c==1){
			int x, y, A;
			cin>>x>>y>>A;
			x^=lstans;y^=lstans;A^=lstans;
			insert(root, node(x, y, A), 0);
		}else if(c==2){
			int x1, y1, x2, y2;
			cin>>x1>>y1>>x2>>y2;
			x1^=lstans;x2^=lstans;y1^=lstans;y2^=lstans;
			lstans=query(root, x1, y1, x2, y2);
			cout<<lstans<<endl;
		}else
			return 0;
	}
	return 0;
}

后两个点TLE。

2024/6/12 20:32
加载中...