結果

問題 No.728 ギブ and テイク
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-05 00:55:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,695 ms / 3,000 ms
コード長 1,444 bytes
コンパイル時間 368 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 151,384 KB
最終ジャッジ日時 2023-09-13 21:25:35
合計ジャッジ時間 17,524 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,100 KB
testcase_01 AC 75 ms
71,400 KB
testcase_02 AC 74 ms
71,292 KB
testcase_03 AC 75 ms
71,052 KB
testcase_04 AC 74 ms
71,224 KB
testcase_05 AC 75 ms
71,400 KB
testcase_06 AC 75 ms
71,340 KB
testcase_07 AC 75 ms
71,052 KB
testcase_08 AC 80 ms
75,932 KB
testcase_09 AC 75 ms
71,052 KB
testcase_10 AC 77 ms
71,352 KB
testcase_11 AC 87 ms
76,068 KB
testcase_12 AC 89 ms
75,828 KB
testcase_13 AC 199 ms
80,228 KB
testcase_14 AC 227 ms
82,912 KB
testcase_15 AC 156 ms
79,332 KB
testcase_16 AC 214 ms
82,460 KB
testcase_17 AC 205 ms
82,300 KB
testcase_18 AC 1,198 ms
115,896 KB
testcase_19 AC 1,272 ms
116,848 KB
testcase_20 AC 1,473 ms
136,904 KB
testcase_21 AC 1,339 ms
129,960 KB
testcase_22 AC 544 ms
91,756 KB
testcase_23 AC 383 ms
85,520 KB
testcase_24 AC 984 ms
106,152 KB
testcase_25 AC 989 ms
106,488 KB
testcase_26 AC 525 ms
90,156 KB
testcase_27 AC 1,695 ms
151,384 KB
testcase_28 AC 797 ms
149,328 KB
testcase_29 AC 75 ms
71,560 KB
testcase_30 AC 76 ms
71,304 KB
testcase_31 AC 73 ms
71,464 KB
testcase_32 AC 75 ms
71,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import sys
input = sys.stdin.buffer.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 = int(input())
A = list(map(int, input().split()))
L = [0]*N
task = []
for i, a in enumerate(A):
    l, r = map(int, input().split())
    L[i] = bisect.bisect_left(A, a - l)
    task.append((a, ~i))
    task.append((a + r, i))
task.sort()

ans = 0
bit = fenwick_tree(N)
for _, i in task:
    if i < 0:
        i = ~i
        l = L[i]
        ans += bit.sum(l, i + 1)
        bit.add(i, 1)
    else:
        bit.add(i, -1)

print(ans)
0