86462024-01-24 19:11:04NagyLeoMI bróker (50 pont)python3Time limit exceeded 5/501.088s24420 KiB
from sys import stdin, stdout

input = stdin.readline


# O(N*Q)
# N, Q = 0, 0
def main(Q, N, Prices):
    # N,Q = list(map(int, input().split()))
    # N,Q = [int(x) for x in input().split()]
    # N = NQ[0]
    # Q = NQ[1]
    # Prices = list(map(int, stdin.readline().split()))
    # balance = [0]*Q

    for i in range(Q):
        isHolding = False
        curBalance = 0
        lo, hi = list(map(int, stdin.readline().split()))
        for f in Prices:
            if not isHolding:
                if lo >= f:
                    curBalance -= f
                    isHolding = True
            elif hi <= f:
                curBalance += f
                isHolding = False
        # balance.append(curBalance)
        # balance[i]=curBalance

        # for i in balance:
        stdout.write(str(curBalance) + '\n')


# main()

# 5 10 10 10 5 5 5 5
# Q:
# 6 9
# 7 8
# 6 8
def ketszam():
    N, Q = list(map(int, input().split()))
    Prices = list(map(int, stdin.readline().split()))
    first = Prices[0]
    second = 0

    if Q <= 50:
        main(Q, N, Prices)
        return

    for i in Prices:  # range(1,len(Prices)))
        if i != first:
            second = i

    if second < first:
        second, first = first, second

    fourth = False
    fourthBalance = 0
    for i in range(Q):
        lo, hi = list(map(int, stdin.readline().split()))
        if lo < first:
            stdout.write('0\n')
        elif hi > second:
            if lo >= second and Prices[0] == second:
                stdout.write(str(-second) + '\n')
            else:
                stdout.write(str(-first) + '\n')
        else:
            if fourth == False:
                fourth = True
                isHolding = False
                curBalance = 0
                for f in Prices:
                    if not isHolding:
                        if lo >= f:
                            curBalance -= f
                            isHolding = True
                    elif hi <= f:
                        curBalance += f
                        isHolding = False

                stdout.write(str(curBalance) + '\n')
                fourthBalance = curBalance
            else:
                stdout.write(str(fourthBalance) + '\n')


#ketszam()

def solv():
    N, Q = list(map(int, input().split()))
    Prices = list(map(int, stdin.readline().split()))
    Valasz = [[0] * 501 for i in range(501)]

    # haszon
    for lo in range(1, 500):
        delta = [0] * 501
        utolsoElad = 501
        for ar in Prices:
            if ar <= lo:
                utolsoElad = lo
            elif utolsoElad < ar:
                delta[utolsoElad] += ar
                delta[ar] -= ar
                utolsoElad = ar
        # if lo==41:
        #    print(Prices)
        #    print(delta)
        #    print(delta[41],delta[80],delta[100],delta[120],delta[125])

        szumma = delta[lo]
        for eladas in range(lo + 1, 501):
            Valasz[lo][eladas] = szumma
            szumma += delta[eladas]

    # vetel
    for hi in range(500, 1, -1):
        delta = [0] * 501
        utolsoVesz = hi
        for ar in Prices:
            if ar >= hi:
                utolsoVesz = hi
            elif utolsoVesz > ar:
                delta[utolsoVesz] += ar
                delta[ar] -= ar
                utolsoVesz = ar
        # if hi==41:
        #    print(Prices)
        #    print(delta)
        #    print(delta[30],delta[40],delta[41],delta[120],delta[125])

        szumma = delta[hi]
        for eladas in range(hi - 1, 0, -1):
            Valasz[eladas][hi] -= szumma
            szumma += delta[eladas]

    for _ in range(Q):
        lo, hi = list(map(int, stdin.readline().split()))
        stdout.write(str(Valasz[lo][hi]) + '\n')


solv()
SubtaskSumTestVerdictTimeMemory
base5/50
1Accepted0/071ms22076 KiB
2Time limit exceeded0/01.07s10364 KiB
3Accepted1/164ms18420 KiB
4Accepted1/165ms21072 KiB
5Accepted2/2209ms24420 KiB
6Time limit exceeded0/21.069s11216 KiB
7Time limit exceeded0/21.083s11348 KiB
8Accepted1/1971ms21304 KiB
9Time limit exceeded0/11.041s9892 KiB
10Time limit exceeded0/21.036s11844 KiB
11Time limit exceeded0/21.065s10880 KiB
12Time limit exceeded0/21.075s11724 KiB
13Time limit exceeded0/21.069s11876 KiB
14Time limit exceeded0/21.06s11400 KiB
15Time limit exceeded0/31.057s12504 KiB
16Time limit exceeded0/31.072s12392 KiB
17Time limit exceeded0/31.052s12252 KiB
18Time limit exceeded0/31.065s12356 KiB
19Time limit exceeded0/31.075s12660 KiB
20Time limit exceeded0/31.088s12728 KiB
21Time limit exceeded0/31.077s12616 KiB
22Time limit exceeded0/31.059s12732 KiB
23Time limit exceeded0/31.075s12716 KiB
24Time limit exceeded0/31.065s12828 KiB