XTUOJ1247 Pair-Pair 预处理+暴力

分析:开个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+5;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
struct Node{
  int x,y;
}p[N];
int a[1005][1005];
int high[1005],low[1005],mid[1005];
LL ret[5];
int main()
{
    int n,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(int i=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(int i=1;i<=m;++i){
        low[i]+=low[i-1];
        mid[i]+=mid[i-1];
        high[i]+=high[i-1];
      }
      for(int i=1;i<=m;++i)
        for(int j=1;j<=m;++j)
          a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
      for(int i=1;i<=n;++i){
        if(p[i].y>p[i].x){
             int tmp=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*(n-tmp);
        }
        else if(p[i].x==p[i].y){
          int tmp=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*(n-tmp);
        }
        else {
          int tmp=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*(n-tmp);
        }
      }
      printf("%I64d %I64d %I64d %I64d
",ret[1],ret[2],ret[3],ret[4]);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/shuguangzw/p/5595723.html