分析:开个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 ;
}

XTUOJ1247 Pair-Pair 预处理+暴力的更多相关文章

  1. CodeForces 359D Pair of Numbers (暴力)

    题意:给定一个正整数数组,求最长的区间,使得该区间内存在一个元素,它能整除该区间的每个元素. 析:暴力每一个可能的区间,从数组的第一个元素开始考虑,向两边延伸,设延伸到的最左边的点为l, 最右边的点为 ...

  2. 51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1158 1158 全是1的最大子矩阵  基准时间限制:1 秒 空 ...

  3. bzoj 3752: Hack 预处理+暴力dfs

    题目大意: 定义字符串的hash值\(h = \sum_{i=0}^{n-1}p^{n-i-1}s_i\) 现在给定K个长度不超过L的字符串S,对于每个字符串S,求字典序最小长度不超过L的字符串T使得 ...

  4. C++学习之Pair

    C++学习之Pair Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型, ...

  5. 找最大重复次数的数和重复次数(C++ Pair)

    Problem A: 第一集 你好,世界冠军 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 265  Solved: 50[Submit][Statu ...

  6. STL模板整理 pair

    pair pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同.如果一个函数有两个返回值的话,如果是相同类型,就可以用数组返回,如果是不同类型,就可以自己写个struct ,但为了方便就 ...

  7. C++编程-&gt;pair(对组)

    pair 是 一种模版类型.每一个pair 能够存储两个值.这两种值无限制,能够是tuple.vector ,string,struct等等. 首先来看一下pair的函数 初始化.复制等相关操作例如以 ...

  8. STL pair 常见用法详解

    <算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...

  9. C++ pair

    C++ pair Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型,第一 ...

随机推荐

  1. CF 221div2 A. Lever

    A. Lever 题目:http://codeforces.com/contest/376/problem/A 题意:杠杆原理 比两边的重量 input =^== output balance 9== ...

  2. lintcode: 有效的括号序列

    题目: 有效的括号序列 给定一个字符串所表示的括号序列,包含以下字符: '(', ')', '{', '}', '[' and']', 判定是否是有效的括号序列. 样例 括号必须依照 "() ...

  3. ubuntu 修改主机及主机名

    修改主机: sudo vim /etc/hostname sudo vim /etc/hosts 修改用户名: sudo vim /etc/passwd sudo mv /home/yinggc /h ...

  4. 显示Servlet API主要版本,次要版本以及服务器系统信息

    package com.mhb; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servle ...

  5. Android IllegalStateException: The specified child already has a parent问题解决办法

    最近遇到一个很让人头疼的问题,使用viewpager动态添加页面或者删除页面时出现了问题(java.lang.IllegalStateException: The specified child al ...

  6. Android getActionBar()报空指针异常

    1. 加载完视图后,再去获取: 写在setContentView()后面. 2.sdk版本: Actionbar的主题在3.0以后才有,使用的时候要确保,最低的版本不能小于3.0. <uses- ...

  7. SQL效率问题

    关于in和or的效率对比:http://blog.chinaunix.net/uid-20639775-id-3416737.html 在没有索引的情况下,in的效率高 SQL语句效率:http:// ...

  8. NDK(15)在ndk代码中注册和注销native函数

    转自:  http://www.cnblogs.com/canphp/archive/2012/11/13/2768937.html C和C++注册native函数的方式大致上相同,下面给出具体的代码 ...

  9. [Codeforces677B]Vanya and Food Processor(模拟,数学)

    题目链接:http://codeforces.com/contest/677/problem/B 题意:n个土豆,每个土豆高ai.现在有个加工机,最高能放h,每次能加工k.问需要多少次才能把土豆全加工 ...

  10. C++ 中字符串标准输入的学习及实验

    声明:下面实验中[]里面表示要输入里面的符号,[]符号本身并未输入 1.cin>> cin使用空白(空格.制表符.回车)来确定字符串的结束位置. cin会将换行符留在输入输出队列中. #i ...