码迷,mamicode.com
首页 > 其他好文 > 详细

XTUOJ1247 Pair-Pair 预处理+暴力

时间:2016-06-18 10:07:52      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

分析:开个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\n",ret[1],ret[2],ret[3],ret[4]);
    }
    return 0;
}
View Code

 

XTUOJ1247 Pair-Pair 预处理+暴力

标签:

原文地址:http://www.cnblogs.com/shuguangzw/p/5595723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!