結果

問題 No.404 部分門松列
ユーザー maspymaspy
提出日時 2020-04-09 01:22:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 817 ms / 2,000 ms
コード長 2,504 bytes
コンパイル時間 1,367 ms
コンパイル使用メモリ 86,316 KB
実行使用メモリ 173,344 KB
最終ジャッジ日時 2023-09-26 11:18:53
合計ジャッジ時間 21,674 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
71,124 KB
testcase_01 AC 78 ms
71,256 KB
testcase_02 AC 81 ms
71,328 KB
testcase_03 AC 82 ms
70,996 KB
testcase_04 AC 92 ms
75,352 KB
testcase_05 AC 89 ms
75,352 KB
testcase_06 AC 94 ms
75,408 KB
testcase_07 AC 93 ms
75,140 KB
testcase_08 AC 90 ms
75,432 KB
testcase_09 AC 90 ms
75,200 KB
testcase_10 AC 91 ms
75,412 KB
testcase_11 AC 89 ms
75,324 KB
testcase_12 AC 93 ms
75,428 KB
testcase_13 AC 479 ms
125,300 KB
testcase_14 AC 439 ms
138,584 KB
testcase_15 AC 339 ms
110,912 KB
testcase_16 AC 417 ms
120,628 KB
testcase_17 AC 637 ms
142,936 KB
testcase_18 AC 437 ms
120,388 KB
testcase_19 AC 302 ms
99,488 KB
testcase_20 AC 369 ms
149,408 KB
testcase_21 AC 707 ms
121,768 KB
testcase_22 AC 355 ms
125,940 KB
testcase_23 AC 407 ms
158,072 KB
testcase_24 AC 508 ms
144,768 KB
testcase_25 AC 817 ms
173,344 KB
testcase_26 AC 728 ms
160,992 KB
testcase_27 AC 241 ms
114,352 KB
testcase_28 AC 495 ms
134,476 KB
testcase_29 AC 659 ms
144,228 KB
testcase_30 AC 366 ms
132,488 KB
testcase_31 AC 190 ms
99,028 KB
testcase_32 AC 581 ms
133,436 KB
testcase_33 AC 560 ms
137,156 KB
testcase_34 AC 429 ms
131,344 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_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()))
A_nums = [0] + sorted(set(A))
convert = {x: i for i, x in enumerate(A_nums)}
inds = [[] for _ in range(len(A_nums))]
for i, x in enumerate(A, 1):
    r = convert[x]
    inds[r].append(i)


filled = 0
bit = BinaryIndexedTree([0] * (N + 1))
imos = [0] * (N + 1)
count = [0] * (N + 1)

for ind in inds:
    n = len(ind)
    for m, i in enumerate(ind):
        if m < n - 1:
            imos[i + 1] += (m + 1) * (n - m - 1)
        imos[i] -= m * (n - m)
        left = bit.get_sum(i)
        right = filled - left
        count[i] = left * right
        left = i - 1 - m - left
        right = N - filled - n - left
        count[i] += left * right
    for i in ind:
        bit.add(i, 1)
    filled += n
for i in range(1, N + 1):
    imos[i] += imos[i - 1]
    count[i] -= imos[i]

num_to_cnt = [0] * len(A_nums)
for x, y in zip(A, count[1:]):
    r = convert[x]
    num_to_cnt[r] += y
for i in range(1, len(num_to_cnt)):
    num_to_cnt[i] += num_to_cnt[i - 1]

Q = int(readline())
m = map(int, read().split())
for L, H in zip(m, m):
    x = num_to_cnt[bisect_right(A_nums, H) - 1] - num_to_cnt[bisect_right(A_nums, L - 1) - 1]
    print(x)
0