結果
問題 | No.404 部分門松列 |
ユーザー | Navier_Boltzmann |
提出日時 | 2023-06-16 23:48:14 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,344 bytes |
コンパイル時間 | 1,208 ms |
コンパイル使用メモリ | 86,948 KB |
実行使用メモリ | 205,772 KB |
最終ジャッジ日時 | 2023-09-06 22:51:36 |
合計ジャッジ時間 | 30,302 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 99 ms
77,252 KB |
testcase_01 | AC | 102 ms
72,788 KB |
testcase_02 | AC | 100 ms
72,728 KB |
testcase_03 | AC | 101 ms
72,900 KB |
testcase_04 | AC | 135 ms
77,596 KB |
testcase_05 | AC | 129 ms
77,680 KB |
testcase_06 | AC | 139 ms
77,752 KB |
testcase_07 | AC | 132 ms
77,572 KB |
testcase_08 | AC | 135 ms
79,640 KB |
testcase_09 | AC | 139 ms
79,568 KB |
testcase_10 | AC | 137 ms
79,436 KB |
testcase_11 | AC | 137 ms
79,564 KB |
testcase_12 | AC | 133 ms
77,688 KB |
testcase_13 | AC | 1,364 ms
124,256 KB |
testcase_14 | AC | 1,094 ms
108,872 KB |
testcase_15 | AC | 1,302 ms
98,504 KB |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | AC | 611 ms
93,172 KB |
testcase_19 | AC | 1,011 ms
112,088 KB |
testcase_20 | AC | 1,892 ms
143,980 KB |
testcase_21 | TLE | - |
testcase_22 | AC | 686 ms
94,104 KB |
testcase_23 | AC | 1,893 ms
138,520 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 | -- | - |
ソースコード
from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.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))