/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 35ms 18.91 MiB
#2 Accepted 137ms 22.594 MiB
#3 Accepted 144ms 22.285 MiB
#4 Accepted 136ms 21.746 MiB
#5 Accepted 138ms 22.492 MiB
#6 Accepted 141ms 21.805 MiB
#7 Accepted 142ms 21.289 MiB
#8 Accepted 138ms 21.863 MiB
#9 Accepted 148ms 22.758 MiB
#10 Accepted 145ms 22.477 MiB
#11 Accepted 142ms 22.742 MiB
#12 Accepted 141ms 22.25 MiB
#13 Accepted 142ms 21.992 MiB
#14 Accepted 139ms 22.492 MiB
#15 Accepted 145ms 21.836 MiB
#16 Accepted 143ms 22.242 MiB
#17 Accepted 143ms 21.812 MiB
#18 Accepted 143ms 22.242 MiB
#19 Accepted 138ms 21.961 MiB
#20 Accepted 148ms 22.266 MiB
#21 Accepted 138ms 22.117 MiB
#22 Accepted 144ms 22.242 MiB
#23 Accepted 138ms 22.742 MiB
#24 Accepted 149ms 22.164 MiB
#25 Accepted 142ms 21.738 MiB
#26 Accepted 138ms 22.539 MiB
#27 Accepted 142ms 22.074 MiB
#28 Accepted 141ms 21.816 MiB
#29 Accepted 142ms 21.934 MiB
#30 Accepted 142ms 21.711 MiB
#31 Accepted 140ms 22.145 MiB
#32 Accepted 149ms 22.316 MiB
#33 Accepted 138ms 22.227 MiB
#34 Accepted 143ms 22.363 MiB
#35 Accepted 138ms 22.242 MiB
#36 Accepted 141ms 22.492 MiB
#37 Accepted 142ms 21.969 MiB
#38 Accepted 144ms 21.977 MiB
#39 Accepted 141ms 22.051 MiB
#40 Accepted 140ms 21.992 MiB
#41 Accepted 169ms 21.992 MiB
#42 Accepted 370ms 32.875 MiB
#43 Accepted 382ms 32.926 MiB
#44 Accepted 440ms 32.359 MiB
#45 Accepted 363ms 32.73 MiB
#46 Accepted 363ms 32.84 MiB
#47 Accepted 401ms 32.902 MiB
#48 Accepted 376ms 32.938 MiB
#49 Accepted 371ms 32.488 MiB
#50 Accepted 371ms 32.863 MiB

Code

import math
import sys
input = sys.stdin.readline

MAXN = 100005
MAXBLOCKS = 400
NEG_INF = -10**18

a = [0] * MAXN
add = [0] * MAXBLOCKS
mx = [NEG_INF] * MAXBLOCKS
block = [0] * MAXN
n = 0
blocksize = 0

def update(l, r, x):
    global a, add, mx
    blockl = block[l]
    blockr = block[r]

    if blockl == blockr:
        for i in range(l, r + 1):
            a[i] += x
        mxb = NEG_INF
        for i in range((blocksize * (blockl - 1)) + 1, min(n, blocksize * blockl) + 1):
            a[i] += add[blockl]
            mxb = max(mxb, a[i])
        mx[blockl] = mxb
        add[blockl] = 0
        return

    if l % blocksize != 1:
        last = min(n, blocksize * blockl)
        for i in range(l, last + 1):
            a[i] += x
        mxb = NEG_INF
        for i in range((blocksize * (blockl - 1)) + 1, last + 1):
            a[i] += add[blockl]
            mxb = max(mxb, a[i])
        mx[blockl] = mxb
        add[blockl] = 0
        blockl += 1

    if r % blocksize != 0:
        first = blocksize * (blockr - 1) + 1
        for i in range(r, first - 1, -1):
            a[i] += x
        mxb = NEG_INF
        for i in range(min(n, blocksize * blockr), first - 1, -1):
            a[i] += add[blockr]
            mxb = max(mxb, a[i])
        mx[blockr] = mxb
        add[blockr] = 0
        blockr -= 1

    for i in range(blockl, blockr + 1):
        add[i] += x

def query(l, r):
    ans = NEG_INF
    blockl = block[l]
    blockr = block[r]

    if blockl == blockr:
        for i in range(l, r + 1):
            ans = max(ans, a[i] + add[blockl])
        return ans

    if l % blocksize != 1:
        last = min(n, blocksize * blockl)
        for i in range(l, last + 1):
            ans = max(ans, a[i] + add[blockl])
        blockl += 1

    if r % blocksize != 0:
        first = blocksize * (blockr - 1) + 1
        for i in range(r, first - 1, -1):
            ans = max(ans, a[i] + add[blockr])
        blockr -= 1

    for i in range(blockl, blockr + 1):
        ans = max(ans, mx[i] + add[i])
    return ans

def solve():
    global n, blocksize
    t = int(input())
    for _ in range(t):
        n = int(input())
        blocksize = int(math.sqrt(n))
        arr = list(map(int, input().split()))
        for i in range(1, n + 1):
            a[i] = arr[i - 1]
            block[i] = (i + blocksize - 1) // blocksize

        for i in range(1, MAXBLOCKS):
            add[i] = 0
            mx[i] = NEG_INF

        for i in range(1, n + 1, blocksize):
            mxv = a[i]
            for j in range(i, min(n, i + blocksize - 1) + 1):
                mxv = max(mxv, a[j])
            mx[block[i]] = mxv

        q = int(input())
        for _ in range(q):
            args = list(map(int, input().split()))
            if args[0] == 1:
                _, l, r, x = args
                update(l, r, x)
            else:
                _, l, r = args
                print(query(l, r))

if __name__ == "__main__":
    solve()

Information

Submit By
Type
Submission
Problem
P1211 Range MAX
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2025-07-10 09:25:11
Judged At
2025-07-10 09:25:11
Judged By
Score
100
Total Time
440ms
Peak Memory
32.938 MiB