結果

問題 No.404 部分門松列
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-06-16 23:48:45
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,351 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 87,336 KB
実行使用メモリ 196,996 KB
最終ジャッジ日時 2023-09-06 22:52:07
合計ジャッジ時間 26,518 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
79,964 KB
testcase_01 AC 109 ms
72,756 KB
testcase_02 AC 106 ms
72,652 KB
testcase_03 AC 107 ms
72,768 KB
testcase_04 AC 132 ms
77,704 KB
testcase_05 AC 136 ms
77,552 KB
testcase_06 AC 134 ms
77,548 KB
testcase_07 AC 134 ms
77,336 KB
testcase_08 AC 138 ms
79,576 KB
testcase_09 AC 141 ms
79,484 KB
testcase_10 AC 132 ms
77,692 KB
testcase_11 AC 140 ms
79,644 KB
testcase_12 AC 138 ms
77,740 KB
testcase_13 AC 1,349 ms
123,740 KB
testcase_14 AC 1,065 ms
109,228 KB
testcase_15 AC 1,263 ms
98,460 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 600 ms
93,824 KB
testcase_19 AC 1,012 ms
112,936 KB
testcase_20 AC 1,885 ms
134,916 KB
testcase_21 AC 1,986 ms
152,632 KB
testcase_22 AC 681 ms
94,584 KB
testcase_23 AC 1,870 ms
127,280 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.buffer.readline

N = int(input())
A = list(map(int,input().split()))
class SegTree:
    """
    Segment Tree
    """

    def __init__(self, init_val, segfunc, ide_ele):
        """
        初期化

        init_val: 配列の初期値
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新

        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る

        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res
val = defaultdict(int)
X = [(a,i) for i,a in enumerate(A)]
X.sort()
T = SegTree([0]*N,lambda x,y:x+y,0)
C = Counter(A)
V = SegTree([0]*(N+3),lambda x,y:x+y,0)
stack = []
for i in range(N):
    
    a,idx = X[i]
    stack.append(idx)
    C[a] -= 1
    val[a] += T.query(0,idx)*T.query(idx,N) - V.query(0,idx+1)
    if C[a]!=0:
        continue
    for jdx in stack:
        T.update(jdx,1)
    n = len(stack)
    stack.sort()
    for i in range(n-1):
        l = stack[i]
        r = stack[i+1]
        cnt = (i+1)*(n-1-i)
        V.update(l+1,V.query(l+1,l+2)+cnt)
        V.update(r,V.query(r,r+1)-cnt)
    stack = []
    
X.reverse()
T = SegTree([0]*N,lambda x,y:x+y,0)
C = Counter(A)
V = SegTree([0]*(N+3),lambda x,y:x+y,0)
stack = []
for i in range(N):
    
    a,idx = X[i]
    stack.append(idx)
    C[a] -= 1
    val[a] += T.query(0,idx)*T.query(idx,N) - V.query(0,idx+1)
    if C[a]!=0:
        continue
    for jdx in stack:
        T.update(jdx,1)
    n = len(stack)
    stack.sort()
    for i in range(n-1):
        l = stack[i]
        r = stack[i+1]
        cnt = (i+1)*(n-1-i)
        V.update(l+1,V.query(l+1,l+2)+cnt)
        V.update(r,V.query(r,r+1)-cnt)
    stack = []
def cle(a,D):
    
    y = len(D)-1
    x = 0
    if D[x]>a:
        return 0
        
    if D[y]<=a:
        return y+1
    
    while y-x>1:
        mid = (y+x)//2
        if D[mid]<=a:
            x = mid
        else:
            y = mid
    return y
B = list(set([0]+A))
B.sort()
S = list(accumulate([0]+[val[b] for b in B]))
Q = int(input())
n = len(B)
def count(H):
    
    h = cle(H,B)
    h = min(h,n-1)
    while B[h]>H:
        h -= 1
    return S[h+1]
    
for _ in range(Q):
    l,h = map(int,input().split())
    print(count(h)-count(l-1))
0