Loserinlife's blog

By Loserinlife, history, 2 weeks ago, In English

A function F(l, r) is defined as follow:

F(l, r) = 0 if l > r

else m is the index of the largest element from l to r.

F(l, r) = r — l + 1 + F(l, m — 1) + F(m + 1, r)

Given a permutation of length N and Q queries (l, r), calculate F(l, r) for each queries.

N <= 1e6, Q <= 1e6

Thanks!

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

»
2 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Wasn't this a codeforces problem? Btw I want to say that while typing this comment I got dejavu

»
2 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Wow, really cool problem. Source?

Solution
»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Observing the form of this contribution, from a different perspective, it is actually the sum of the number of elements in the left and right monotonic stacks for each number in [l, r]. Taking the inquiry offline is a basic scanning line problem that can achieve O((n+q)logn).

»
2 weeks ago, # |
  Vote: I like it +8 Vote: I do not like it

CF1117G