题意:给定n<=100000个二维点,并且0<=x,y<=100000,求有多少个平行于坐标轴的正方形

思路:本来想hash的,但是感觉不好弄。。

后来感觉像是分块,最坏的情况就是那种x,y点稠密在一起的情况,并且x与y大致相同的情况下答案最多。。

然后就想到了跟分块很像的暴力。

对于每个点,用vx[x]记录有p.x==x的点的y,用vy[y]记录p.y==y的x

那么对于每个vx[], vy[]排序。。

接着对于每个点我们统计以它为右上角的正方形。。

那么对于每个点(x, y),我们二分查找x处于vy[y]的位置ty,我们二分查找y处于vx[x]的位置tx

那么如果tx<ty,我们直接统计vx[x][0]~vx[x][tx-1]的所有y是否有符合的

每次判断可以通过使用二分查找其他点是否存在。。

如果tx>=ty则统计vy[y][0]~vy[y][ty-1]的所有y是否符合。方法类似。

这样最坏情况下应该跟分块的复杂度差不多吧(不会证明)

code:

 #include <bits/stdc++.h>
#define M0(x) memset(x, 0, sizeof(x))
#define MP make_pair
#define PB push_back
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
#define x first
#define y second
const int maxn = ;
pair<int, int> p[maxn];
vector<int> vx[maxn], vy[maxn];
int n, m; void solve(){
int mx = , my = ;
repf(i, , n){
scanf("%d%d", &p[i].x, &p[i].y);
mx = max(p[i].x, mx);
my = max(p[i].y, my);
}
repf(i, , mx) vx[i].clear();
repf(i, , my) vy[i].clear();
repf(i, , n){
vx[p[i].x].PB(p[i].y);
vy[p[i].y].PB(p[i].x);
}
repf(i, , mx) sort(vx[i].begin(), vx[i].end());
repf(i, , my) sort(vy[i].begin(), vy[i].end());
int ans = ;
int tx, ty, x, y, t2, t3, d, nx, ny;
repf(i, , n){
x = p[i].x, y = p[i].y;
tx = lower_bound(vx[x].begin(), vx[x].end(), y) - vx[x].begin();
ty = lower_bound(vy[y].begin(), vy[y].end(), x) - vy[y].begin();
if (tx < ty){
t2 = ty;
for (int j = tx-; j >= ; --j){
d = y - vx[x][j];
nx = x - d;
t2 = lower_bound(vy[y].begin(), vy[y].end(), nx) - vy[y].begin();
if (vy[y][t2] == nx){
t3 = lower_bound(vx[nx].begin(), vx[nx].end(), vx[x][j]) - vx[nx].begin();
if (vx[nx][t3] == vx[x][j]) ++ans;
}
}
continue;
}
// t2 = tx;
for (int j = ty-; j >= ; --j){
d = x - vy[y][j];
ny = y - d;
t2 = lower_bound(vx[x].begin(), vx[x].end(), ny) - vx[x].begin();
if (vx[x][t2] == ny){
t3 = lower_bound(vy[ny].begin(), vy[ny].end(), vy[y][j]) - vy[ny].begin();
if (vy[ny][t3] == vy[y][j]) ++ans;
}
}
}
cout << ans << endl;
} int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
while (scanf("%d", &n) != EOF){
solve();
}
return ;
}

codeforces 425D的更多相关文章

  1. codeforces 425D Sereja and Squares n个点构成多少个正方形

    输入n个点,问可以构成多少个正方形.n,xi,yi<=100,000. 刚看题的时候感觉好像以前见过╮(╯▽╰)╭最近越来越觉得以前见过的题偶尔就出现类似的,可是以前不努力啊,没做出来的没认真研 ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. Home not found. Define system property "openfireHome" or create and add the openfire_init.xml file to the classpath

    启动openfire后出现这个错误,貌似什么配置没对吧.网上搜索了下,找到解决办法, $ vi /etc/profile在里面加入:export openfireHome=/opt/openfire ...

  2. 百度地图API示例之添加定位相关控件

    代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...

  3. C# 读取Excel内容

    一.方法 1.OleD方法实现该功能. 2.本次随笔内容只包含读取Excel内容,并另存为. 二.代码 (1)找到文档代码 OpenFileDialog openFile = new OpenFile ...

  4. Windows 7 下如何设置机器级别的DCOM权限

    Windows 7 下如何设置机器级别的DCOM权限 To grant Remote Activation permissions to the SMS Admins group From the S ...

  5. linux C学习笔记03--单链表

    单链表一直是程序员的基础,我也来复习下,下面是link.c中的代码,供main.c 调用,代码很简单,单链表的插入,删除,查找和遍历输出, #include <stdio.h> #incl ...

  6. jQuery.bind() 函数详解

    bind()函数用于为每个匹配元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 执行bind()时,事件处理函数会绑定到每个匹配元素上.因此你使用bind( ...

  7. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。

    今天单位一ASP.NET网站,里面有个功能是导出数据,发现一导出就报错,报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT ...

  8. [Swift]基础

    [Swift]基础 一, 常用变量 var str = "Hello, playground" //变量 let str1="Hello xmj112288" ...

  9. Quartus 的管脚分配

    与管脚分配相关的一些功能在assignments菜单下, Remove assignments... Back-Annotate Assignment... Import Assignment... ...

  10. 清除SQLServer日志的两种方法

    日志文件满而造成SQL数据库无法写入文件时,可用两种方法:一种方法:清空日志.1.打开查询分析器,输入命令DUMP TRANSACTION 数据库名 WITH NO_LOG2.再打开企业管理器--右键 ...