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

Автор Orn0, история, 3 дня назад, По-английски

Hello codeforcers.

I'm learning segment trees, and for now I'm focused on "standard" range query/point update segment trees. I've tried to solve CSES 1144 : Salary Queries, but I keep getting TLE.

I've written a simple segment tree struct, and I'm using coordinate compression. I suppose that I should find a constant factor optimization to pass the time limit. Is there a generic optimization I should include in my implementation to make it faster ?

I guess I could consider using more advanced segment trees variants, but I thought I could stay on a generic ST. Am I wrong ?

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

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

I don't think you are supposed to use segment tree here. Have you tried using Order Statistics Tree? Basically you need a DS that supports finding how many indices below a certain point (like lower_bound). Also, I saw that each node in your segtree is holding info of the sum of its children. I don't see how this info helps solving the problem.

  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I'm indeed using ST where each node holds info for the sum of its children. The leaves represent a frequency array of salaries (compressed to only consider salaries actually used in the queries).

    I haven't tried Order Statistics Tree, I will look into it.

    • »
      »
      »
      3 дня назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      fbrunodr solution can be implemented using PBDS that supports that type of queries. I solved it here for your reference. I am also adding PBDS basic operations for you to look up below:

      1. order_of_key(k): Returns the number of elements strictly less than k — log(N)
      2. find_by_order(index): : Returns an iterator pointing to the element at the given index in the sorted order (0-based indexing) — log(N)
      3. insert && erase as usual — log(N)
      4. lower_bound: Returns an iterator pointing to the first element that is not less than k — log(N)
      5. upper_bound(k): Returns an iterator pointing to the first element that is strictly greater than k — log(N)
      • »
        »
        »
        »
        3 дня назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Yeah, you can also implement a BST yourself or use a Fenwick tree (although you would have to solve offline compressing the values of the salaries)

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

You can use policy based data structure that supports operations in $$$log(N)$$$ , as explained below by fellow programmers.

Here is my solution https://cses.fi/paste/409056f70f117a41b33be3/