Fighting Tournament (Codeforces 1719C)
Tags: Implement, two pointers

Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya.$$$n$$$ athletes participate in the tournament, numbered from $$$1$$$ to $$$n$$$. Burenka determined the strength of the $$$i$$$-th athlete as an integer $$$a_i$$$, where $$$1 \leq a_i \leq n$$$. All the strength values are different, that is, the array $$$a$$$ is a permutation of length $$$n$$$. We know that in a fight, if $$$a_i > a_j$$$, then the $$$i$$$-th participant always wins the $$$j$$$-th.The tournament goes like this: initially, all $$$n$$$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back.Burenka decided to ask Tonya $$$q$$$ questions. In each question, Burenka asks how many victories the $$$i$$$-th participant gets in the first $$$k$$$ rounds of the competition for some given numbers $$$i$$$ and $$$k$$$. Tonya is not very good at analytics, so he asks you to help him answer all the questions.

Input: The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq q \leq 10^5$$$) — the number of tournament participants and the number of questions.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$) — the array $$$a$$$, which is a permutation.The next $$$q$$$ lines of a test case contain questions. Each line contains two integers $$$i$$$ and $$$k$$$ ($$$1 \leq i \leq n$$$, $$$1 \leq k \leq 10^9$$$) — the number of the participant and the number of rounds.It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$10^5$$$.

Output: For each Burenka's question, print a single line containing one integer — the answer to the question.

Note: In the first test case, the first numbered athlete has the strength of $$$3$$$, in the first round he will defeat the athlete with the number $$$2$$$ and the strength of $$$1$$$, and in the second round, the athlete with the number $$$3$$$ and the strength of $$$2$$$.In the second test case, we list the strengths of the athletes fighting in the first $$$5$$$ fights: $$$1$$$ and $$$3$$$, $$$3$$$ and $$$4$$$, $$$4$$$ and $$$2$$$, $$$4$$$ and $$$1$$$, $$$4$$$ and $$$3$$$. The participant with the number $$$4$$$ in the first $$$5$$$ rounds won $$$0$$$ times (his strength is $$$2$$$). The participant with the number $$$3$$$ has a strength of $$$4$$$ and won $$$1$$$ time in the first two fights by fighting $$$1$$$ time.

Sample Inputs:
3
3 1
3 1 2
1 2
4 2
1 3 4 2
4 5
3 2
5 2
1 2 3 5 4
5 1000000000
4 6

Sample Outputs:
2
0
1
0
4

Solution (Python):
def s():
    (n, q) = map(int, input().split())
    a = [int(i) for i in input().split()]
    l = [[0, 0] for i in range(n)]
    (now, ma, ind) = (a[0], max(a), 0)
    if a[0] > a[1]:
        l[0][1] = 1
    if a[0] == ma:
        l[0][0] = 1000000000
    else:
        for i in range(1, n):
            if a[i] == ma:
                (l[i][0], l[i][1]) = (1000000000, i)
                break
            if a[i] < now:
                l[ind][0] += 1
            else:
                (ind, now) = (i, a[i])
                l[ind][0] = 1
                l[ind][1] = i
    res = ''
    for x in range(q):
        (participant, round) = map(int, input().split())
        if l[participant - 1][0] == 0:
            res += '0\n'
        else:
            p = round - l[participant - 1][1]
            if p < 0:
                res += '0\n'
            else:
                res += str(min(p + 1, l[participant - 1][0])) + '\n'
    return res
res = ''
t = int(input())
for x in range(t):
    res += s()
print(res)

Solution (Python):
import sys
input = sys.stdin.readline

def int_num():
    return int(input())

def int_list():
    return list(map(int, input().split()))

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())

def solve():
    (n, q) = int_list()
    a = int_list()
    d = {}
    (max_id, max_first) = (0, 1)
    (head_id, head_num) = (0, a[0])
    for i in range(1, n):
        if a[i] == n:
            (max_id, max_first) = (i, i)
            break
        if a[i] > head_num:
            (head_id, head_num) = (i, a[i])
        if head_id in d:
            (first, cnt) = d[head_id]
            d[head_id] = (first, cnt + 1)
        else:
            d[head_id] = (i, 1)
    while q:
        (qid, k) = int_list()
        qid = qid - 1
        if qid == max_id:
            ans = 0 if k < max_id else k - max_first + 1
        elif qid in d:
            (first, cnt) = d[qid]
            if k < first:
                ans = 0
            elif k > first + cnt - 1:
                ans = cnt
            else:
                ans = k - first + 1
        else:
            ans = 0
        print(ans)
        q -= 1

def main():
    t = int_num()
    while t:
        solve()
        t -= 1
    return
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
t = int(input())
for t_item in range(t):
    (n, q) = map(int, input().split())
    a = [int(i) for i in input().split()]
    b = [0] * n
    prev = 0
    for i in range(1, n):
        if a[i] > a[prev]:
            prev = i
        b[prev] += 1
    for j in range(q):
        (i, k) = map(int, input().split())
        i -= 1
        if b[i] == 0:
            print(0)
            continue
        if k >= n - 1:
            if a[i] == n:
                print(b[i] + k - (n - 1))
            else:
                print(b[i])
        elif k < i:
            print(0)
        elif i == 0:
            print(min(k - i, b[i]))
        else:
            print(min(k - i + 1, b[i]))