-
Notifications
You must be signed in to change notification settings - Fork 156
Add Tutorial for LOJ-1266: Points in Rectangle (en) #444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
16c86f1
tutorials of 1266 no problem
Rabby033 24bf776
Update 1266/en.md
Rabby033 86dfde8
Update en.md
Rabby033 bf5e1b7
Update 1266/en.md
Rabby033 cd71a01
Update 1266/en.md
Rabby033 100ef03
Update en.md
Rabby033 d27bbdb
Add files via upload
Rabby033 ceb0f34
Update en.md
Rabby033 e92cb7f
Merge branch 'lightoj-dev:main' into main-1
Rabby033 1b1df7a
Added everything according to feedback
Rabby033 f75e5fb
Update 1266/en.md
Rabby033 cccdb4e
Update 1266/en.md
Rabby033 dc87705
Update 1266/en.md
Rabby033 a58a1d0
Update 1266/en.md
Rabby033 bcf8e2f
Update 1266/en.md
Rabby033 3618cf9
Update 1266/en.md
Rabby033 bf27759
removing extra break tag
Rabby033 b278473
Delete solve.txt
Rabby033 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# LightOj 1266 - Points in Rectangle | ||
### Tag | ||
Data Structure, Binary Indexed Tree | ||
### Quick links for prerequisites | ||
Binary Indexed Tree: | ||
- https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2 | ||
- https://cp-algorithms.com/data_structures/fenwick.html | ||
|
||
|
||
### Solution | ||
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). | ||
|
||
Let's take a look first testcase: | ||
``` | ||
1 | ||
4 | ||
0 1 1 | ||
0 2 6 | ||
1 1 1 6 6 | ||
1 2 2 5 5 | ||
``` | ||
- We got two points (1,1) and (2,6), let's plot these points <br> | ||
 | ||
- 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. | ||
- 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- | ||
 | ||
- 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> | ||
`Ans=A-B-C+D` where | ||
* A = Number of points in rectangle (1, 1) to (x2,y2) | ||
* B = Number of points in rectangle (1, 1) to (x1-1,y2-1) | ||
* C = Number of points in rectangle (1, 1) to (x2-1,y1-1) | ||
* D = Number of points in rectangle (1, 1) to (x1-1,y1-1) | ||
let's clarify this equation. First look at the area ploted bellow of **A,B,C,D**. | ||
 | ||
 | ||
- 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 | ||
we got our area where no point is located. So ans of the second query is 0. Hope you understood the solution. | ||
|
||
### Code | ||
|
||
#### C++ | ||
```cpp | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
long long int BIT[1005][1005]; | ||
bool vis[1005][1005]; | ||
|
||
void update(int x, int y , int val) | ||
{ | ||
while(x<=1001) | ||
{ | ||
int y1=y; | ||
while(y1<=1001) | ||
{ | ||
BIT[x][y1]+=val; | ||
y1+=(y1&-y1); | ||
} | ||
x+=(x&-x); | ||
} | ||
} | ||
long long int query(int x, int y) | ||
{ | ||
long long int sum=0; | ||
while(x>0) | ||
{ | ||
int y1=y; | ||
while(y1>0) | ||
{ | ||
sum+=BIT[x][y1]; | ||
y1-=(y1&-y1); | ||
} | ||
x-=(x&-x); | ||
} | ||
return sum; | ||
} | ||
void solve() | ||
{ | ||
|
||
memset(BIT,0,sizeof(BIT)); //initialize with zero | ||
memset(vis,false,sizeof(vis)); // initalize with zero | ||
long long int q,a,b,c,d,x1,y1,x2,y2; | ||
cin>>q; | ||
while(q--) | ||
{ | ||
cin>>a; | ||
if(a==0) | ||
{ | ||
cin>>a>>b; | ||
a++,b++; | ||
if(!vis[a][b]) | ||
{ | ||
vis[a][b]=true; | ||
update(a,b,1); //update the index | ||
} | ||
} | ||
else | ||
{ | ||
cin>>x1>>y1>>x2>>y2; | ||
x1++,x2++,y1++,y2++; | ||
// find total point inside the given rectangle | ||
long long int ans=query(x2,y2)-query(x2,y1-1)-query(x1-1,y2)+query(x1-1,y1-1); | ||
cout<<ans<<endl; | ||
} | ||
} | ||
return; | ||
} | ||
int32_t main() | ||
{ | ||
int tt; | ||
tt = 1; | ||
cin>>tt; | ||
for(int i=1;i<=tt;i++) | ||
{ | ||
cout<<"Case "<<i<<":"<<endl; | ||
solve(); | ||
} | ||
return 0; | ||
} | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.