Скоро Codeforces Round 1133 (Div. 1, Div. 2)! Регистрация здесь. ×

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

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

Hello there

I'm trying to solve this problem

So i was thinking if i create a segment tree where each node has a segment tree that would do the trick

I've heard about 2D segment tree before but never implemented it

the only way i could think off would definitely get me MLE where i do an array seg[n*2][n*2]

if anybody has any tricks that could save memory or maybe the correct way for creatin a 2D segment tree that would be really appreciated :D

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

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

You can build segment tree, where in each node you have a treap. It will be O(n * logn) memory.

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

Also you can use sqrt-decomposition with Fenwick tree. All what you need is to divide the array into parts of and for every part maintains amount of every number (in fenwick tree). Then every modify query will be processed in O(log(N)), k-query in .

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

If you still want to use 2-D segment tree, you can store in every node Dynamic segment tree.

The key idea is that you don't have to keep all the nodes of segment tree (which is stored in node I mean). You can dynamically create it as soon as you need it.

That is require O(n * log(n) * log(M))

Little example: '[1-4]=0 [1-2] [1] [2] [3-4] [3] [4] ' here is segment tree for 4 nodes. '=0' — indicates the vertex that was created already, and stored the value 0. So if vertex wasn't created means that there is no value in it ==> 0 there. let's make update query: add to +1 to 4-th, then the tree are gonna look like the following:


[1-4]=1 [1-2] [1] [2] [3-4]=1 [3] [4]=1

Let's provide query how much elements >= 2 in our Data structure. In our case is simply sum over segment [2..4]


[1-4]=1 1) go left son, 3) go to right son, 5) return 0 + 1 = 1 [1-2] 2) come to left son, realized that it was not created, so return 0 [1] [2] [3-4]=1 4) return 1 [3] [4]=1

Hope it make sense.