Analysis: Open an array of 1000*1000, preprocess the matrix and then sort the discussion on it.
Time complexity: O (N)
#include <cstdio>#include<iostream>#include<ctime>#include<vector>#include<cmath>#include<map>#include<queue>#include<algorithm>#include<cstring>using namespaceStd;typedefLong LongLL;Const intn=1e5+5;Const intinf=0x3f3f3f3f;Const intmod=1e9+7;structnode{intx, y;} P[n];inta[1005][1005];inthigh[1005],low[1005],mid[1005]; LL ret[5];intMain () {intn,m; while(~SCANF ("%d%d",&n,&m)) {memset (A,0,sizeof(a)); memset (ret,0,sizeof(ret)); memset (High,0,sizeof(high)); memset (Low,0,sizeof(low)); Memset (Mid,0,sizeof(mid)); for(intI=1; 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(intI=1; i<=m;++i) {Low[i]+=low[i-1]; Mid[i]+=mid[i-1]; High[i]+=high[i-1]; } for(intI=1; i<=m;++i) for(intj=1; j<=m;++j) A[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]; for(intI=1; i<=n;++i) { if(p[i].y>p[i].x) { inttmp=0, T; T=high[m]-HIGH[P[I].Y]; TMP+=t;ret[4]+=T; T=low[m]-low[p[i].y]+mid[m]-MID[P[I].Y]; TMP+=t;ret[3]+=T; T=high[p[i].y]-high[p[i].y-1]; TMP+=t;ret[3]+=T; T=high[p[i].y-1]-high[p[i].x]; TMP+=t;ret[3]+=T; T=a[p[i].x][m]-A[P[I].X][P[I].Y]; TMP+=t;ret[3]+=T; ret[2]+=1ll* (ntmp); } Else if(p[i].x==p[i].y) { inttmp=0, T; T=high[m]-high[p[i].x]; TMP+=t;ret[3]+=T; T=low[p[i].x]+mid[p[i].x]; TMP+=t;ret[1]+=T; ret[2]+=1ll* (ntmp); } Else { inttmp=0, T; T=low[p[i].y]+MID[P[I].Y]; TMP+=t;ret[1]+=T; T=high[m]-HIGH[P[I].Y]; TMP+=t;ret[3]+=T; ret[2]+=1ll* (ntmp); }} printf ("%i64d%i64d%i64d%i64d\n", ret[1],ret[2],ret[3],ret[4]); } return 0;}
View Code
XTUOJ1247 Pair-pair pretreatment + violence