結果

問題 No.259 セグメントフィッシング+
ユーザー convexineqconvexineq
提出日時 2020-12-10 08:52:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 473 ms / 2,000 ms
コード長 2,008 bytes
コンパイル時間 1,210 ms
コンパイル使用メモリ 81,680 KB
実行使用メモリ 88,700 KB
最終ジャッジ日時 2023-10-19 20:34:58
合計ジャッジ時間 11,668 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,472 KB
testcase_01 AC 39 ms
53,472 KB
testcase_02 AC 39 ms
53,472 KB
testcase_03 AC 39 ms
53,472 KB
testcase_04 AC 39 ms
53,472 KB
testcase_05 AC 39 ms
53,472 KB
testcase_06 AC 39 ms
53,472 KB
testcase_07 AC 39 ms
53,472 KB
testcase_08 AC 418 ms
87,224 KB
testcase_09 AC 424 ms
87,464 KB
testcase_10 AC 419 ms
87,784 KB
testcase_11 AC 419 ms
87,700 KB
testcase_12 AC 424 ms
87,740 KB
testcase_13 AC 471 ms
87,632 KB
testcase_14 AC 458 ms
88,332 KB
testcase_15 AC 469 ms
88,008 KB
testcase_16 AC 466 ms
88,452 KB
testcase_17 AC 472 ms
88,016 KB
testcase_18 AC 470 ms
88,384 KB
testcase_19 AC 473 ms
88,700 KB
testcase_20 AC 470 ms
87,636 KB
testcase_21 AC 467 ms
88,488 KB
testcase_22 AC 461 ms
88,384 KB
testcase_23 AC 159 ms
76,956 KB
testcase_24 AC 295 ms
86,432 KB
testcase_25 AC 288 ms
85,848 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 _ in range(1,q+1):
    x,t,y,z = input().split()
    t,y,z = int(t),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