题目链接:

I. Special Squares

There are some points and lines parellel to x-axis or y-axis on the plane. If arbitrary chosen two lines parallel to x-axis and two lines parallel to y-axis, one rectangle, or sometimes a square, will be formed. If a square is formed and there is one or more point in the square or on the side of the square, the square is called a "special square". Please find the number of special squares.

 

Input

The 1st line contains three positive integer n1, n2 and n3. n1 stands for the number of lines parallel to x-axis, n2 stands for the number of lines parallel to y-axis, n3 stands for the number of points.(0<n1, n2, n3≤1000)Each of the 2nd line to (n1+1)th line gives an integer y_i (0≤y_i≤1000), means a line with equation y=y_i.Each of the (n1+2)th line to (n1+n2+1)th line gives an integer x_j (0≤x_j≤1000), means a line with equation x=x_j.Each of the last three lines gives two integers px_k and py_k (0≤px_k,py_k≤1000), means a point with coordinate (px_k, py_k).

 

Output

Output one line containing an integer specifies the number of special squares. The test data ensures that the result is less than 2^31

 

Sample Input

4 4 3
0 2 4 6
0 2 4 6
1 1
3 3
6 6
 

Sample Output

8

题意:

给出一些x和y方向上的直线,然后让分别选两个会形成长方形,其中是正方形且在其内部和边界上有给出的点时叫这种正方形,问有多少个这种正方形;

思路:

把给出的点求出一个二维前缀和,然后枚举一个方向上的两个,在枚举另一个方向上的一条线,这就得到了这个正方形,然后用前缀和判断是否存在点,复杂度是O(n*n*n)数据范围给的有问题,我开大了一倍才过;
题解说枚举对角线,然后枚举坐下角,这样复杂度是O(n*n),想不通为啥是这个复杂度; AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
//#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); //typedef long long LL;
typedef unsigned long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e3+20;
const int maxn=2e3+20;
const double eps=1e-3; int sum[maxn][maxn],su[maxn][maxn],n1,n2,n3;
int x[maxn],y[maxn],vis[2*maxn],a[maxn][maxn];
int main()
{
mst(a,0);
mst(vis,0);
read(n1);read(n2);read(n3);
for(int i=1;i<=n1;i++)read(y[i]),y[i]++;
for(int i=1;i<=n2;i++)read(x[i]),x[i]++,vis[x[i]]=1;
sort(y+1,y+n1+1);sort(x+1,x+n2+1);
int fx,fy;
for(int i=1;i<=n3;i++)
{
read(fx);read(fy);
fx++;fy++;
a[fx][fy]=1;
}
for(int i=1;i<N;i++)
for(int j=1;j<N;j++)
su[i][j]=su[i][j-1]+a[i][j];
for(int i=1;i<N;i++)
for(int j=1;j<N;j++)
sum[i][j]=sum[i-1][j]+su[i][j];
int ans=0,dx,dy,ux,uy;
for(int i=1;i<=n1;i++)
{
for(int j=i+1;j<=n1;j++)
{
int l=y[j]-y[i];
for(int k=1;k<=n2;k++)
{
if(x[k]+l>=N||x[k]+l<=0)continue;
else if(!vis[x[k]+l])continue;
else
{
dx=x[k];dy=y[i];
ux=x[k]+l;uy=y[j];
if(sum[ux][uy]+sum[dx-1][dy-1]-sum[ux][dy-1]-sum[dx-1][uy]>0)ans++;
}
}
}
}
printf("%d\n",ans);
return 0;
}

  

poj-3739. Special Squares(二维前缀和)的更多相关文章

  1. poj_3179 Corral the Cows (二分+二维前缀和+离散化)

    [题目链接] http://poj.org/problem?id=3179 [参考] http://www.cnblogs.com/evenbao/p/9243183.html [算法] 二分答案+判 ...

  2. openjudge1768 最大子矩阵[二维前缀和or递推|DP]

    总时间限制:  1000ms 内存限制:  65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...

  3. COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)

    题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...

  4. Good Bye 2015 C. New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

  5. 计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和

    题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将 ...

  6. 二维前缀和模板题:P2004 领地选择

    思路:就是使用二维前缀和的模板: 先放模板: #include<iostream> using namespace std; #define ll long long ; ll a[max ...

  7. 二维前缀和好题hdu6514

    #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; ]; )* ...

  8. P2280 [HNOI2003]激光炸弹(二维前缀和)

    题目描述 一种新型的激光炸弹,可以摧毁一个边长为R的正方形内的所有的目标.现在地图上有n(n≤10000)个目标,用整数xi,yi(0≤xi,yi≤5000)表示目标在地图上的位置,每个目标都有一个价 ...

  9. Gym 102091L Largest Allowed Area 【二分+二维前缀和】

    <题目链接> 题目大意:给你一个由01组成的矩形,现在问你,该矩形中,最多只含一个1的正方形的边长最长是多少. 解题分析: 用二维前缀和维护一下矩形的01值,便于后面直接$O(1)$查询任 ...

随机推荐

  1. [moka同学摘录]在Centos 6.5下成功安装和配置了vim7.4

    来源:https://my.oschina.net/gzyh/blog/266097 资源下载地址: 链接:http://pan.baidu.com/s/1kVuaV5P 密码:xkq9   摘要: ...

  2. ADO.NET 增删改查的基本用法

    ADO.NET:数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中也可以将数据库中的数据提取到内存中供程序调用 所有数据访问技术的基础 连接 ...

  3. OS X 在Cisco无线环境下丢包分析 part 1

    补发一篇博客,之前遇到的没有写成博文的一个情况.我擦,那一阵儿真是被无线搞疯了. 现象:苹果OS X用户连入WiFi之后莫名丢包,而且有规律的丢,丢个5s恢复正常,再过会儿再丢5s左右. 就如同这样 ...

  4. 详解JavaScript函数模式

    JavaScript设计模式的作用是提高代码的重用性,可读性,使代码更容易的维护和扩展.在javascript中,函数是一类对象,这表示他可以作为参数传递给其他函数:此外,函数还可以提供作用域. 创建 ...

  5. This application is currently offline. To enable the application, remove the app_offline.htm file from the application root directory.

    IIS提示:This application is currently offline. To enable the application, remove the app_offline.htm f ...

  6. Interoperability between Java and SharePoint 2013 on Premises

    http://ctp-ms.blogspot.com/2012/12/interoperability-between-java-and.html   Introduction One of the ...

  7. 实战3--应用EL表达式判断用户登录信息

    1.判断用户名是否为空,空则显示提示信息 (1)编写index.jsp页面 <%@ page language="java" contentType="text/h ...

  8. Python数据结构与算法--List和Dictionaries

    Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...

  9. HTML5--Audio

    一.Audio标签 Web 上的音频 直到现在,仍然不存在一项旨在网页上播放音频的标准. 今天,大多数音频是通过插件(比如 Flash)来播放的.然而,并非所有浏览器都拥有同样的插件. HTML5 规 ...

  10. OC中代理的使用

    通常代理的使用需要以下几个步骤: 1.制定协议.协议可以在委托对象的.h中声明,也可以在单独的.h中声明.制定协议后,在协议中声明需要代理对象来实现的方法. 2.设置代理属性.制定协议后需要为委托对象 ...