How can I solve this problem UVA Problem 11297 — Census using Binary Indexed Tree ?
Problem Description in Short : Given a 2D grid (500x500 at most), I need to process 2 types of query.
- Change the value of grid[x][y] by Val
- Output the maximum and minimum number of a sub rectangle of grid (x1,y1) to (x2,y2)
( considering every input is valid ) How can I solve this problem using Binary Indexed Tree? Also it will be a great hand if you help me to understand how Range Minimum or Maximum Query can be done by BIT.
Thanks in Advance :)
I don't think we can solve this problem with Binary Indexed Tree (Fenwick Tree). We can use Fenwick Tree only in cases:
However, we can solve this problem with SegmentTree 2D.
Thank you I_love_tigersugar for your reply. However I found this problem under BIT category in this site (29 th problem), So I was wondering if it can be solved by BIT..
Then I searched a little and found two blogs that has code that can do RMQ using BIT ( at least they say so). Although I could not understand them. Right now I am not home and with a little access to internet, but I will edit this comment and provide links of those blogs no sooner I get back home. :) Edit Blog Links
Well... Actually it seems that you can use BIT for this problem. But you need a bit special BIT which allows you to calculate not invertible function. You can read more about it here (Russian). But you still can only maximize/minimize but not change...
Ok, I_love_tigersugar I'm back in home again. I found these two blogs interesting,
Please help me to understand how they used BIT to calculate RMQ
why do you only have one sequence? https://ioinformatics.org/journal/v9_2015_39_44.pdf
I copied goo.gl_SsAhv code from here.
No. RMQ is commutative, thus N can be any number.
that n in maxn because it makes a ballaced tree
keyvankhademi Can you Please elaborate your answer.. And help me to understand the idea of the code above.
The things I do not understand,
const int N = 1 << 17; // has to be power of 2
Why we set like it, why not setconst int N = 1 << 18;
orconst int N = 1 << 19;
orconst int N = 100000;
At
void SetMin(int pos, int x)
we are iterating likefor (int i = pos + N; i; i >>= 1)
why we add N with pos, what's wrong if we do not add N with pos and just iterate likefor (int i = pos; i; i >>= 1)
Also please describe the logic in this code portion
Thanks in Advance :)
what balance you talking about? What will be much more complicated? For example, there is my submission 7938499 with such segment tree and N = 1000010 — not a power of 2.
Power of 2 has no advantages. Only memory waste.
You can count all vertices that aren't in the last layer of tree and call it
R
. After that to have access to positioni
you need just to returntree[i + R]
.Thanks keyvankhademi.. Big Time :)
your code takes R as R + 1. I used your code and when passing the parameter in GetMin I had to give R as R + 1. can you tell why??
i know that it's an old post , but today i just read a paper about RMQ with BIT so i decided to share it to everyone who are interested about this topic
http://ioinformatics.org/oi/pdf/v9_2015_39_44.pdf