题目链接:

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. linux下firefox手工安装flash插件

    1. 前往adobe官网,下载flash安装包.下载.tar.gz安装包即可.2. 解压安装包,得到libflashplayer.so文件3. 新建文件夹,~/.mozilla/plugins4. 拷 ...

  2. jQuery淡入淡出效果轮播图

    用JavaScript做了平滑切换的焦点轮播图之后,用jQuery写了个简单的淡入淡出的轮播图,代码没有做优化,html结构稍微有一些调整,图片部分用ul替换了之前用的div. html结构如下: & ...

  3. andriod 动态创建控件

    Button btNext=(Button)findViewById(R.id.next); btNext.setOnClickListener(new Button.OnClickListener( ...

  4. “Stamping” PDF Files Downloaded from SharePoint 2010

    http://blog.falchionconsulting.com/index.php/2012/03/stamping-pdf-files-downloaded-from-sharepoint-2 ...

  5. java多线程系列2-线程控制

    前面的文章已经介绍了有关线程的调度,接下来介绍如何使用方法对线程进行控制 1.线程休眠 public static void sleep(long millis) /* * 线程休眠 * public ...

  6. IOS MenuController的部分操作

    这里我们要实现的将是选择按钮的自定义 综合上一节的随笔,这里给出效果图. ViewController.m // // ViewController.m // CX-MenuController // ...

  7. IOS UICollectionView基础+UICollectionViewFlowLayout基础

    UICollectionView在众多控件中也算是比较常用的了,像淘宝在浏览宝贝时采用的就是UICollectionView,对于UICollectionView->UICollectionVi ...

  8. 全球最低功耗蓝牙单芯片DA14580的软件体系 -层次架构和BLE消息事件处理过程

    在作者之前发表的<全球最低功耗蓝牙单芯片DA14580的系统架构和应用开发框架分析>.<全球最低功耗蓝牙单芯片DA14580的硬件架构和低功耗>.<全球最低功耗蓝牙单芯片 ...

  9. iOS 秒数转换成时间,时,分,秒

    //转换成时分秒 - (NSString *)timeFormatted:(int)totalSeconds{ int seconds = totalSeconds % 60;     int min ...

  10. DP大作战——多重背包

    题目描述 在之前的上机中,零崎已经出过了01背包和完全背包,也介绍了使用-1初始化容量限定背包必须装满这种小技巧,接下来的背包问题相对有些难度,可以说是01背包和完全背包的进阶问题. 多重背包:物品可 ...