結果

問題 No.151 セグメントフィッシング
ユーザー 👑 rin204rin204
提出日時 2022-11-03 17:15:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 399 ms / 5,000 ms
コード長 2,752 bytes
コンパイル時間 607 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 86,172 KB
最終ジャッジ日時 2023-09-25 01:42:10
合計ジャッジ時間 7,121 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,144 KB
testcase_01 AC 71 ms
71,144 KB
testcase_02 AC 71 ms
71,464 KB
testcase_03 AC 73 ms
71,184 KB
testcase_04 AC 73 ms
71,184 KB
testcase_05 AC 76 ms
71,280 KB
testcase_06 AC 78 ms
71,384 KB
testcase_07 AC 75 ms
71,284 KB
testcase_08 AC 209 ms
79,708 KB
testcase_09 AC 206 ms
81,212 KB
testcase_10 AC 205 ms
80,360 KB
testcase_11 AC 198 ms
79,828 KB
testcase_12 AC 369 ms
84,948 KB
testcase_13 AC 399 ms
86,172 KB
testcase_14 AC 378 ms
85,272 KB
testcase_15 AC 359 ms
84,704 KB
testcase_16 AC 371 ms
84,532 KB
testcase_17 AC 116 ms
77,600 KB
testcase_18 AC 120 ms
77,548 KB
testcase_19 AC 339 ms
81,756 KB
testcase_20 AC 339 ms
81,812 KB
testcase_21 AC 217 ms
80,156 KB
testcase_22 AC 207 ms
79,164 KB
testcase_23 AC 365 ms
85,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.n0 = 1 << (n.bit_length() - 1)
        self.tree = [0] * (n + 1)
    
    def range_sum(self, l, r):
        return self.sum(r - 1) - self.sum(l - 1)
        
    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
        
    def get(self, i):
        return self.sum(i) - self.sum(i - 1)
 
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
         
    def lower_bound(self, x):
        pos = 0
        plus = self.n0
        while plus > 0:
            if pos + plus <= self.size and self.tree[pos + plus] < x:
                x -= self.tree[pos + plus]
                pos += plus
            plus //= 2
        return pos

n, Q = map(int, input().split())

def start(t, x, lr):
    t %= (2 * n)
    while t > 0:
        if lr == "R":
            if x == 0:
                t -= 1
                lr = "L"
            else:
                d = x
                mi = min(t, d)
                t -= mi
                x -= mi
        else:
            if x == n - 1:
                t -= 1
                lr = "R"
            else:
                d = n - 1 - x
                mi = min(t, d)
                t -= mi
                x += mi
    return x, lr

L = Bit(n)
R = Bit(n)

for t in range(Q):
    query = input().split()
    y, z = map(int, query[1:])
    if query[0] == "L":
        p, lr = start(t, y, "L")
        if lr == "L":
            L.add(p, z)
        else:
            R.add(p, z)
    elif query[0] == "R":
        p, lr = start(t, y, "R")
        if lr == "L":
            L.add(p, z)
        else:
            R.add(p, z)
    else:
        ans = 0
        z -= 1
        p1, lr1 = start(t, y, "L")
        p2, lr2 = start(t, z, "L")
        if lr1 == lr2:
            if p1 > p2:
                p1, p2 = p2, p1
            if lr1 == "L":
                ans += L.range_sum(p1, p2 + 1)
            else:
                ans += R.range_sum(p1, p2 + 1)
        elif lr1 == "L":
            ans += L.range_sum(p1, n) + R.range_sum(p2, n)
        else:
            ans += R.range_sum(0, p1 + 1) + L.range_sum(0, p2 + 1)

        p1, lr1 = start(t, y, "R")
        p2, lr2 = start(t, z, "R")
        if lr1 == lr2:
            if p1 > p2:
                p1, p2 = p2, p1
            if lr1 == "L":
                ans += L.range_sum(p1, p2 + 1)
            else:
                ans += R.range_sum(p1, p2 + 1)
        elif lr1 == "R":
            ans += R.range_sum(p1, n) + L.range_sum(p2, n)
        else:
            ans += L.range_sum(0, p1 + 1) + R.range_sum(0, p2 + 1)

        print(ans)
0