Блог пользователя Code_sprinter

Автор Code_sprinter, история, 6 лет назад, По-английски

hello everyone,

Recently i came across this problem http://www.spoj.com/problems/DQUERY/en/. I solved the problem using Mo's algo and BST which is basically offline solution. Is there a way I can solve for each query online (and similarly for trees that involves queries on the subtree of its nodes)? If yes, please help me out.

Thanks in advance!!

  • Проголосовать: нравится
  • -24
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Code_sprinter (previous revision, new revision, compare).

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

You can use merge Sort Tree to solve this problems. Here you can see sample code of merger sort tree

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится -6 Проголосовать: не нравится

You could use a BIT (segment or fenwick tree) of size 1e6 and update the positions of the a[i] setting that to 1. Then query the range will tell the number of distinct elements.

Edit: But more simple would be to use prefix sums on an array of size 1e6.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

What an interesting coincidence!! Codechef has a very similar problem : https://www.codechef.com/JUNE20A/problems/DIFVAL

Please wait a few days more for contest to get over and you can read the editorial.

Feel free to research on your own till then, but please refrain asking someone about it since other contestants might get solution without working for it.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

You can use persistent data structures like persistent segment tree