A. Watchmen
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).

They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .

The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.

Input

The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.

Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).

Some positions may coincide.

Output

Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.

Examples
input
3
1 1
7 5
1 5
output
2
input
6
0 0
0 1
0 2
-1 1
0 1
1 1
output
11
Note

In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and  for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.

题目是让你求有多少点,它们的曼哈顿距离 等于 欧几里得距离。就是(xi - xj) * (yi - yj) == 0;

用到了容斥原理,计算 xi & xj 相等的点有多少个,计算 yi & yj 相等的点有多少个,然后再减去 xi & xj 和 yi & yj 都想的点有多少个。

package codefroces345;

import java.io.*;
import java.util.*; public class C345{
/*
* java io 系统给的这么慢。。。时间是优化后的3倍。。。
* */
static class Pair{
int x, y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
} @Override
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(this == null) {
return false;
}
if(getClass() != obj.getClass()){
return false;
}
Pair other = (Pair)obj;
if(x != other.x)
return false;
if(y != other.y)
return false;
return true;
}
} public static void main(String[] args){
Scanner scanner = new Scanner(new InputStreamReader(System.in));
int n;
n = scanner.nextInt();
HashMap<Integer, Integer> xs = new HashMap<>();
HashMap<Integer, Integer> ys = new HashMap<>();
HashMap<Pair, Integer> both = new HashMap<>(); for(int i = 0; i < n; ++i) {
int x = scanner.nextInt();
int y = scanner.nextInt();
Pair p = new Pair(x, y);
xs.put(x, xs.getOrDefault(x, 0) + 1);
ys.put(y, ys.getOrDefault(y, 0) + 1);
both.put(p, both.getOrDefault(p, 0) + 1);
} long ans = 0;
for(int v : xs.values()){
ans += (long) v * (v-1) / 2;
}
for(int v : ys.values()){
ans += (long) v * (v-1) / 2;
}
for(int v : both.values()) {
ans -= (long) v * (v-1) / 2;
}
System.out.println(ans);
}
}

  

看人家们的代码,用到了BufferedReader包装加速

package codefroces345;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer; /**
* Created by lenovo on 2016-03-10.
*/ /*
* 经过 i/o 包装后,只要600ms左右,还是学的少
* */
public class C {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof; static class Pair {
int x, y; public Pair(int x, int y) {
this.x = x;
this.y = y;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
void solve() throws IOException {
int n = nextInt();
HashMap<Integer, Integer> xs = new HashMap<>();
HashMap<Integer, Integer> ys = new HashMap<>();
HashMap<Pair, Integer> both = new HashMap<>(); for (int i = 0; i < n; i++) {
int x = nextInt();
int y = nextInt();
Pair p = new Pair(x, y);
xs.put(x, xs.getOrDefault(x, 0) + 1);
ys.put(y, ys.getOrDefault(y, 0) + 1);
both.put(p, both.getOrDefault(p, 0) + 1);
} long ans = 0;
for (int v : xs.values()) {
ans += (long)v * (v - 1) / 2;
} for (int v : ys.values()) {
ans += (long)v * (v - 1) / 2;
} for (int v : both.values()) {
ans -= (long)v * (v - 1) / 2;
} out.println(ans);
}
C() throws IOException{
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
br.close();
}
public static void main(String[] args) throws IOException{
new C();
}
String nextToken(){
while(st == null || !st.hasMoreTokens()){
try{
st = new StringTokenizer(br.readLine());
} catch(IOException e) {
eof = true;
return null;
}
}
return st.nextToken();
} String nextString(){
try{
return br.readLine();
} catch (Exception e) {
eof = true;
return null;
}
} int nextInt() throws IOException {
return Integer.parseInt(nextToken());
} long nextLong() throws IOException {
return Long.parseLong(nextToken());
} double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
}

  

Codeforces Round #345 (Div. 1) A. Watchmen的更多相关文章

  1. Codeforces Round #345 (Div. 1) A - Watchmen 容斥

    C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...

  2. Codeforces Round #345 (Div. 1) A. Watchmen 模拟加点

    Watchmen 题意:有n (1 ≤ n ≤ 200 000) 个点,问有多少个点的开平方距离与横纵坐标的绝对值之差的和相等: 即 = |xi - xj| + |yi - yj|.(|xi|, |y ...

  3. Codeforces Round #345 (Div. 1) A. Watchmen (数学,map)

    题意:给你\(n\)个点,求这\(n\)个点中,曼哈顿距离和欧几里得距离相等的点对数. 题解: 不难发现,当两个点的曼哈顿距离等于欧几里得距离的时候它们的横坐标或者纵坐标至少有一个相同,可以在纸上画一 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

    A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  6. Codeforces Round #345 (Div. 2)

    DFS A - Joysticks 嫌麻烦直接DFS暴搜吧,有坑点是当前电量<=1就不能再掉电,直接结束. #include <bits/stdc++.h> typedef long ...

  7. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  8. Codeforces Round #345 (Div. 2) C (multiset+pair )

    C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...

  9. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

随机推荐

  1. 【codevs1282】约瑟夫问题

    题目描述 有编号从1到N的N个小朋友在玩一种出圈的游戏.开始时N个小朋友围成一圈,编号为I+1的小朋友站在编号为I小朋友左边.编号为1的小朋友站在编号为N的小朋友左边.首先编号为1的小朋友开始报数,接 ...

  2. Go 项目的目录结构 及 安装技巧

    项目目录结构如何组织,一般语言都是没有规定.但 Go 语言这方面做了规定,这样可以保持一致性 1.一般的,一个 Go 项目在 GOPATH 下,会有如下三个目录: |--bin |--pkg |--s ...

  3. android App使用新浪微博sdk的使用总结

    问题1:注册app的key 问题2:在微博开放平台,我的应用中心中,设置应用的基本信息的时候其中有一项,是设置你的应用的签名,签名是需要在安卓设备上安装一个生成签名的app(这个app界面很丑,这点我 ...

  4. mysql怎么查询一条记录的前一条记录和后一条记录

    上一条:select * from 表 where 数据id<@当前显示数据id order by 数据_id asc) limit 1下一条:select * from 表 where 数据i ...

  5. 玩QQ游戏,见到好几个图像是美女的QQ,就不始玩

    玩QQ游戏,见到好几个图像是美女的QQ,光占坑就是不开始玩 加了一个,发现是传播不良网站的QQ 聊天还是自动的 估计是利用webqq写的程序,也就那几句话来回重复,让你去注册网站什么 可以加这个Q去体 ...

  6. swift与OC之间不得不知道的21点

    swift与OC之间不得不知道的21点   自6月的WWDC大会上由苹果的大神Chris Lattner向我们首次展示swift至今已经大半年时间了,虽然绝大部分软件公司代码里还都见不到一丁点swif ...

  7. react+redux官方实例TODO从最简单的入门(6)-- 完结

    通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...

  8. Visual Studio 默认保存为UTF8编码

    Visual Studio (中文版)默认保存的文本文件是GB2312编码(代码页936)的,默认的行尾(End of line)是CRLF的. 如果仅仅是在windows下开发问题也不大,但是涉及到 ...

  9. 读《Android编程权威指南》

    因为去年双十二购买了一折的<Android 编程权威指南(第一版)>,在第二版出来后图灵社区给我推送了第二版的优惠码,激动之余就立马下单购买电子书,不得不说Big Nerd Ranch G ...

  10. WPF之命名空间和资源

    1.参考: https://msdn.microsoft.com/zh-cn/library/ms747086(v=vs.110).aspx http://www.cnblogs.com/cww201 ...