結果

問題 No.404 部分門松列
ユーザー maspymaspy
提出日時 2020-04-09 01:22:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 708 ms / 2,000 ms
コード長 2,504 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 176,308 KB
最終ジャッジ日時 2024-07-19 05:58:37
合計ジャッジ時間 14,425 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,864 KB
testcase_01 AC 39 ms
52,736 KB
testcase_02 AC 37 ms
52,608 KB
testcase_03 AC 42 ms
52,352 KB
testcase_04 AC 48 ms
59,648 KB
testcase_05 AC 50 ms
60,032 KB
testcase_06 AC 46 ms
59,776 KB
testcase_07 AC 47 ms
59,904 KB
testcase_08 AC 46 ms
59,776 KB
testcase_09 AC 46 ms
59,776 KB
testcase_10 AC 46 ms
59,776 KB
testcase_11 AC 47 ms
60,160 KB
testcase_12 AC 47 ms
60,032 KB
testcase_13 AC 412 ms
116,592 KB
testcase_14 AC 255 ms
131,644 KB
testcase_15 AC 259 ms
116,328 KB
testcase_16 AC 362 ms
114,944 KB
testcase_17 AC 499 ms
137,068 KB
testcase_18 AC 272 ms
119,808 KB
testcase_19 AC 246 ms
101,100 KB
testcase_20 AC 302 ms
141,644 KB
testcase_21 AC 438 ms
131,956 KB
testcase_22 AC 318 ms
129,920 KB
testcase_23 AC 387 ms
145,884 KB
testcase_24 AC 486 ms
140,264 KB
testcase_25 AC 708 ms
176,308 KB
testcase_26 AC 663 ms
145,992 KB
testcase_27 AC 217 ms
113,152 KB
testcase_28 AC 451 ms
127,824 KB
testcase_29 AC 547 ms
138,948 KB
testcase_30 AC 371 ms
123,720 KB
testcase_31 AC 183 ms
97,728 KB
testcase_32 AC 483 ms
138,132 KB
testcase_33 AC 473 ms
131,056 KB
testcase_34 AC 348 ms
125,124 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