結果

問題 No.151 セグメントフィッシング
ユーザー maspymaspy
提出日時 2020-03-26 22:41:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 203 ms / 5,000 ms
コード長 2,073 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 16,584 KB
最終ジャッジ日時 2023-08-30 15:52:25
合計ジャッジ時間 4,185 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,144 KB
testcase_01 AC 18 ms
8,144 KB
testcase_02 AC 17 ms
8,104 KB
testcase_03 AC 16 ms
8,180 KB
testcase_04 AC 16 ms
8,084 KB
testcase_05 AC 16 ms
8,036 KB
testcase_06 AC 17 ms
8,136 KB
testcase_07 AC 17 ms
7,984 KB
testcase_08 AC 46 ms
10,080 KB
testcase_09 AC 47 ms
10,144 KB
testcase_10 AC 47 ms
9,984 KB
testcase_11 AC 47 ms
10,196 KB
testcase_12 AC 163 ms
15,636 KB
testcase_13 AC 162 ms
16,580 KB
testcase_14 AC 164 ms
16,528 KB
testcase_15 AC 164 ms
15,640 KB
testcase_16 AC 165 ms
16,584 KB
testcase_17 AC 113 ms
14,452 KB
testcase_18 AC 142 ms
16,316 KB
testcase_19 AC 203 ms
15,624 KB
testcase_20 AC 202 ms
15,436 KB
testcase_21 AC 139 ms
16,256 KB
testcase_22 AC 142 ms
16,400 KB
testcase_23 AC 107 ms
14,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, Q = map(int, readline().split())
X, Y, Z = zip(*[line.split() for line in readlines()])
Y = map(int, Y)
Z = map(int, Z)


class BinaryIndexedTree():
    def __init__(self, seq):
        self.size = len(seq)
        self.depth = self.size.bit_length()
        self.build(seq)

    def build(self, seq):
        data = seq
        size = self.size
        for i, x in enumerate(data):
            j = i + (i & (-i))
            if j < size:
                data[j] += data[i]
        self.data = data

    def __repr__(self):
        return self.data.__repr__()

    def get_sum(self, i):
        data = self.data
        s = 0
        while i:
            s += data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        data = self.data
        size = self.size
        while i < size:
            data[i] += x
            i += i & -i

    def find_kth_element(self, k):
        data = self.data
        size = self.size
        x, sx = 0, 0
        dx = 1 << (self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= size:
                continue
            y = x + dx
            sy = sx + data[y]
            if sy < k:
                x, sx = y, sy
        return x + 1


bit = BinaryIndexedTree([0] * (N + N + N + N + 10))
for t, (x, y, z) in enumerate(zip(X, Y, Z)):
    if x == b'L':
        n = N + N - 1 - y
        n -= t
        n %= (N + N)
        bit.add(n + 1, z)
        bit.add(n + N + N + 1, z)
    elif x == b'R':
        n = y - t
        n %= (N + N)
        bit.add(n + 1, z)
        bit.add(n + N + N + 1, z)
    elif x == b'C':
        cnt = 0
        z -= 1
        for y1, z1 in ((y, z), (N + N - 1 - z, N + N - 1 - y)):
            y1 -= t
            z1 -= t
            q = y1 // (N + N)
            y1 -= q * (N + N)
            z1 -= q * (N + N)
            cnt += bit.get_sum(z1 + 1) - bit.get_sum(y1)
        print(cnt)
0