結果

問題 No.151 セグメントフィッシング
ユーザー convexineqconvexineq
提出日時 2020-12-10 08:47:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 275 ms / 5,000 ms
コード長 1,997 bytes
コンパイル時間 673 ms
コンパイル使用メモリ 81,820 KB
実行使用メモリ 80,576 KB
最終ジャッジ日時 2023-10-19 20:28:25
合計ジャッジ時間 5,990 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,484 KB
testcase_01 AC 41 ms
53,484 KB
testcase_02 AC 40 ms
53,484 KB
testcase_03 AC 40 ms
53,484 KB
testcase_04 AC 41 ms
53,484 KB
testcase_05 AC 43 ms
53,484 KB
testcase_06 AC 43 ms
53,484 KB
testcase_07 AC 43 ms
53,484 KB
testcase_08 AC 141 ms
77,860 KB
testcase_09 AC 149 ms
77,708 KB
testcase_10 AC 155 ms
77,708 KB
testcase_11 AC 152 ms
77,712 KB
testcase_12 AC 262 ms
80,020 KB
testcase_13 AC 256 ms
80,176 KB
testcase_14 AC 262 ms
79,804 KB
testcase_15 AC 261 ms
79,880 KB
testcase_16 AC 255 ms
80,360 KB
testcase_17 AC 91 ms
76,900 KB
testcase_18 AC 94 ms
78,228 KB
testcase_19 AC 272 ms
80,576 KB
testcase_20 AC 275 ms
80,164 KB
testcase_21 AC 172 ms
77,720 KB
testcase_22 AC 167 ms
77,956 KB
testcase_23 AC 230 ms
77,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class segment_tree:
    __slots__ = ["op_M", "e_M","N","N0","dat"]
    def __init__(self, N, operator_M, e_M):
        self.op_M = operator_M
        self.e_M = e_M
        self.N = N
        self.N0 = 1<<(N-1).bit_length()
        self.dat = [self.e_M]*(2*self.N0)
    
    # 長さNの配列 initial で初期化
    def build(self, initial):
        assert self.N == len(initial)
        self.dat[self.N0:self.N0+len(initial)] = initial[:]
        for k in range(self.N0-1,0,-1):
            self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])

    # a_k の値を x に更新
    def update(self,k,x):
        k += self.N0
        self.dat[k] = x
        k >>= 1
        while k:
            self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])
            k >>= 1

    # 区間[L,R]をopでまとめる
    def query(self,L,R):
        L += self.N0; R += self.N0 + 1 
        sl = sr = self.e_M
        while L < R:
            if R & 1:
                R -= 1
                sr = self.op_M(self.dat[R],sr)
            if L & 1:
                sl = self.op_M(sl,self.dat[L])
                L += 1
            L >>= 1; R >>= 1
        return self.op_M(sl,sr)

    def get(self, k): #k番目の値を取得。query[k,k]と同じ
        return self.dat[k+self.N0]

n,q = map(int,input().split())
from operator import add
seg = segment_tree(2*n,add,0)
for t in range(1,q+1):
    x,y,z = input().split()
    y,z = int(y),int(z)
    if x=="L":
        idx = (2*n-y-1-t)%(2*n)
        seg.update(idx, seg.get(idx) + z)
    elif x=="R":
        idx = (y-t)%(2*n)
        seg.update(idx, seg.get(idx) + z)
    else:
        v = 0
        L = (y-t)%(2*n)
        R = (z-1-t)%(2*n)
        if L <= R:
            v += seg.query(L,R)
        else:
            v += seg.query(0,R) + seg.query(L,2*n-1)
        R = (2*n-y-1-t)%(2*n)
        L = (2*n-z-t)%(2*n)
        if L <= R:
            v += seg.query(L,R)
        else:
            v += seg.query(0,R) + seg.query(L,2*n-1)
        print(v)
0