| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | jiangly | 3810 |
| 2 | Benq | 3676 |
| 3 | Kevin114514 | 3655 |
| 4 | maroonrk | 3463 |
| 5 | strapple | 3447 |
| 6 | Um_nik | 3387 |
| 7 | heuristica | 3322 |
| 8 | turmax | 3317 |
| 9 | tourist | 3307 |
| 10 | jiangbowen | 3291 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 156 |
| 2 | nik_exists | 150 |
| 2 | maspy | 150 |
| 4 | Um_nik | 142 |
| 5 | Errichto | 139 |
| 6 | adamant | 137 |
| 7 | AmShZ | 135 |
| 8 | BledDest | 132 |
| 8 | maroonrk | 132 |
| 10 | qwexd | 129 |
Hi everyone,
Today i've released a video about a crazy problem:

Its a problem like you've never seen before. I've encountered it back in 2018 and i was able to solve it only recently.
Link to the problem: link
Let me know the craziest problems that you ever saw!
Fenwick tree is a dynamic prefix sums array. You can:
Fenwick tree is a single array. Each element $$$i$$$ has sum of elements on some range. Each range ends in its element, but they have different sizes.
Here's how the segments look for each element:

See a pattern? Sizes of ranges go like $$$1, 2, 1, 4, 1, 2, 1, 8, \dots$$$
How do we get size of range for element $$$i$$$?
Look at binary representation of index $$$i$$$:
We can count the number of consecutive ones on the right side (least significant bits). Say this number is $$$k$$$. Then the size of the range is $$$2^k$$$.
So each element in fenwick array will keep the sum of its segment. Initially the array is filled with zeroes.
What happens when you add $$$x$$$ to element at position $$$i$$$? You need to increment $$${tre}[i]$$$ by $$$x$$$. But also, you need to update other ranges that contain this position. How do we find them?

Look at binary representation of $$$i$$$ again. What is the next index, where the range contains this element? Its the same index, but with the first $$$0$$$ from the right (least significant) replaced with a $$$1$$$:
To find such index we'll use some binary magic. Try to figure this out for yourself.
What happens when you add $$$1$$$ to your index $$$i$$$ in binary? All consecutive ones turn into zeroes and the first zero becomes a one:
So if we take bitwise OR of the two, we'll get the desired index.
inline void add(int i, int x)
{
for(; i < MAXN; i |= (i+1))
{
tre[i] += x;
}
}
To get prefix sum of first $$$i$$$ elements, we'll start at position $$$i-1$$$. We'll take the sum on the range for that index, and jump to the next index.

To do that, we simply subtract the range size. But, there is a cleaner solution, using bitwise magic again.
Look at binary. If length of the consecutive segment of ones is $$$k$$$, what does subtracting $$$2^k$$$ look like? The segment of ones itself has value $$$1 + 2 + \dots = 2^k-1$$$. So we can turn all those ones into zeroes, and then simply subtract a one.
How do we remove the segment of ones? Same way we did before. We'll take $$$i$$$ and $$$i+1$$$, but instead of ORing them, we will AND them.
int sum(int r)
{
r--;
int res = 0;
for(; r >= 0; r = (r & (r+1))-1)
{
res += tre[r];
}
return res;
}
As i said at the start, time complexity for both operations is $$$O(\log N)$$$. Why?
const int MAXN = 200005;
struct fenwick
{
int tre[MAXN];
fenwick() {}
inline void clear(int n)
{
for(int i = 0; i < n; i++)
{
tre[i] = 0;
}
}
inline void add(int i, int x)
{
for(; i < MAXN; i |= (i+1))
{
tre[i] += x;
}
}
inline int sum(int r)
{
r--;
int res = 0;
for(; r >= 0; r = (r & (r+1))-1)
{
res += tre[r];
}
return res;
}
};
Fenwick tree is used to keep a dynamic prefix sums array with every operation being $$$O(\log N)$$$.
Yes, but you can't really set individual values, you can only update them (i.e. min= or max= )
Segment trees are far more versatile, but the implementation for fenwick tree is much simpler and faster.
Sure you can:

Hello everyone!
Me and DJeniUp are back with another programming video!

https://www.youtube.com/watch?v=dv_dGwrazuE
This video is about a tricky problem that i've encountered at my first ever programming competition. At first glance it seems like a simple strategy will cut it, but as you submit more and more solution, you start to realize it's not that simple...
This video is about interval dp, and you can solve the problem for yourself: https://codeforces.me/gym/106465/problem/A
PS: Thanks everybody for 1k subscribers!
Me and DJeniUp recently started a YouTube channel dedicated to competitive programming.
Today, we released a new video! It's about Dynamic Programming:

Link: https://www.youtube.com/watch?v=eNjDWXugJCo
Feel free to leave your feedback and suggestions under the comments, we appreciate those!
Recently, me and DJeniUp started a YouTube channel, dedicated to algorithms, math and competetive programming in general.
Today we released a new video on a topic of Topological Sort!

Link: https://www.youtube.com/watch?v=EJoKJiod0KQ
Feel free to leave your suggestions in the comments under the video or this blog! We would like to explore more competetive programming topics, from basic to complicated ones
Here's the link to the whiteboard that you see in the video: https://miro.com/app/board/uXjVGcpqFXU=/
All codes can be found there or on our github: https://github.com/Lincatoria/VideoMaterials
Centroid Decomposition is kinda like Divide & Conquer on arrays (merge sort type divide&connquer) but for trees. Ever thought about it that way? Like HLD is a segment tree but for trees, what do you think? nvm just had a fun thought yesterday, never looked at it from this angle, cool (i attached some imagery) 
| Название |
|---|


