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

Автор BledDest, 6 лет назад, По-русски

1000A - Футболки Codehorses

Разбор
Решение

1000B - Подсвети

Разбор
Решение

1000C - Подсчет покрытых точек

Разбор
Решение

1000D - Очередная задача на подпоследовательности

Разбор
Решение

1000E - Нужно больше боссов

Разбор
Решение

1000F - Одно вхождение

Разбор
Решение с персистентным ДО
Решение с персистентным ДО (менее читаемое, но более быстрое)
Решение с обычным ДО
Решение с алгоритмом Мо

1000G - Два-пути

Разбор
Решение
  • Проголосовать: нравится
  • +53
  • Проголосовать: не нравится

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

Can F be solved with a mergesort tree? ( By keeping all the elements which occur once in the nodes, where each node is a sorted vector. Also, merging is easy. )

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

I like the fact that you have given different approaches for solving F. Thanks.

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

In B that's enough to check a_i + 1 only

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

    You don't right.

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

      Why not?

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

      hmm

      Am I wrong in the situaton when there are two adjacent points?

      I didn't think about it because my solution had AC

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

        Could you give a case please. Because if there are only 2 points the solution would be put nothing

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

          No, I meant another thing. It's correct that making a point in a_i-1 and a_i+1 would give the same result. But sometimes it's impossible to put a point in a_i+1(like if there are points with coordinates x and x+1)

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

      There are some facts I can't prove because of my bad Englsh)) They are not very hard, but I can try to write proofs for them in Russian if you would want)

      It's enough. If we can put a point in ai + 1 and ai - 1, the result will be the same. So I can be wrong in a situation when we can put a point in ai - 1 but can't in ai + 1. Let's take a look at this. The points must be like ...ai - 1, ai, ai + 1(because we can't make a point at here)... Let's increase i until we find a free ai + 1. What do we get? A sequence of adjacent points. I want to say that the result will be the same if we put a point in the left of this sequence or in the right.

      If we don't find this i, it means there is a sequence with the end at M. in that case I say that the result will be the same if we put a point in the left or don't put at all.

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

А это нормально, что у меня tl на 9 тесте, когда я отправляю авторское решение с МО?

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

    Нормально, у меня в полигоне оно тоже ТЛ ловит, надо его оптимизировать еще.

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

    нет

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

Mo-based solution for problem 1000F - One Occurrence is getting TLE

At first I try to write a solution with help of Mo-based solution Editorial, but I getting TLE, after getting many TLE I submit the code from Editorial exactly, but this also getting TLE...:(

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

Can anyone explain normal segment tree approach for 1000F - One Occurrence ?

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

What will happen in E if the graph is complete graph? there will not be any bridge. so what will be the answer?

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

What will happen in E if the graph is complete graph? there won't be any bridge. so what will be the answer?

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

The problem F is very similar to this problem from BZOJ.

Sorry for my poor English.

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

Can anyone elaborate 1000D.

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

Mo-based solution of Problem F doesn't get AC.

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

    Thanks for information! It is being discussed 3 comments higher.

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

Is it possible to use F's solution to query element that occurs exactly t times on a segment?

I think that in this case we would go with persistent ST from right to left (instead of left->right as in editorial). Suppose we are making a version that corresponds to l'th element of an array. We could precompute indices of occurrence of every array element. Suppose array[l] has more or equal than t - 1 occurrences after l: in this case we should update index of t - 1'th position with an index of t'th one or if such doesn't exist. So, the version l now contains t + 1'th occurrences of t'th elements. Initial version is initialized with negative infinities.

Now, the query [l, r] is the range maximum query [l, r] on l'th version.

As in editorial one needs to unset t + 1'th position assigning negative infinity to it.

I also realized that we could build the tree from left to right and do range minimum queries just as in editorial.

Please, tell me if there is an apparent flaw in this solution.

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

Anyone please explain the solution of Problem 1000F with standard segment tree

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

Why do we need to 'keep only the unique values' in the solution for problem C? I got AC with this and it was pretty straightforward.

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

I got it :) sorry for that comment

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

We can solve G online with same approach from editorial. Let f(2^i, u) is maximal profit of 2-path from u to 2^i th ancestor of u. f can easily calculated using d' and dp in editorial. Then, we can solve query (u, v) by binary jump on tree (similar to finding LCA).

Code: 39743132

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

Слишком сложное объяснение С, как по мне, есть намного более легкий способ. Разве что автор объяснил, что есть "Метод сжатия координат".

Мое решение , основанное на разделении на подотрезки:

http://codeforces.com/contest/1000/submission/39776916

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

int tp = (i & 1) ^ 1; meaning in B solution code??Can anyone explain

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

Can anyone suggest a good resource for understanding and implementing bridges in a graph ?

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

Is this some kind of hashing of edges? y = A[h] ^ B[h] ^ x; in Farhod's solution for 1000G.

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

There's another way to do problem D that might make the code simplier. Maintain a dp[][] where the first dimension is the index you are at and the second dimension is the number of integers you have to choose to include before you can open another good sequence.

dp[i][k] is simply the sum of:

  1. dp[i — 1][k] (you didn't chose the current index to be included in the subsequence)
  2. dp[i — 1][k + 1] (you chose the current index to be included in the subsequence)
  3. dp[i — 1][0] if arr[i] = k and k isn't 0 (you open a new good sequence)

At the beginning, dp[0][0] = 1. At the end, the answer is dp[n][0] — 1 (because empty subsequence isn't allowed)

This is the code:

http://codeforces.com/contest/1000/submission/39787639

Opps and forget my dfs function it's not part of the solution.

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

We don't necessarily have to answer the queries offline in 1000G.

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

Another cool dp for G: You can use the dp i -> i, and another one called "dpBestToRoot" which saves the best path to root (which is basically the best detour downward + the bestPathToRoot of your parent + the weight between you and your parent — your part in the detour of your parent).

Now you just do dpBestToRoot[u]+dpBestToRoot[v]-2*dpBestToRoot[lca]+dp[lca]. (http://codeforces.com/contest/1000/submission/39811424)

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

I think more straightforward solution for G is to count dpv than build heavy light decomposition with prefix sums of ways where value for each node would be dpv - ev, u - max(dpu - 2 * ev, u) where v is node and u is child of v that lying on the same way(you know this ways in HLD) 39742958

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

In fact, problem C can be solved with O(n) complexity,I think :P

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

В задаче F для того чтобы алгоритм МО заходил совсем не обязательно хранить sqrt декомпозицию. Сделаем такую вещь. Создадим стек и массив пометок, где для каждого числа от 1 до maxa будет записано есть ли оно в стеке. Теперь, когда на надо добавить число, мы добавляем его если его еще нет в стеке (для этого нужен массив пометок), если нужно удалить элемент ничего не делаем. Теперь заметим, что в каждый момент времени в стеке лежат элементы, которые могут быть потенциальным ответом. Теперь, чтобы получить ответ, мы удаляем верхний элемент из стека (не забывая снять пометку) пока он не ответ. Заметим, что суммарно движений границы порядка nsqrt(n), значит суммарно таких добавлений/удаления nsqrt(n), что не портит ассимптотику, добавляет это O(n) памяти, что не критично. Моя посылка с этом решением на раунде

https://codeforces.com/contest/1000/submission/39713669

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

Anyone Plz HELP me for 1000C — Covered Points Count I am implementing the same logic as describes above but getting WA for Test Case 4 For Points Covered by 1 Segment. My Code — 39936831 THANKS IN ADVANCE

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

In G, since wi > 0, does it matter if we remove the "twice" constraint in the statement?

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

This block is superb.

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

In problem E, what is being asked in the question and why the answer for that is equal to the diameter of the bridge tree instead of the total number of bridges?

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

    I also did not understand at the beginning. In the problem they ask to find two vertex such that the number of bridges between them is maximum The task does not give specific starting and finishing positions, you need to find such that the number of bridges between them is maximum (shortest path)

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

For problem F, I used a mergesort tree to get online queries in $$$O(\log^2 n)$$$ (barely snuck under the time limit).

Let each element in the array be a triple $$$(l, r, x)$$$, where $$$x$$$ is the value of that element, $$$l$$$ is the index of the previous occurrence of value $$$x$$$ in the array $$$+1$$$, and $$$r$$$ is the index of the next occurrence of value $$$x$$$ in the array $$$-1$$$. If there is no next or previous occurrence, then just use $$$+\infty$$$ or $$$-\infty$$$ or something.

Now, to query an interval $$$[a, b]$$$, we would like to know if there exists any triple $$$(l, r, x)$$$ in the array between indices $$$a$$$ and $$$b$$$, such that $$$l\leq a$$$ and $$$r\geq b$$$. If any such triple exists, we return $$$x$$$.

Now, we build a mergesort tree on the triples $$$(l, r, x)$$$ (sorted by $$$l$$$). Each node of the segment tree will store a sorted list of the triples on some interval, as well as a list of pairs $$$(r, x)$$$ which denote prefix maximums in terms of $$$r$$$ (and $$$x$$$ just gets copied over from whichever element had the largest $$$r$$$), on that sorted list.

Next, using normal segment tree tactics, we can make it so that for a query interval $$$[a, b]$$$, we only need to examine $$$O(\log n)$$$ nodes to try and find some valid $$$x$$$. For each node, we binary search the sorted list of triples to get a prefix such that $$$l\leq a$$$, and then take the corresponding $$$(r, x)$$$ at the end of that prefix, which gives us the largest possible $$$r$$$ on that prefix. If $$$r<b$$$, then no valid solution exists, otherwise the $$$x$$$ that we found is valid. Examining one node thus takes $$$O(\log n)$$$ time.

So, we can query an interval $$$[a, b]$$$ in $$$O(\log^2 n)$$$ time. Submission here: 84532864

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

strict time limit for problem F even edutorial solutions get TLE

»
9 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
  • F can be solved with a simple minimum segment tree, a standard segtree + sorting technique.

  • We just require the last and 2nd last occurrence of each element to answer our queries.
  • In ascending order, we can sort queries (x, y) based on Y [end points].
  • We keep adding elements until we can answer a query. Let's call it an $$$ADD(value, index)$$$ operation.
  • To find the answer for each query, we can do a query on the segment tree, let's call it $$$FIND(l,r)$$$ operation, for the time being...

  • Now we will see how we can implement $$$add(v , i)$$$ and $$$find(l,r)$$$ functions using a segment tree.

  • add(value , i)
    • Change last occurence $$$i$$$ and second last occurence $$$j$$$ of $$$value$$$ accordingly.
    • set $$$segtree[i] = (j , val)$$$
    • set $$$segtree[j] = (inf , -1)$$$

  • find(l , r)
    • Lets do a min range query $$$(l , r)$$$.
    • If we get our pair as $$$(index, value)$$$ where $$$index < l$$$, then it means that the $$$value$$$ has one of its occurrences within range and its previous occurrence is outside the range. So $$$value$$$ is a perfect candidate for the answer.
    • If $$$index >= l$$$, then no such number exists with the unique occurrence, print 0.

Basic idea: Sort queries with endpoints, index segtree with last occurrence index, and find if any value's prev last occurrence is in out of range.
Code : https://codeforces.com/contest/1000/submission/214379121
Complexity: $$$O(NlogN)$$$

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

Problem G can be solved online in same complexity of $$$O((n + q)\log{n})$$$ with any tree path sum structure and some additional precomputation.

Implementation: link

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

can anybody explain what problem E is asking ?