https://nanti.jisuanke.com/t/41399
离散差分
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
template<typename T>inline void read(T &x)
{
x=0;
static int p;p=1;
static char c;c=getchar();
while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
while(isdigit(c)) {x=(x<<1)+(x<<3)+(c-48);c=getchar();}
x*=p;
}
//-----------------------------------------------
const int maxn=1e5+2;
struct node
{
int pos;
int f;
}a[maxn];
int cnt;
bool cmp(node x,node y)
{
if(x.pos==y.pos) return x.f>y.f;
return x.pos<y.pos;
}
int main()
{
//FAST_IO;
//freopen("input.txt","r",stdin);
int cas=0;
int T;
//cin>>T;
read(T);
while(T--)
{
cnt=0;
int n,m;
//cin>>n>>m;
read(n);
read(m);
for(int i=1;i<=m;i++)
{
int l,r;
read(l);
read(r);
++l;
++r;
++r;
a[++cnt].pos=l;
a[cnt].f=1;
a[++cnt].pos=r;
a[cnt].f=-1;
}
//
// a[++cnt].pos=n;
// a[cnt].f=-1;
sort(a+1,a+1+cnt,cmp);
int ans=0;
int sum=a[1].f;
int pre=a[1].pos;
//cout<<pre<<endl;
for(int i=2;i<=cnt;i++)
{
if(sum%2==1)
{
//cout<<a[i].pos<<endl;
ans+=a[i].pos-pre;
}
sum+=a[i].f;
pre=a[i].pos;
}
//cout<<"Case #"<<(cas++)<<": ";
//cout<<ans<<endl;
printf("Case #%d: %d\n",++cas,ans);
}
return 0;
}