1266 - Points in Rectangle
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
As the name says, this problem is about finding the number of points in a rectangle whose sides are parallel to axis. All the points and rectangles consist of 2D Cartesian co-ordinates. A point that lies in the boundary of a rectangle is considered inside.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing an integer q (1 ≤ q ≤ 30000) denoting the number of queries. Each query is either one of the following:
1) 0 x y, meaning that you have got a new point whose co-ordinate is (x, y). But the restriction is that, if a point (x, y) is already listed, then this query has no effect.
2) 1 x1 y1 x2 y2 meaning that you are given a rectangle whose lower left co-ordinate is (x1, y1) and upper-right corner is (x2, y2); your task is to find the number of points, given so far, that lie inside this rectangle. You can assume that (x1 < x2, y1 < y2).
You can assume that the values of the co-ordinates lie between 0 and 1000 (inclusive).
Output
For each case, print the case number in a line first. Then for each query type (2), you have to answer the number of points that lie inside that rectangle. Print each of the results in separated lines.
Sample Input |
Output for Sample Input |
|
1 9 0 1 1 0 2 6 1 1 1 6 6 1 2 2 5 5 0 5 5 1 0 0 6 5 0 3 3 0 2 6 1 2 1 10 10 |
Case 1: 2 0 2 3 |
Note
Dataset is huge, use faster I/O methods.
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<queue>
6 #include<string.h>
7 using namespace std;
8 int bit[1005][1005];
9 bool flag[1005][1005];
10 int lowbit(int x)
11 {
12 return x&(-x);
13 }
14 void add(int x1,int y1)
15 {
16 int i,j;
17 for(i = x1; i <= 1001; i+=lowbit(i))
18 for(j = y1; j <= 1001; j+=lowbit(j))
19 {
20 bit[i][j]++;
21 }
22 }
23 int ask(int x1,int y1)
24 {
25 int i,j;
26 int sum = 0;
27 for(i = x1; i > 0; i-=lowbit(i))
28 for(j = y1; j > 0; j-=lowbit(j))
29 {
30 sum+=bit[i][j];
31 }
32 return sum;
33 }
34 int main(void)
35 {
36 int T;
37 scanf("%d",&T);
38 int __ca = 0,q;
39 while(T--)
40 {
41 __ca++;
42 memset(bit,0,sizeof(bit));
43 memset(flag,0,sizeof(flag));
44 scanf("%d",&q);
45 printf("Case %d:\n",__ca);
46 int val ;
47 int x,y,x1,y1;
48 while(q--)
49 {
50 scanf("%d",&val);
51 if(!val)
52 {
53 scanf("%d %d",&x,&y);
54 x+=1;
55 y+=1;
56 if(!flag[x][y])
57 {
58 add(x,y);
59 flag[x][y]=true;
60 }
61 }
62 else
63 {
64 scanf("%d %d %d %d",&x,&y,&x1,&y1);
65 x++;y++;x1++;y1++;
66 int sum = ask(x1,y1);
67 sum += ask(x-1,y-1);
68 sum -= ask(x-1,y1);
69 sum -= ask(x1,y-1);
70 printf("%d\n",sum);
71 }
72 }
73 }
74 return 0;
75 }
76
复杂度:n*log(n)^2;
1266 - Points in Rectangle的更多相关文章
- Light OJ 1266 - Points in Rectangle
题目 Link 就是查询矩形内有多少个点. 分析 二维树状数组维护就好了,. Code #include <bits/stdc++.h> const int maxn = 1000 + 1 ...
- D. Points in rectangle
D. Points in rectangle 单点时限: 2.0 sec 内存限制: 512 MB 在二维平面中有一个矩形,它的四个坐标点分别为(0,a),(a,0),(n,n−a),(n−a,n). ...
- C++ 基于凸包的Delaunay三角网生成算法
Delaunay三角网,写了用半天,调试BUG用了2天……醉了. 基本思路比较简单,但效率并不是很快. 1. 先生成一个凸包: 2. 只考虑凸包上的点,将凸包环切,生成一个三角网,暂时不考虑Delau ...
- 人脸识别引擎SeetaFaceEngine中Alignment模块使用的测试代码
人脸识别引擎SeetaFaceEngine中Alignment模块用于检测人脸关键点,包括5个点,两个眼的中心.鼻尖.两个嘴角,以下是测试代码: int test_alignment() { std: ...
- Opencv 最小外接矩形合并拼接
前一篇画出了最小外接矩形,但是有时候画出来的矩形由于中间像素干扰或者是其他原因矩形框并不是真正想要的 如图1是一个信号的雨图,被矩形框分割成了多个小框: 需要合并矩形框达到的效果: 主要思想: 扫描两 ...
- HPU暑期集训积分赛2
A. 再战斐波那契 单点时限: 1.0 sec 内存限制: 512 MB 小z 学会了斐波那契和 gcd 后,老师又给他出了个难题,求第N个和第M个斐波那契数的最大公约数,这可难倒了小z ,不过在小z ...
- pdf 中内容的坐标系
PDF Page Coordinates (page size, field placement, etc.) AcroForm, Basics, Automation Page coordinate ...
- leetcode 1266. Minimum Time Visiting All Points
On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to find th ...
- 【leetcode】1266. Minimum Time Visiting All Points
题目如下: On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to f ...
随机推荐
- 笔记Mysql(1)
客户端的登录命令 查看版本 查看设置(数据库的基本设置信息) 查看时间 查看链接数 查看超时的关键字 创建数据库 查看已有数据库 进入数据库,查询链接到的数据库 查询数据目录 创建表 查看表
- Ubuntu16.04安装 2.8.5版本Ansible
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py && python get-pip.py pip install --upgrade ...
- C/C++ Qt 数据库与ComBox多级联动
Qt中的SQL数据库组件可以与ComBox组件形成多级联动效果,在日常开发中多级联动效果应用非常广泛,例如当我们选择指定用户时,我们让其在另一个ComBox组件中列举出该用户所维护的主机列表,又或者当 ...
- HTML5 基础内容(元素/属性/格式化)
HTML基础 1.HTML元素 1.1 元素指的是开始标签到结束标签的所有代码. 1.2 元素的内容是开始标签与结束标签之间的内容. 1.3大多数HTML元素可用有属性. 1.4标签可以拥有属性为元素 ...
- JavaScript 链表
------------恢复内容开始------------ 背景 数组并不总是组织数据的最佳数据结构,原因如下.在很多编程语言中,数组的长度是固定的,所以当数组已被数据填满时,再要加入新的元素就会非 ...
- TCP中的TIME_WAIT状态
TIME_WAIT的存在有两大理由 1.可靠地实现TCP全双工连接的终止 2.允许老的可重复分节在网络中消失. 对于理由1,我们知道TCP结束需要四次挥手,若最后一次的客户端的挥手ACK丢失(假设是客 ...
- 视频框架 Vitamio使用
转自http://blog.csdn.net/u010181592/article/category/5893483 1.在https://github.com/yixia/VitamioBundle ...
- docker创建tomcat容器无法正常访问
记一次创建tomcat docker容器后访问是404,进入到tomcat docker容器后发现webapps是空的 1.挂载 docker run -v localConfigFile:/cont ...
- 监控网站是否异常的shell脚本
本节内容:shell脚本监控网站是否异常,如有异常就自动发邮件通知管理员. 脚本检测流程,如下:1,检查网站返回的http_code是否等于200,如不是200视为异常.2,检查网站的访问时间,超过M ...
- FastDFS分布式文件系统及源码解析
记录一次本人学习FastDFS-分布式文件系统的学习过程,希望能帮助到有需要的人. 首选得对此技术有个大概的了解,可以参考 https://www.cnblogs.com/centos2017/p/7 ...