bhikkhu's blog

By bhikkhu, 7 months ago, In English

I have polished my implementation after ten long days of refactoring and minimizing the number of variables required to keep track of each action. I added hardcore random numbers tests to tally results against brute force verifier.

I have tested it against a dozen problems. It's a continuous process.

Please just run the program it will automatically test against the brute force algorithm for range update and queries.

The code should be extremely easy to understand from what it was ten days ago. The rest of logic is just propagation of lazy values from top to the bottom of the intervals.

Solution for below CSES

https://pastebin.com/Tnr0f5PF

https://cses.fi/alon/task/1651

Fenwick tree with lazy propagation

Hacker earth hard https://www.hackerearth.com/problem/algorithm/range-update-range-max-queries

Solution for the above hacker earth hard problem with generic combine methods

https://pastebin.com/CYj1UHAd

SPOJ GSS3

https://www.spoj.com/problems/GSS3/

https://pastebin.com/GWzBsEYr

CSES range update range sums hard

https://cses.fi/problemset/task/1735/

https://pastebin.com/3zrwTsSm

this should be much more readable as its just collecting intervals, propagating lazy values to them and then going all the way to the root to update impacted parents.

I will keep polishing. I have many ideas to take it to the next level e.g hashing, book keeping cleanup, macro hacks etc. YMMV

  • Vote: I like it
  • +10
  • Vote: I do not like it

»
7 months ago, hide # |
Rev. 4  
Vote: I like it 0 Vote: I do not like it

https://www.spoj.com/problems/GSS1/

SPOJ GSS1 can you answer these queries

Classic segment tree problem solved using Fenwick tree.

Please ignore the fast int code. Performance comparable to segment tree.

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The basic idea is to cover a range [left, right] we need at max log square powers of two.

Keep removing all powers of two from right until we reach left value. If we cant remove a power we can always decrease by 1.

If mergeing two consecutive segment is constant time. Whole range only take log square.

»
7 months ago, hide # |
Rev. 4  
Vote: I like it 0 Vote: I do not like it

I visited sereja and brackets after ten years.

It's running much faster than segment tree.

https://mirror.codeforces.com/problemset/problem/380/C

sereja and brackets Fenwick

As always as the whole picture becomes clearer, the code is always getting shorter lol.

Trying to get nails for my new hammer. Let's keep hammering.