#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=200020;
struct hbr{
int y,y2,x,v;
bool operator <(hbr a)const{
return x<a.x;
}
}a[2*maxn+2];
struct tree{
int l,r,v,c;
}t[4*maxn+2];
unordered_map<int,int> b,d;
int c[2*maxn];
void build(int p,int l,int r){
t[p].l=l;
t[p].r=r;
t[p].v=t[p].c=0;
if(l==r){
return;
}
int m=(l+r)>>1;
build(p*2,l,m);
build(p*2+1,m+1,r);
}
void pushup(int p){
if(t[p].c){
t[p].v=d[t[p].r+1]-d[t[p].l];
}else{
t[p].v=t[2*p].v+t[2*p+1].v;
}
}
void add(int p,int l,int r,int v){
if(t[p].r<l||r<t[p].l)return;
if(l<=t[p].l&&t[p].r<=r){
t[p].c+=v;
pushup(p);
return ;
}
add(p*2,l,r,v);
add(p*2+1,l,r,v);
pushup(p);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i=0;i<n;i++){
int x1,y,x2,y2;
cin>>x1>>y>>x2>>y2;
a[2*i]={y,y2,x1,1};
a[2*i+1]={y,y2,x2,-1};
c[2*i]=y;
c[2*i+1]=y2;
}
sort(a,a+2*n);
sort(c,c+2*n);
int p=1;
for(int i=0;i<2*n;i++){
if(c[i]!=c[i+1]){
b[c[i]]=p;
d[p++]=c[i];
}
}
int s=0;
build(1,1,p);
for(int i=0;i<2*n-1;i++){
add(1,b[a[i].y],b[a[i].y2]-1,a[i].v);
s+=t[1].v*(a[i+1].x-a[i].x);
}
cout<<s;
return 0;
}
上述代码开了8倍空间,20pts,没有RE,但是WA。
开到12倍就过了。
属实玄学......