結果

問題 No.151 セグメントフィッシング
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-09 20:15:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 139 ms / 5,000 ms
コード長 1,798 bytes
コンパイル時間 1,386 ms
コンパイル使用メモリ 86,740 KB
実行使用メモリ 79,592 KB
最終ジャッジ日時 2023-09-21 07:58:30
合計ジャッジ時間 5,370 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,184 KB
testcase_01 AC 73 ms
71,284 KB
testcase_02 AC 73 ms
71,200 KB
testcase_03 AC 74 ms
71,216 KB
testcase_04 AC 73 ms
71,316 KB
testcase_05 AC 74 ms
71,292 KB
testcase_06 AC 73 ms
70,980 KB
testcase_07 AC 74 ms
71,352 KB
testcase_08 AC 122 ms
77,624 KB
testcase_09 AC 122 ms
77,980 KB
testcase_10 AC 118 ms
77,712 KB
testcase_11 AC 124 ms
77,980 KB
testcase_12 AC 130 ms
78,120 KB
testcase_13 AC 130 ms
78,416 KB
testcase_14 AC 132 ms
79,116 KB
testcase_15 AC 131 ms
78,228 KB
testcase_16 AC 134 ms
78,328 KB
testcase_17 AC 94 ms
77,364 KB
testcase_18 AC 92 ms
77,156 KB
testcase_19 AC 133 ms
79,588 KB
testcase_20 AC 139 ms
79,592 KB
testcase_21 AC 117 ms
77,584 KB
testcase_22 AC 119 ms
77,668 KB
testcase_23 AC 135 ms
77,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


class fenwick_tree(object):
    def __init__(self, n):
        self.n = n
        self.log = n.bit_length()
        self.data = [0] * n

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def add(self, p, x):
        """ a[p] += xを行う"""
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """a[l] + a[l+1] + .. + a[r-1]を返す"""
        return self.__sum(r) - self.__sum(l)

    def lower_bound(self, x):
        """a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""
        if x <= 0:
            return -1
        i = 0
        k = 1 << self.log
        while k:
            if i + k <= self.n and self.data[i + k - 1] < x:
                x -= self.data[i + k - 1]
                i += k
            k >>= 1
        return i


N, Q = map(int, input().split())
bit = fenwick_tree(2 * N)
ans = []

for time in range(Q):
    X, Y, Z = input().rstrip().split()
    if X == "L":
        Y = (int(Y) + time) % (2 * N)
        bit.add(Y, int(Z))
    elif X == "R":
        Y = (2 * N - int(Y) - 1 + time) % (2 * N)
        bit.add(Y, int(Z))
    else:
        cnt = 0
        L = (int(Y) + time) % (2 * N)
        R = (int(Z) + time - 1) % (2 * N)
        if L <= R:
            cnt += bit.sum(L, R + 1)
        else:
            cnt += bit.sum(0, R + 1) + bit.sum(L, 2 * N)
        L = (2 * N - int(Z) + time) % (2 * N)
        R = (2 * N - int(Y) - 1 + time) % (2 * N)
        if L <= R:
            cnt += bit.sum(L, R + 1)
        else:
            cnt += bit.sum(0, R + 1) + bit.sum(L, 2 * N)
        ans.append(cnt)

print(*ans, sep="\n")
0