```cpp
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
int re()
{
int s=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
s=s*10+ch-48,ch=getchar();
return s*f;
}
void wr(int s)
{
if(s<0)putchar('-'),s=-s;
if(s>9)wr(s/10);
putchar(s%10+48);
}
const int inf=1e5+7;
int n,ans,l[inf],r[inf];
int bok[inf<<1],boks;
struct Time{
int id,len;
bool operator <(const Time &b)const
{
return id<b.id;
}
}h[inf<<3];
int cnt;
bool operator <(const Time &a,const int &b)
{
return a.id<b;
}
int a[inf],s[inf];
int lowbit(int x){return x&(-x);}
void insert(int i,int k){while(i<=cnt)s[i]+=k,i+=lowbit(i);}
int ask(int i){int ret=0;while(i)ret+=s[i],i-=lowbit(i);return ret;}
signed main()
{
n=re();
for(int i=1;i<=n;i++)
{
l[i]=re(),r[i]=re();
bok[++boks]=l[i],bok[++boks]=r[i];
}
sort(bok+1,bok+boks+1);
int num=unique(bok+1,bok+boks+1)-bok-1;
for(int i=1;i<=num;i++)
{
h[++cnt].id=bok[i];h[cnt].len=1;
if(bok[i]-bok[i-1]>1)
{
int ls=bok[i]-bok[i-1]-1;
h[++cnt].id=bok[i]-1;h[cnt].len=ls;
}
}
sort(h+1,h+cnt+1);
for(int i=1;i<=n;i++)
{
l[i]=lower_bound(h+1,h+cnt+1,l[i])-h;
r[i]=lower_bound(h+1,h+cnt+1,r[i])-h;
}
for(int i=1;i<=cnt;i++)a[i]=i;
for(int i=1;i<=n;i++)
swap(a[l[i]],a[r[i]]);
for(int i=1;i<=cnt;i++)
ans+=h[i].len*(ask(cnt)-ask(a[i])),insert(a[i],h[i].len);
wr(ans);
return 0;
}
```
by Zvelig1205 @ 2022-09-15 21:08:35
@[极冬寒雪](/user/413020) 为啥您的代码我AC了?
by OoXiao_QioO @ 2022-09-16 07:45:02
@[OoXiao_QioO](/user/721429) ??但我还是 [WA](https://www.luogu.com.cn/record/86712689)
by Zvelig1205 @ 2022-09-16 07:57:54
@[极冬寒雪](/user/413020) 您的代码我交上去,re,把inf改成1e6+5,ac
by OoXiao_QioO @ 2022-09-16 08:01:31
@[OoXiao_QioO](/user/721429) 真棒,数组开小了……
by Zvelig1205 @ 2022-09-16 08:06:02
由于一些奇怪的计数方式,`cnt` 的大小不止 $10^5$,留此贴以警示后人(不过应该没人跟我一样傻)。
by Zvelig1205 @ 2022-09-16 08:07:20