Skip to content

Commit 9055a99

Browse files
Add Tutorial for LOJ-1266: Points in Rectangle (en) (#444)
* tutorials of 1266 no problem super easy explanation * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * Update en.md * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * Update en.md * Add files via upload * Update en.md Image Added in same directory * Added everything according to feedback * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * Update 1266/en.md Co-authored-by: Rafid Bin Mostofa <[email protected]> * removing extra break tag * Delete solve.txt --------- Co-authored-by: mahedi hassan rabby <[email protected]> Co-authored-by: Rafid Bin Mostofa <[email protected]>
1 parent 580bfec commit 9055a99

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

1266/en.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# LightOj 1266 - Points in Rectangle
2+
### Tag
3+
Data Structure, Binary Indexed Tree
4+
### Quick links for prerequisites
5+
Binary Indexed Tree:
6+
- https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2
7+
- https://cp-algorithms.com/data_structures/fenwick.html
8+
9+
10+
### Solution
11+
We will maintain a 2-D BIT to solve this problem. For query 0 x y, we will insert a new point (x,y). For the second type of query, we can utilize the 2-D BIT to know how many points there are in the rectangle (1,1) to (x,y).
12+
13+
Let's take a look first testcase:
14+
```
15+
1
16+
4
17+
0 1 1
18+
0 2 6
19+
1 1 1 6 6
20+
1 2 2 5 5
21+
```
22+
- We got two points (1,1) and (2,6), let's plot these points <br>
23+
![first](./first.PNG)
24+
- In the first query, we have to find how many point are inside (1,1) and (6,6). We can easily do that by querying the BIT, the ans is 2.
25+
- Let's come to the second query. Here we have to find the number of point inside (2,2) and (5,5). We will call query function with parameter (5,5). But it will return the number of point inside (1,1) and (5,5). The area is ploted bellow in red-
26+
![second](./second.PNG)
27+
- We took some extra area. Because we have to find from (2,2) to (5,5) not (1,1) to (5,5). However, the final equation of ans is - <br>
28+
`Ans=A-B-C+D` where
29+
* A = Number of points in rectangle (1, 1) to (x2,y2)
30+
* B = Number of points in rectangle (1, 1) to (x1-1,y2-1)
31+
* C = Number of points in rectangle (1, 1) to (x2-1,y1-1)
32+
* D = Number of points in rectangle (1, 1) to (x1-1,y1-1)
33+
let's clarify this equation. First look at the area ploted bellow of **A,B,C,D**.
34+
![third](./third.PNG)
35+
![fourth](./fourth.PNG)
36+
- As D is inside of both B and C. So at the time of substracting B and C , D is subtracted twice . so D is added. Now after calculating
37+
we got our area where no point is located. So ans of the second query is 0. Hope you understood the solution.
38+
39+
### Code
40+
41+
#### C++
42+
```cpp
43+
#include<bits/stdc++.h>
44+
using namespace std;
45+
long long int BIT[1005][1005];
46+
bool vis[1005][1005];
47+
48+
void update(int x, int y , int val)
49+
{
50+
while(x<=1001)
51+
{
52+
int y1=y;
53+
while(y1<=1001)
54+
{
55+
BIT[x][y1]+=val;
56+
y1+=(y1&-y1);
57+
}
58+
x+=(x&-x);
59+
}
60+
}
61+
long long int query(int x, int y)
62+
{
63+
long long int sum=0;
64+
while(x>0)
65+
{
66+
int y1=y;
67+
while(y1>0)
68+
{
69+
sum+=BIT[x][y1];
70+
y1-=(y1&-y1);
71+
}
72+
x-=(x&-x);
73+
}
74+
return sum;
75+
}
76+
void solve()
77+
{
78+
79+
memset(BIT,0,sizeof(BIT)); //initialize with zero
80+
memset(vis,false,sizeof(vis)); // initalize with zero
81+
long long int q,a,b,c,d,x1,y1,x2,y2;
82+
cin>>q;
83+
while(q--)
84+
{
85+
cin>>a;
86+
if(a==0)
87+
{
88+
cin>>a>>b;
89+
a++,b++;
90+
if(!vis[a][b])
91+
{
92+
vis[a][b]=true;
93+
update(a,b,1); //update the index
94+
}
95+
}
96+
else
97+
{
98+
cin>>x1>>y1>>x2>>y2;
99+
x1++,x2++,y1++,y2++;
100+
// find total point inside the given rectangle
101+
long long int ans=query(x2,y2)-query(x2,y1-1)-query(x1-1,y2)+query(x1-1,y1-1);
102+
cout<<ans<<endl;
103+
}
104+
}
105+
return;
106+
}
107+
int32_t main()
108+
{
109+
int tt;
110+
tt = 1;
111+
cin>>tt;
112+
for(int i=1;i<=tt;i++)
113+
{
114+
cout<<"Case "<<i<<":"<<endl;
115+
solve();
116+
}
117+
return 0;
118+
}
119+
```

1266/first.PNG

6.18 KB
Loading

1266/fourth.PNG

8.08 KB
Loading

1266/second.PNG

7.25 KB
Loading

1266/third.PNG

7.93 KB
Loading

0 commit comments

Comments
 (0)