分析:开个1000*1000的数组,预处理矩阵和,然后分类讨论就好
时间复杂度:O(n)
#include <cstdio>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=1e5+;
const int INF=0x3f3f3f3f;
const int mod=1e9+;
struct Node{
int x,y;
}p[N];
int a[][];
int high[],low[],mid[];
LL ret[];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(a,,sizeof(a));
memset(ret,,sizeof(ret));
memset(high,,sizeof(high));
memset(low,,sizeof(low));
memset(mid,,sizeof(mid));
for(int i=;i<=n;++i){
scanf("%d%d",&p[i].x,&p[i].y);
++a[p[i].x][p[i].y];
if(p[i].y>p[i].x)++high[p[i].x];
else if(p[i].y==p[i].x)++mid[p[i].x];
else ++low[p[i].x];
}
for(int i=;i<=m;++i){
low[i]+=low[i-];
mid[i]+=mid[i-];
high[i]+=high[i-];
}
for(int i=;i<=m;++i)
for(int j=;j<=m;++j)
a[i][j]+=a[i-][j]+a[i][j-]-a[i-][j-];
for(int i=;i<=n;++i){
if(p[i].y>p[i].x){
int tmp=,t;
t=high[m]-high[p[i].y];
tmp+=t;ret[]+=t;
t=low[m]-low[p[i].y]+mid[m]-mid[p[i].y];
tmp+=t;ret[]+=t;
t=high[p[i].y]-high[p[i].y-];
tmp+=t;ret[]+=t;
t=high[p[i].y-]-high[p[i].x];
tmp+=t;ret[]+=t;
t=a[p[i].x][m]-a[p[i].x][p[i].y];
tmp+=t;ret[]+=t;
ret[]+=1ll*(n-tmp);
}
else if(p[i].x==p[i].y){
int tmp=,t;
t=high[m]-high[p[i].x];
tmp+=t;ret[]+=t;
t=low[p[i].x]+mid[p[i].x];
tmp+=t;ret[]+=t;
ret[]+=1ll*(n-tmp);
}
else {
int tmp=,t;
t=low[p[i].y]+mid[p[i].y];
tmp+=t;ret[]+=t;
t=high[m]-high[p[i].y];
tmp+=t;ret[]+=t;
ret[]+=1ll*(n-tmp);
}
}
printf("%I64d %I64d %I64d %I64d\n",ret[],ret[],ret[],ret[]);
}
return ;
}