-
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 1 commit
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 | ||
[Problem Statement](http://lightoj.com/volume_showproblem.php?problem=1266) | ||
### Summary | ||
You will be given two type of query and the range of query is 1<=q<=3000. Two type of query is - | ||
1. 0 x y that means add a point in (x,y) co-ordinate if it doesn't already exist. | ||
2. 1 x1 y1 x2 y2 - you have to find the number of point exist in this range where (x1,y1) is lower left corner of rectangle and (x2,y2) is upper right corner. | ||
|
||
### Tag | ||
Data Structure , Binary index tree | ||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### Quick links for prerequisites | ||
Binray index tree: | ||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2 | ||
https://cp-algorithms.com/data_structures/fenwick.html | ||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### Observation | ||
In basic BIT (binary index tree) we deal with one dimentional array . The difference is here we have to deal with both X and Y value. so if we take two dimentional array for saving segmented sum . Here BIT[X][Y] denotes for each x index we have another array for Y value. | ||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### Solution | ||
1. for first type of query we will just update the point in (x,y) index with value 1 which mainly represent number of point in that index. As it is told only one point exist in one point so value used for update always 1. But if that point is already updated then we don't have to update it. | ||
2. The come to second type of query . Here two index is given . Now how to use our query function?? lets look at the image bellow- | ||
 | ||
|
||
from above image we have to find number of point in green colored rectangle. <br> | ||
so firstly query(x2,y2)-query(x2,y1-1) denotes the rectengle bellow with colored yellow shown bellow | ||
|
||
 | ||
but we have to avoid the left part which is marked in image bellow with colored red. | ||
 | ||
so final ans <br> | ||
=query(x2,y2)-query(x2,y1-1)-{ query(x1-1,y2)-query(x1-1,y1-1)} <br> | ||
=query(x2,y2)-query(x2,y1-1)-query(x1-1,y2)+query(x1-1,y1-1) <br> | ||
then we can find easily number of point from BIT array | ||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Code | ||
|
||
#### C++ | ||
```cpp | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long int | ||
const ll N=2e6+5; | ||
const ll mod=1000000007; | ||
ll BIT[1005][1005]; | ||
bool vis[1005][1005]; | ||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
} | ||
ll query(int x, int y) | ||
{ | ||
ll 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 | ||
ll 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++; | ||
ll ans=query(x2,y2)-query(x2,y1-1)-query(x1-1,y2)+query(x1-1,y1-1); /* find total point inside the given rectengle */ | ||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cout<<ans<<endl; | ||
} | ||
} | ||
|
||
|
||
return; | ||
} | ||
int32_t main() | ||
{ | ||
|
||
|
||
|
||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int tt; | ||
tt = 1; | ||
cin>>tt; | ||
for(int i=1;i<=tt;i++) | ||
{ | ||
cout<<"Case "<<i<<":"<<endl; | ||
solve(); | ||
} | ||
return 0; | ||
} | ||
``` | ||
##### Happy_coding (inshallah! One day you will be rewarded for your hardwork ) | ||
Rabby033 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.