题目链接:https://ac.nowcoder.com/acm/contest/1111/E
占坑有空补详解
code
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm 1006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 7
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m;
int a[maxm][maxm],high[maxm],low[maxm],mid[maxm],b[maxn][2];
ll ans[5];
int main()
{
while(~scanf("%d%d",&n,&m)){
inc(i,1,n){
b[i][0]=read();b[i][1]=read();
a[b[i][0]][b[i][1]]++;
if(b[i][0]<b[i][1])high[b[i][0]]++;
else if(b[i][0]==b[i][1])mid[b[i][0]]++;
else low[b[i][0]]++;
}
inc(i,1,m)inc(j,1,m)a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
inc(i,1,m)high[i]+=high[i-1],mid[i]+=mid[i-1],low[i]+=low[i-1];
inc(i,1,n){
if(b[i][0]<b[i][1]){
int tmp=0,t;
t=high[m]-high[b[i][1]];
tmp+=t,ans[4]+=t;
t=low[m]-low[b[i][1]]+mid[m]-mid[b[i][1]];
tmp+=t,ans[3]+=t;
t=high[b[i][1]]-high[b[i][1]-1];
tmp+=t,ans[3]+=t;
t=high[b[i][1]-1]-high[b[i][0]];
tmp+=t,ans[3]+=t;
t=a[b[i][0]][m]-a[b[i][0]][b[i][1]];
tmp+=t,ans[3]+=t;
ans[2]+=n-tmp;
}
else if(b[i][0]==b[i][1]){
int tmp=0,t;
t=high[m]-high[b[i][1]];
tmp+=t,ans[3]+=t;
t=mid[b[i][0]];
tmp+=t,ans[1]+=t;
t=low[b[i][0]];
tmp+=t,ans[1]+=t;
ans[2]+=n-tmp;
}
else {
int tmp=0,t;
t=high[m]-high[b[i][1]];
tmp+=t,ans[3]+=t;
t=low[b[i][1]]+mid[b[i][1]];
tmp+=t,ans[1]+=t;
ans[2]+=n-tmp;
}
}
printf("%lld %lld %lld %lld\n",ans[1],ans[2],ans[3],ans[4]);
inc(i,1,m)inc(j,1,m)a[i][j]=0;
inc(i,1,m)high[i]=low[i]=mid[i]=0;
inc(i,1,4)ans[i]=0;
}
return 0;
}