聚聚们,为什么我的线段树要开16倍才给过啊QAQ
查看原帖
聚聚们,为什么我的线段树要开16倍才给过啊QAQ
523864
paradoxxd楼主2021/11/17 23:37

知道不能开4倍,结果开8倍也re了

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
struct node{
	int yu,yd;
	int x;
	int flag;//-1 or 1
};
vector<int>ys;
vector<node>xs;
bool cmp(node a,node b){
	return a.x<b.x;
}
struct{
	int l,r;
	int cover;
	long long val;
}st[maxn*8];
void build(int l,int r,int rt){
	st[rt].l=ys[l];
	st[rt].r=ys[r];
	if(l+1==r) return;
	int m=l+r>>1;
	build(l,m,rt*2);
	build(m,r,rt*2+1);
}
inline void pushup(int rt){
	if(st[rt].cover>0) st[rt].val=st[rt].r-st[rt].l;//dont need +1?
	else st[rt].val=st[rt*2].val+st[rt*2+1].val;
}
void update(int rt,int s,int e,int flag){
	int l=st[rt].l;
	int r=st[rt].r;
	if(s<=l&&r<=e){
		st[rt].cover+=flag;
		pushup(rt);
		return;
	}
	int m=st[rt*2].r;
	if(s<m) update(rt*2,s,e,flag);
	if(m<e) update(rt*2+1,s,e,flag);
	pushup(rt);
}
int main(){
	int n;
	scanf("%d",&n);
	int i;
	int x1,x2,y1,y2;
	for(i=0;i<n;i++){
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		ys.push_back(y1);
		ys.push_back(y2);
		xs.push_back({y2,y1,x1,1});
		xs.push_back({y2,y1,x2,-1});
	}
	sort(xs.begin(),xs.end(),cmp);
	sort(ys.begin(),ys.end());
	ys.erase(unique(ys.begin(),ys.end()),ys.end());
	build(0,ys.size()-1,1);
	unsigned long long ans=0;
	int last=0;
	for(auto it:xs){
		ans+=st[1].val*(it.x-last);
		update(1,it.yd,it.yu,it.flag);
		last=it.x;
	}
	printf("%llu\n",ans);
}
2021/11/17 23:37
加载中...