无意间找到以前的一段代码,决定发出来
#include<bits/stdc++.h>
#define ls rt<<1
#define rs rt<<1|1
#define mid hl+hh>>1
using namespace std;
const int MXN=1e6+5;
int n;
int Y[MXN<<1];long long xx1,xx2,yy1,yy2;
struct Line{
int hl,hh,x;
int mark;
bool operator<(const Line &a)
const{
return x<a.x;
}
}line[MXN<<1];
struct Tree{
int hl,hh;
int tag;
long long len;
}tree[MXN<<1];
void build(int hl,int hh,int rt)
{
tree[rt]={hl,hh,0,0};
if(hl==hh)
{
return;
}
build(hl,mid,ls);
build(mid+1,hh,rs);
}
void pushup(int rt)
{
int hl=tree[rt].hl;int hh=tree[rt].hh;
if(tree[rt].tag)
{
tree[rt].len=Y[hh+1]-Y[hl];
}
else
{
tree[rt].len=tree[ls].len+tree[rs].len;
}
}
void update(int HL,int HH,int v,int rt)
{
int hl=tree[rt].hl,hh=tree[rt].hh;
if(Y[hh+1]<=HL||HH<=Y[hl])return;
if(HL<=Y[hl]&&Y[hh+1]<=HH)
{
tree[rt].tag+=v;
pushup(rt);
return;
}
update(HL,HH,v,ls);
update(HL,HH,v,rs);
pushup(rt);
}
int main()
{
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>xx1>>yy1>>xx2>>yy2;
Y[i*2-1]=yy1;
Y[i*2]=yy2;
line[i*2-1]={yy1,yy2,xx1,1};
line[i*2]={yy1,yy2,xx2,-1};
}
n<<=1;
sort(line+1,line+1+n);
sort(Y+1,Y+n+1);
int tot=unique(Y+1,Y+n+1)-Y-1;
build(1,tot-1,1);
cout<<1;
long long ans=0;
for(int i=1;i<n;++i)
{
update(line[i].hl,line[i].hh,line[i].mark,1);
ans+=tree[1].len*(line[i+1].x-line[i].x);
// cout<<ans;
}
cout<<ans<<endl;
return 0;
}