Top
//STL
multiset<int >s;
unordered_set<int >s;
map<pair<int , int> , int>m;
unordered_map<int , int>m;
unique(a + 1 , a + 1 + n);
reverse(a + 1 , a + 1 + n);
stable_sort(a + 1 , a + 1 + n);//待写:扫描线
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
int tot=0;
struct Segment
{
int l,r,h,f;
} seg[200010];
bool cmp(Segment x,Segment y) { return x.h<y.h; }
int ttot=1;
struct Tree
{
int lson,rson,cnt,ans;
} tree[8000010];
int upd(int now,int ql,int qr,int qz,int l,int r)
{
if(!now) now=++ttot;
if(ql<=l && r<=qr)
{
tree[now].cnt+=qz;
if(tree[now].cnt==1) tree[now].ans=r-l+1;
if(tree[now].cnt==0) tree[now].ans=tree[tree[now].lson].ans+tree[tree[now].rson].ans;
}
else
{
int mid=(l+r)>>1;
if(ql<=mid) tree[now].lson=upd(tree[now].lson,ql,qr,qz,l,mid);
if(qr>mid) tree[now].rson=upd(tree[now].rson,ql,qr,qz,mid+1,r);
if(tree[now].cnt==0) tree[now].ans=tree[tree[now].lson].ans+tree[tree[now].rson].ans;
}
return now;
}
signed main()
{
cin>>n;
for(int i=1; i<=n; ++i)
{
int x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2;
seg[++tot]={x1,x2,y1,1},seg[++tot]={x1,x2,y2,-1};
}
sort(seg+1,seg+tot+1,cmp);
int ans=0;
for(int i=1; i<tot; ++i)
{
upd(1,seg[i].l,seg[i].r-1,seg[i].f,0,1e9);
ans+=tree[1].ans*(seg[i+1].h-seg[i].h);
}
cout<<ans;
return 0;
}//模板tools:
train list:
Article: