AminAnvari's blog

By AminAnvari, 9 years ago, In English

Hi :)
These are some segment tree problems on codeforces. Ofcourse it is not complete and I hope we will complete it with your help. And great thank to NikaraBika for helping me.

UPD : more Segment Tree

Classic :
339D - Ксюша и битовые операции
356A - Рыцарский турнир
459D - Задача Пашмака и Пармиды
61E - Противник слаб
380C - Сережа и скобочки
474F - Муравьиная колония
292E - Копирование данных
501D - Миша и сложение перестановок
220E - Маленький Слоник, а также инверсии
338E - Оптимизировать!
19D - Точки
351D - Джефф и удаление периодов
515E - Drazil и парк
540E - Бесконечные инверсии
609F - Лягушки и комары
594D - REQ
455E - Функция

Lazy Propagating:
52C - Циклические RMQ
145E - Счастливые запросы
558E - Простое задание
240F - TorCoder
446C - DZY любит числа Фибоначчи
115E - Гонки в Линейном Королевстве
438D - Ребенок и последовательность
121E - Счастливый массив
610E - Перестановки алфавита
580E - Кефа и часы

Segment tree with Vector:
369E - Валера и запросы
610D - Вика и отрезки

Offline Query:
301D - Ярослав и делители
500E - Новогоднее домино

Segment Tree & Dp:
474E - Столбы
597C - Подпоследовательности
56E - Принцип домино

Segment Tree & Bits:
482B - Интересный массив
242E - XOR на отрезке

Segment Tree & Tree:
383C - Дерево подвижных сумм
343D - Водяное дерево
173E - Отдыхаем группами
276E - Девочка и задача про дерево
396C - О меняющемся дереве
516D - Drazil и утренняя зарядка
375D - Дерево и запросы

titles are not tag :))

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

| Write comment?
»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Thank you MR anvari

»
9 years ago, # |
  Vote: I like it +6 Vote: I do not like it

omG Tnx AA :D

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This should also help.

»
9 years ago, # |
  Vote: I like it +6 Vote: I do not like it
»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Segment tree on string: Letter Array

»
9 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Hackerrank's advanced level problems on segment trees are nice too. Check them out.

»
9 years ago, # |
  Vote: I like it +16 Vote: I do not like it

I was looking for this everywhere , i think it will be great helpful for me Thank you.

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How come i can only see problem names in russian?

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Umm... right now it can't be seen in english, which I believe most of us need, it would be great to show it like this: [problem code] [english name] / [russian name]

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Now you can see problems in English :))

      • »
        »
        »
        »
        9 years ago, # ^ |
        Rev. 2   Vote: I like it +3 Vote: I do not like it

        Again they are in russian; Why ?

        UPD You fixed that again, but in my comment it is russian yet,how to fix it?

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Is 356A — Knight Tournament a segment tree problem?

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    A little bit late ;-) Yes, it is possible to solve it with a segment tree. But instead of updating a single value and querying for ranges, you have to invert it so that you can modify an interval and get access to each element. Additionally you have to make sure that you insert the fights in the reversed order, otherwise some fights will overwrite others. Here is my implementation: 23198751

  • »
    »
    6 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Thank YOU :)

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This should be updated with the problem links in this post. How can it be done ??

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Wow! This will be so much helpful to many. Thanks for the effort!

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

760E is also a segment tree problem

»
8 years ago, # |
  Vote: I like it -13 Vote: I do not like it

This question(570C) can also be solved using segment tree.Here is my solution.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +20 Vote: I do not like it

    That's overkill. There exists an easier and faster solution for that problem.

»
8 years ago, # |
  Vote: I like it +1 Vote: I do not like it
»
8 years ago, # |
  Vote: I like it +7 Vote: I do not like it

You can also find many Segment Tree problems on A2 Online Judge. They are ranked by their difficulty, and also including many online judges like codeforces, SPOJ, codechef etc.

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Segment tree + DP problem : D. Babaei and Birthday Cake

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

Can anyone help me with this? I don't know how use a segment tree in order to solve the problem. I solved this using a C++ set. Thanks in advance.

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

How to solve 459D using a segment tree? I solved it using Fenwick tree but I could not think of a solution using segment tree.

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

This blog contains 20 Segment tree Problems (Easy, Medium, hard) along with there solutions. Hope it helps someone.

http://mirror.codeforces.com/blog/entry/46602

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

Shouldn't 292E — Copying Data be under the "Lazy Propagation" section?

If not, how to solve this problem using just classic segment tree?

Nice list, by the way.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    I solved it using Lazy Propagation, I couldn't find a way without it, I join the request for a non-lazy-propagation solution.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve Enemy is Weak Problem using segment tree,I'm not able to understand the editorial.

Links Problem Enemy is Weak

Editorial blogpost

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    1 Let's use coordinate compression on a[i]. Then we have all the a[i] in the range (0, N].

    2 Let's create new array b[MAXN].

    b[i] — amount of j what a [j] > a [i] and j < i.

    It can be done with Fenwick or Segment tree.

    for (int i = 1; i <= n; i++){
      b [i] = get (a[i] + 1, n);
      upd(a [i], 1); 
    }
    

    3 Answer will be sum of d [i].

    d[i] — sum of all b [j] what j < i and a [j] > a [i].

    for (int i = 1; i <= n; i++){
      d[i] = get (a[i] + 1, n);
      upd(a [i], b [i]);
    }
    

    Sorry For My English

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

Thanks !! It was very very helpful.

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

Thanks a lot bud. Finally a compilation of segtrees problems in codeforces

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

877E — segment tree on tree

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

Why the code is showing TLE for Xenia and Bit operation. http://mirror.codeforces.com/contest/339/submission/33017272

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

377D - Разработка игры

605D - Настольная игра — segment tree and ...

...

540E - Бесконечные инверсии does not really require segment tree, Fenwick tree is enough.

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

Great effort!!

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

920F - SUM и REPLACE

could solve with segment... :))

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Do you need lazy propagation? That was my idea in-contest but couldn't implement it right.

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

Easy!

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

Can someone explain , how to solve http://mirror.codeforces.com/contest/459/problem/D using segment tree . I tried to solve it using merge sort tree but got TLE . Then , i could solve it using BIT and map .

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I solved it using merge sort tree. Needed a little bit of optimization though.

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

Problem link

Can anyone help me out? I m stuck and not able to find any editorial for it.

»
7 years ago, # |
  Vote: I like it +3 Vote: I do not like it

958C3 - Encryption (hard) ==> dp + segment(bit)

it's a good problem for practice in my opinion :)

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How is https://mirror.codeforces.com/contest/438/problem/D a lazy propagation problem?

»
6 years ago, # |
  Vote: I like it -6 Vote: I do not like it

This problem also can be solved using segment tree: 101061E - Playing with numbers
I have solved it using segment tree.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

877E - Данил и подработка , good problem about segment tree+tree+Lazy Propagating.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Any of those about Persistent Segment Tree?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks! It's really useful.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Thank you AminAnvari! Can someone share a list of MUST SOLVE Dynamic Programming Problems on Codeforces?

»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it

356A — Knight Tournament can be solved using set. No segment tree required

»
6 years ago, # |
  Vote: I like it +2 Vote: I do not like it

1179 C

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

Thank you so much for the set of tasks!

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

DP+segment tree: B. The Bakery

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

thank you so much my segment tree journey started

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone pls give me a bit intuition of using segment tree for 338E — Optimize!

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Has anyone solved 375D — Trees and Queries ? It is the last problem mentioned in the Segment Tree and Tree section?

My approach — I flattened the tree with Euler tour and I couldn't figure out the way to compute the answer with MO's algorithm. How with a change in k, can I get a current query answer from my past queries?? Any suggestion or external references are appreciated

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I've solved this problem using MO's algorithm. First I've transformed the given tree into an array based on traversal order. Two index for every node. One for starting time another for finishing time. Within the segment lies the others vertexes of it's subgraph Then based on query nodes, I've a range for left and right. Then I ordered them in MO's ordering. In the MO's for add and remove function, I used two counter array. One for counting the appearances of the colors. And number of appearances of number of colors

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

brother next time plz upload problems on HLD

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

Much needed problem set. Thanks a lot!

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Thanks a lot. That's really helpfull.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks!!!

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Parenthesis Checking — A good segment tree problem from the recent AtCoder Beginner Contest

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hey! I just wanted to ask how you kept track of the minimum element in the cumulative sum. I saw the tutorial but didn't really get it. Any hints?

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      In case you still haven't solved it, here are few hints:

      Hint 1
      Hint 2
      Hint 3
      Hint 4
      Hint 5
      Hint 6
      Hint 7
      Still not able to solve?

      Have a nice day ahead :)

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    This is the same problem as Sereja and Brackets mentioned above. You just check if max subsequence is equal to (r-l+1)

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

nothing

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I found another problem that uses "Segment Tree on Euler Tour of the tree" to solve it. The problem is 1132G - Жадные подпоследовательности and the tutorial is https://mirror.codeforces.com/blog/entry/65752.

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

Thank you man

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

how to solve ant colony?

  • »
    »
    11 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    you find gcd of [l,r] with sparse table, and count number of occurences of gcd in [l, r] with a simple pos array, and subtract it from n

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

i am learning segment trees i tried Knight Tournament here is my solution 269847713 can anyone help i looked at others solution i cot that set approach but i wanted to know what i am doing wrong in segment trees....

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

Thank you for this!

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

Thanks