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 ...
随机推荐
- Oracle-distinct()用法、count(distinct( 字段A || 字段B))是什么意思?distinct多个字段
0.distinct用法 在oracle中distinct的使用主要是在查询中去除重复出现的数据 直接在字段前加distinct关键字即可,如:select distinct 名字 from tabl ...
- 质量体系建设之路---可视化的MockServer
一. 背景 福禄网络作为一家数字权益商品及服务提供商,覆盖了我们衣食住行的各种生活场景的权益内容,对接了如支付宝.京东.银行APP各种渠道,如何能够快速的响应渠道需求,提供稳定的接口服务,这就要求我们 ...
- 生产调优3 HDFS-多目录配置
目录 HDFS-多目录配置 NameNode多目录配置 1.修改hdfs-site.xml 2.格式化NameNode DataNode多目录配置(重要) 1.修改hdfs-site.xml 2.测试 ...
- Could not get a resource from the pool
redis报错Could not get a resource from the pool情况是:1.可以连接redis2.可以keys *查看数据,但是发现key少了好多(其实原因就是大量的key过 ...
- express系列(1)概述
在 Node.js 出现之前,前后端的开发必须使用不同的语言进行.为此你需要学习多种的语言和框架.有了 Node.js 之后,你就可以使用一门语言在前后端开发中自由切换,这是最吸引人的地方. 什么是 ...
- Oracle中的DBMS_LOCK包的使用
一.DBMS_LOCK相关知识介绍 锁模式: 名字 描述 数据类型 值 nl_mode Null INTEGER 1 ss_mode Sub Shared: used on an aggregate ...
- ps精修
1.磨皮方法: a,, 添加高斯模糊后,按住alt键新建图层蒙版,设置前景色为白色,用画笔在脸上雀斑的位置涂抹,注意脸轮廓位置不要涂抹.最后添加曲线提亮 b. 添加蒙尘和划痕后,后面上面的一样
- java中super的几种用法,与this的区别
1. 子类的构造函数如果要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base"); ...
- Initialization of data members
In C++, class variables are initialized in the same order as they appear in the class declaration. C ...
- my43_mysql内存相关概念
相关参数 read_buffer_size https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_re ...