89342024-02-05 18:29:42NagyLeoSzínes szobák (50 pont)python3Accepted 50/50234ms48880 KiB
from sys import stdin, stdout

input = stdin.readline

def print(x):
    stdout.write(str(x) + "\n")


def main():
    N, K, M = map(int, input().split())
    rooms_raw = list(map(int, input().split()))
    rooms = []

    for i in rooms_raw:
        rooms.append([i, 1])

    left_people = 0
    for i in range(M):
        color, direct = map(int, input().split())

        for j in range(N):
            if rooms[j][0] == color:
                if j + direct > N - 1 or j + direct < 0:
                    left_people += rooms[j][1]
                    rooms[j][1] = 0
                else:
                    rooms[j + direct][1] += rooms[j][1]
                    rooms[j][1] = 0
    print(left_people)


def main2():
    N, K, M = map(int, input().split())
    rooms = [int(x) for x in input().split()]

    left_people = 0
    rules=[0]*M
    for i in range(M):
        rules[i]=[int(x) for x in input().split()]

    def kijute(i):
        # az i. emberrel mi tortenik?
        # balra, nem jut ki , jobbra  -1, 0, 1

        for j in range(M):
            if rooms[i] == rules[j][0]:
                i += rules[j][1]
                if i > N - 1:
                    return 1
                elif i < 0:
                    return -1

        return 0

    elso = kijute(0)
    utolso = kijute(N - 1)
    # jobbra:
    lo = 0
    hi = N - 1
    if elso == 1:
        print(N)
        return
    if utolso != 1:
        pass
    else:
        while hi - lo > 1:
            half = (hi + lo) // 2
            if kijute(half) == 1:
                hi = half
            else:
                lo = half
        left_people += N - hi
    # balra:
    lo = 0
    hi = N - 1
    if utolso == -1:
        print(N)
        return
    if elso != -1:
        pass
    else:
        while hi - lo > 1:
            half = (hi + lo) // 2
            if kijute(half) == -1:
                lo = half
            else:
                hi = half
        left_people += hi

    print(left_people)


def main3():
    N, K, M = map(int, input().split())
    rooms = [0] + [int(x) for x in input().split()] + [0]

    left_people = 0
    rules = [0] * M
    for i in range(M):
        rules[i] = [int(x) for x in input().split()]

    left_pos = 0
    right_pos = N + 1
    for i in range(M - 1, -1, -1):
        if rules[i][1] == -1 and rules[i][0] == rooms[left_pos + 1]:
            left_pos += 1
        if rules[i][1] == 1 and rules[i][0] == rooms[left_pos]:
            left_pos -= 1
        if rules[i][1] == 1 and rules[i][0] == rooms[right_pos - 1]:
            right_pos -= 1
        if rules[i][1] == -1 and rules[i][0] == rooms[right_pos]:
            right_pos += 1
    print(left_pos + (N + 1 - right_pos))


main3()
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/018ms11888 KiB
2Accepted0/0231ms47260 KiB
3Accepted1/119ms12544 KiB
4Accepted1/119ms12556 KiB
5Accepted1/119ms12480 KiB
6Accepted1/119ms12492 KiB
7Accepted1/119ms13060 KiB
8Accepted1/120ms13156 KiB
9Accepted1/121ms13032 KiB
10Accepted1/124ms14032 KiB
11Accepted1/120ms13556 KiB
12Accepted1/123ms14092 KiB
13Accepted1/137ms16468 KiB
14Accepted1/125ms14936 KiB
15Accepted1/126ms14940 KiB
16Accepted1/135ms16284 KiB
17Accepted1/139ms16556 KiB
18Accepted1/139ms16600 KiB
19Accepted1/1122ms36368 KiB
20Accepted1/1231ms48804 KiB
21Accepted1/1155ms38880 KiB
22Accepted1/1172ms41480 KiB
23Accepted1/1214ms46256 KiB
24Accepted1/1232ms48880 KiB
25Accepted1/1232ms48656 KiB
26Accepted1/1231ms48788 KiB
27Accepted3/3229ms43136 KiB
28Accepted3/3231ms43104 KiB
29Accepted3/3230ms42896 KiB
30Accepted3/3231ms43148 KiB
31Accepted3/3234ms42876 KiB
32Accepted3/3231ms43004 KiB
33Accepted4/4122ms28516 KiB
34Accepted4/4216ms41100 KiB