Skip to content

Latest commit

 

History

History
18 lines (11 loc) · 769 Bytes

non-trivial-examples.md

File metadata and controls

18 lines (11 loc) · 769 Bytes

Some non-trivial ideas for RMQs and RSQs

  1. https://codeforces.com/contest/220/problem/B

Problem: Given an array and a range [l...r]. How many integers x is in the range such that frequency of x is x?

Let's say you are in index r. The difference array for any integer x to queries ending at r will look like below:

..................x(x+2)..x(x+1)..x(x).....x......x(2)......x(1)..................r
.................... 0.............-1 ............1..............................................r

Updates:

- 0 -> 1 for xth index = +1 
- 1 -> -1 for x+1th index = -2 
- -1 -> 0 for x+2th index = +1 

Use fenwick tree or segment tree for the updates. Sum of the difference arrays l...r will give the answer for l...r.