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 ... 
随机推荐
- Machine Learning读书会,面试&算法讲座,算法公开课,创业活动,算法班集锦
			Machine Learning读书会,面试&算法讲座,算法公开课,创业活动,算法班集锦 近期活动: 2014年9月3日,第8次西安面试&算法讲座视频 + PPT 的下载地址:http ... 
- 学习java的第十六天
			一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ... 
- 基于python win32setpixel api 实现计算机图形学相关操作
			最近读研期间上了计算机可视化的课,老师也对计算机图形学的实现布置了相关的作业.虽然我没有系统地学过图形可视化的课,但是我之前逆向过一些游戏引擎,除了保护驱动之外,因为要做透视,接触过一些计算机图形学的 ... 
- 【分布式】Zookeeper伪集群安装部署
			zookeeper:伪集群安装部署 只有一台linux主机,但却想要模拟搭建一套zookeeper集群的环境.可以使用伪集群模式来搭建.伪集群模式本质上就是在一个linux操作系统里面启动多个zook ... 
- mysql_取分组后的前几行值
			--方法一: select a.id,a.SName,a.ClsNo,a.Score from Table1 a left join Table1 b on a.ClsNo=b.ClsNo and a ... 
- Linux基础命令---mysqlshow显示数据库
			mysqlshow mysqlshow是一个客户端的程序,它可以显示数据库的信息.表信息.字段信息. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ... 
- 【Linux】【Services】【KVM】virsh命令详解
			1. virsh的常用命令 help:获取帮助 virsh help KEYWORD list:列出域 dumpxml:导出指定域的xml格式的配置文件: create:创建并启动域: define: ... 
- 【Java】【设计模式】单例设计模式
			思想: 为了避免其他程序过多建立该类对象,先禁止其他程序建立该类对象 为了让其他程序可以访问到该类对象,只好在本类中自定义一个对象 为了方便其他程序对自定义对象的访问,可以对外提供一些访问方式 代码体 ... 
- 静态类中不可以使用$this
			//静态方法中不能使用$this,静态方法调用其他方法可以用static\self\类名来代替class ceshi{ static public function aa(){ static::bb( ... 
- 为什么要用urlencode()函数进行url编码
			URLEncode就是将URL中特殊部分进行编码.URLDecoder就是对特殊部分进行解码. 因为当字符串数据以url的形式传递给web服务器时,字符串中是不允许出现空格和特殊字符的 譬如:你要传的 ... 
