题目链接:

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. oracle sql 语句优化

    (1)选择最有效率的表名顺序(只在基于规则的优化器中有效):Oracle的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理 ...

  2. ahjesus 安装mongodb企业版for ubuntu

    导入共匙 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 创建源列表 echo 'deb http ...

  3. 使用PowerQuery操作OData数据

             Excel是我们耳熟的办公软件.PowerQuery是一个允许连接多种数据源的Excel插件.它能从一个网页上智能查询数据.使用PowerQuery能合并数据集使用join,merg ...

  4. C# Sqlite事务

    在 C#中执行Sqlite数据库事务有两种方式:Sql代码和C#代码 1.Sql代码: BEGIN… COMMIT /ROLLBACK 2.C#代码: using (SQLiteConnection ...

  5. udid替代方案

    转自http://www.cnblogs.com/zhulin/archive/2012/03/26/2417860.html UDID替代方案   背景: 大多数应用都会用到苹果设备的UDID号,U ...

  6. Microsoft Dynamics CRM 前瑞开发

    做CRM开发最大的感受就是其前瑞开发过程中,调试起来比较麻烦,需要做一些断点还要配制一些浏览器设置,对新手来说比较困难.还有就是对REST调试,经常为了调试一个正确的结果而花费大量的时间.现在推荐一个 ...

  7. MSCRM 2011 操作大全

    CRM字段类型:货币:new Money(Decimal){SQL更新Money类型字段,需要同时更新_base字段,存在汇率差的时候值不同}查找:new EntityReference(object ...

  8. 参加:白帽子活动-赠三星(SAMSUNG) PRO....

    参加:白帽子活动-—赠三星(SAMSUNG) PRO.... Everybody~小i在这里提前祝大家国庆假期愉快,咱们期待已久的国庆活动终于开始拉,下面进入正题,恩,很正的题! 活动地址:http: ...

  9. Android-Application

    1:Application是什么? Application和Activity,Service一样,是android框架的一个系统组件,当android程序启动时系统会创建一个 application对 ...

  10. C语言异常与断言接口与实现

    程序中通常会出现三种错误:用户错误.运行期错误以及异常 欢迎关注我的个人博客:www.wuyudong.com, 更多精彩文章与您分享 标准库函数setjmp和longjmp 在C语言中,标准库函数s ...