結果

問題 No.728 ギブ and テイク
ユーザー maspymaspy
提出日時 2020-03-27 08:41:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 989 ms / 3,000 ms
コード長 1,815 bytes
コンパイル時間 1,997 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 190,680 KB
最終ジャッジ日時 2023-08-30 16:10:59
合計ジャッジ時間 13,536 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,468 KB
testcase_01 AC 75 ms
71,420 KB
testcase_02 AC 75 ms
71,420 KB
testcase_03 AC 75 ms
71,568 KB
testcase_04 AC 76 ms
71,412 KB
testcase_05 AC 75 ms
71,272 KB
testcase_06 AC 75 ms
71,344 KB
testcase_07 AC 75 ms
71,612 KB
testcase_08 AC 79 ms
75,336 KB
testcase_09 AC 75 ms
71,492 KB
testcase_10 AC 75 ms
71,224 KB
testcase_11 AC 84 ms
76,020 KB
testcase_12 AC 86 ms
75,884 KB
testcase_13 AC 204 ms
82,952 KB
testcase_14 AC 229 ms
88,232 KB
testcase_15 AC 177 ms
81,684 KB
testcase_16 AC 222 ms
86,924 KB
testcase_17 AC 217 ms
86,224 KB
testcase_18 AC 745 ms
169,424 KB
testcase_19 AC 785 ms
172,868 KB
testcase_20 AC 835 ms
172,900 KB
testcase_21 AC 755 ms
179,780 KB
testcase_22 AC 394 ms
108,496 KB
testcase_23 AC 326 ms
103,600 KB
testcase_24 AC 617 ms
132,900 KB
testcase_25 AC 616 ms
133,176 KB
testcase_26 AC 399 ms
107,580 KB
testcase_27 AC 989 ms
188,116 KB
testcase_28 AC 426 ms
190,680 KB
testcase_29 AC 76 ms
71,608 KB
testcase_30 AC 74 ms
71,292 KB
testcase_31 AC 76 ms
71,424 KB
testcase_32 AC 75 ms
71,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from bisect import bisect_left, bisect_right


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


N = int(readline())
A = tuple(map(int, readline().split()))
m = map(int, read().split())
LR = zip(m, m)

remove_x = [[] for _ in range(N + 10)]
bit = BinaryIndexedTree([0] * (N + 10))
answer = 0
total = 0
for i, (a, (l, r)) in enumerate(zip(A, LR), 1):
    for x in remove_x[i]:
        bit.add(x, -1)
        total -= 1
    n = bisect_left(A, a - l)
    m = bisect_right(A, a + r) + 1
    answer += total - bit.get_sum(n)
    remove_x[m].append(i)
    bit.add(i, 1)
    total += 1

print(answer)
0