結果

問題 No.1982 [Cherry 4th Tune B] 絶険
ユーザー lam6er
提出日時 2025-03-26 15:48:47
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,967 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 567,276 KB
最終ジャッジ日時 2025-03-26 15:49:48
合計ジャッジ時間 11,582 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 5 MLE * 1 -- * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

class Node:
    __slots__ = ['left', 'right', 'sum']
    def __init__(self, left=None, right=None, sum=0):
        self.left = left
        self.right = right
        self.sum = sum

def main():
    import sys
    sys.setrecursionlimit(1 << 25)
    input = sys.stdin.read
    data = input().split()
    ptr = 0
    N = int(data[ptr])
    ptr +=1
    K = int(data[ptr])
    ptr +=1
    Q = int(data[ptr])
    ptr +=1

    operations = []
    for _ in range(K):
        L = int(data[ptr])
        ptr +=1
        R = int(data[ptr])
        ptr +=1
        C = int(data[ptr])
        ptr +=1
        H = int(data[ptr])
        ptr +=1
        operations.append( (L, R, C, H) )

    # Persistent segment tree setup
    roots = [None]*(K+1)  # roots[0] is the initial empty tree
    roots[0] = Node()
    current_root = roots[0]
    for j in range(1, K+1):
        L, R, C, H = operations[j-1]
        # Function to apply the range add, creating a new root
        def add(node, a, b):
            if b < L or a > R:
                return node
            new_node = Node()
            if node is not None:
                new_node.left = node.left
                new_node.right = node.right
                new_node.sum = node.sum
            else:
                new_node.sum = 0
            if L <= a and b <= R:
                new_node.sum += H
                return new_node
            mid = (a + b) // 2
            new_node.left = add(new_node.left, a, mid) if new_node.left else add(Node(), a, mid)
            new_node.right = add(new_node.right, mid+1, b) if new_node.right else add(Node(), mid+1, b)
            return new_node
        new_root = add(current_root, 1, N)
        roots[j] = new_root
        current_root = new_root

    # Query function
    def query(root, a, b, i):
        if not root:
            return 0
        res = root.sum
        if a == b:
            return res
        mid = (a + b) // 2
        if i <= mid:
            res += query(root.left, a, mid, i)
        else:
            res += query(root.right, mid+1, b, i)
        return res

    # Process queries
    output = []
    for _ in range(Q):
        I = int(data[ptr])
        ptr +=1
        X = int(data[ptr])
        ptr +=1
        x = X - 0.5
        if K ==0:
            output.append(-1)
            continue
        sum_total = query(roots[K], 1, N, I)
        if sum_total < x:
            output.append(-1)
            continue
        # Binary search to find the earliest j
        low = 1
        high = K
        answer = K
        while low <= high:
            mid = (low + high) //2
            s = query(roots[mid], 1, N, I)
            if s >= x:
                answer = mid
                high = mid -1
            else:
                low = mid +1
        # Get the color
        L_j, R_j, C_j, H_j = operations[answer-1]
        output.append(C_j)
    
    print('\n'.join(map(str, output)))

if __name__ == "__main__":
    main()
0