#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200002],c[200002];
int cnt=0,sum=0;
struct tree{
int l,r,sum,add,num=0;
}t[400009];
map<int,int> mp1;
struct node{
int a,b,c,d;
int l,r;
int h;
}k[200009];
struct st{
int s,p;
int l,r;
int h;
}m[100009];
bool asd(const st l,const st r){
return l.s<r.s;
}
bool cmp(const node l,const node r){
return l.b<r.b;
}
//1 2 4 8
void build(int l,int r,int p){
t[p].l=l;
t[p].r=r;
if(l==r){
t[p].sum=c[l];
return;
}
build(l,(l+r)/2,p*2);
build((l+r)/2+1,r,p*2+1);
t[p].sum=t[p*2].sum+t[p*2+1].sum;
return;
}
void sp(int p){
t[p*2].add=t[p].add;
t[p*2+1].add=t[p].add;
if(t[p].add) t[p*2].num=t[p*2].sum;
if(t[p].add) t[p*2+1].num=t[p*2+1].sum;
}
void add(int x,int y,int k,int p=1){
if(x<=t[p].l&&y>=t[p].r){
if(k) t[p].add++;
else t[p].add--;
if(t[p].add) t[p].num=t[p].sum;
return;
}
sp(p);
int mid=(t[p].l+t[p].r)/2;
if(x<=mid) add(x,y,k,p*2);
if(y>mid) add(x,y,k,p*2+1);
t[p].num=t[p*2].num+t[p*2+1].num;
return;
}
int ask(int x,int y,int p=1){
int ans=0;
//cout<<t[p].num<<endl;
//cout<<"add:"<<t[p].add<<endl;
if(x<=t[p].l&&y>=t[p].r) return t[p].num;
sp(p);
int mid=t[p].l+t[p].r>>1;
if(x<=mid) ans+=ask(x,y,p*2);
if(y>mid) ans+=ask(x,y,p*2+1);
return ans;
}
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int temp1,temp2;//1更小,x
cin>>temp1>>k[i*2-1].b>>temp2>>k[i*2].b;
int temp;
if(temp1>temp2){
temp=temp1;
temp1=temp2;
temp2=temp;
}
sum++;
m[sum]={temp1,1};
m[sum].l=k[i*2-1].b;
m[sum].r=k[i*2].b;
sum++;
m[sum]={temp2,0};
m[sum].l=k[i*2-1].b;
m[sum].r=k[i*2].b;
}
sort(k+1,k+n*2+1,cmp);
sort(m+1,m+n*2+1,asd);
for(int i=1;i<=n*2;i++){
if(!mp1.count(k[i].b)){
cnt++;
mp1[k[i].b]=cnt;
}
}
for(int i=1;i<=n*2;i++){
m[i].l=mp1[m[i].l];
m[i].r=mp1[m[i].r];
}
sort(m+1,m+n*2+1,asd);
int len=mp1.size()-1;
int lo=1;
for(map<int,int>::iterator it=mp1.begin();it!=mp1.end();it++,lo++){
a[lo]=it->first;
}
for(int i=1;i<=len;i++){
c[i]=a[i+1]-a[i];
//cout<<c[i]<<' ';
}
build(1,len,1);
//cout<<endl;
//cout<<ask(1,len)<<endl;
//cout<<len<<endl;
int ans=0;
for(int i=1;i<2*n;i++){
//cout<<m[i].l<<' '<<m[i].r-1<<' '<<m[i].p<<' '<<ask(1,len)<<"*"<<m[i+1].s-m[i].s<<endl;
//add(1,len-1,m[i].p);
add(m[i].l,m[i].r-1,m[i].p);
// cout<<ask(1,len)<<" "<<m[i+1].s-m[i].s<<endl;
ans+=ask(1,len)*(m[i+1].s-m[i].s);
}
cout<<ans;
return 0;
}
可能写的比较奇怪(?,下了一组数据,应该是漏考虑了一些情况,差的不多。