Shayan's blog

By Shayan, 3 months ago, In English

Note: The text editorials will be provided by the authors of the round. This video tutorial acts as an additional resource for those who prefer video over text, not as a substitute for the text editorial.

2019A — Max Plus Size

Video

2019B — All Pairs Segments

Video

2019C — Cards Partition

Video

2019D — Speedbreaker

Video

2019E — Tree Pruning

Video
  • Vote: I like it
  • +45
  • Vote: I do not like it

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

Update: Now Videos are playing. Thank you Shayan for the video tutorial.

Video tutorials are not playing! It shows this error An error occurred. Please try again later. (Playback ID: dLE5_mB8I9IVhzlN)

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

Why all downvotes? I think these videos are much better than texts.

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

I still don't understand how to solve the problem B-All Pairs.

Please help.

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

    You can check out the explanation by ninjacoder2209 in this video, he has explained it very nicely as well. It might give you some more clearity

    https://www.youtube.com/watch?v=2M5S9QNetg4

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

    We can derive a formula for each of the points in the array a to find out in how many segments they exist. Now for the numbers between each element of a (if any exist) the formula for how many segments they exist in is slightly different.

    Anyhow, once we have the said formula, we can start looping through the array a and in a map store the number of segments the element ai exists in and increment it if any other ai exists the same number of times. For the numbers between ai, we do the same thing.

    Once we have this map of segments and the count of numbers that exist in these segments we can loop through queries, see if they exist and then output the answer accordingly.

    Here's a python code that breaks it down and I have added comments as well that break down the formula derivation.

    t = int(input())
    for i in range(t):
        n, q = list(map(int, input().split(" ")))
        a = list(map(int, input().split(" ")))
        k = list(map(int, input().split(" ")))
    
        ans = {}
    
        l = n
        n -= 1
        for i in range(l-1):
            ans[a[i]] = n-i + i*(n-i+1)
    
            if a[i+1] - a[i] > 1:
                ans[a[i]+1] = n-i + i*(n-i)
        ans[a[i+1]] = n
    
        print(ans)
    
        """
        1 2 3 5 6 7
    
        1 = 5
        2 = 4 + 5                   = 9
        3 = 3 + 4 + 4 or (5-1)      = 11
        # 4 = 3 + 3 or (4-1) + 3 or (4-1) = 9
        5 = 2 + 3 + 3 + 3 = 11
        7 = 1 + 1 + 1 + 1 + 1 =
    
        1 = n
        2 = n-1 + n
        3 = n-2 + 2*(n-1)
        .
        4 = n-2 + 2*(n-2)
        .
        5 = n-3 + 3*(n-2)
        6 = n-4 + 4*(n-3)
        7 = n-5 + 5*(n-4)
        """