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 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).
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.
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
Dataset is huge, use faster I/O methods.
PROBLEM SETTER: JANE ALAM JAN
思路:二维树状数组;
模板题:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
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;
手机扫一扫
移动阅读更方便
你可能感兴趣的文章