最后一个点WA了。
#include<bits/stdc++.h>
using namespace std;
struct line{
int x,y1,y2;
int op;
bool operator<(line s)const{
if(x==s.x) return op>s.op;
return x<s.x;
}
};
struct node{
int l,r;
int len;
int covernumber;
bool flagl,flagr;
int ans;
}tree[3000010];
long long ally[1000010];
int cnt,len;
int reply(int x){
return lower_bound(ally+1,ally+1+len,x)-ally;
}
void pushup(int rt,int l,int r){
if(tree[rt].covernumber){
tree[rt].len=ally[tree[rt].r+1]-ally[tree[rt].l];
tree[rt].flagl=tree[rt].flagr=true;
tree[rt].ans=1;
}
else{
tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;
tree[rt].flagl=tree[rt<<1].flagl;
tree[rt].flagr=tree[rt<<1|1].flagr;
tree[rt].ans=tree[rt<<1].ans+tree[rt<<1|1].ans;
if(tree[rt<<1].flagr&&tree[rt<<1|1].flagl) tree[rt].ans-=1;
}
}
void build(int l,int r,int rt){
tree[rt].l=l;
tree[rt].r=r;
tree[rt].len=tree[rt].covernumber=tree[rt].ans=0;
tree[rt].flagl=tree[rt].flagr=false;
if(l==r) return;
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
}
void update(int l,int r,int opt,int rt){
if(l<=tree[rt].l&&tree[rt].r<=r){
tree[rt].covernumber+=opt;
pushup(rt,l,r);
return;
}
int mid=(tree[rt].l+tree[rt].r)>>1;
if(l<=mid) update(l,r,opt,rt<<1);
if(mid<r) update(l,r,opt,rt<<1|1);
pushup(rt,l,r);
}
signed main(){
int n;
scanf("%d",&n);
int tot=0;
vector<line> v;
for(int i=1;i<=n;i++){
int x1,y1,x2,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
v.push_back((line){x1,y1,y2,1});
ally[++tot]=y1;
v.push_back((line){x2,y1,y2,-1});
ally[++tot]=y2;
}
sort(ally+1,ally+1+tot);
len=unique(ally,ally+1+tot)-(ally+1);
sort(v.begin(),v.end());
build(1,len-1,1);
long long answer=0;int pre=0;
for(int i=0;i<2*n-1;i++){
update(reply(v[i].y1),reply(v[i].y2)-1,v[i].op,1);
answer+=abs(tree[1].len-pre);
answer+=(v[i+1].x-v[i].x)*2*tree[1].ans;
pre=tree[1].len;
}
answer+=v[2*n-1].y2-v[2*n-1].y1;
printf("%lld",answer);
return 0;
}
向大佬们求助