結果
問題 | No.404 部分門松列 |
ユーザー | 已经死了 |
提出日時 | 2024-06-29 14:24:03 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,681 ms / 2,000 ms |
コード長 | 3,995 bytes |
コンパイル時間 | 186 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 322,732 KB |
最終ジャッジ日時 | 2024-06-29 14:24:41 |
合計ジャッジ時間 | 27,814 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 143 ms
92,160 KB |
testcase_01 | AC | 144 ms
92,416 KB |
testcase_02 | AC | 153 ms
92,416 KB |
testcase_03 | AC | 142 ms
92,288 KB |
testcase_04 | AC | 149 ms
92,672 KB |
testcase_05 | AC | 150 ms
92,928 KB |
testcase_06 | AC | 167 ms
92,672 KB |
testcase_07 | AC | 149 ms
92,672 KB |
testcase_08 | AC | 160 ms
92,672 KB |
testcase_09 | AC | 153 ms
92,672 KB |
testcase_10 | AC | 152 ms
92,800 KB |
testcase_11 | AC | 150 ms
92,672 KB |
testcase_12 | AC | 151 ms
92,800 KB |
testcase_13 | AC | 905 ms
240,012 KB |
testcase_14 | AC | 866 ms
263,888 KB |
testcase_15 | AC | 710 ms
209,216 KB |
testcase_16 | AC | 1,030 ms
166,404 KB |
testcase_17 | AC | 1,252 ms
263,408 KB |
testcase_18 | AC | 686 ms
224,144 KB |
testcase_19 | AC | 568 ms
163,408 KB |
testcase_20 | AC | 960 ms
251,908 KB |
testcase_21 | AC | 1,061 ms
264,080 KB |
testcase_22 | AC | 762 ms
263,024 KB |
testcase_23 | AC | 991 ms
261,912 KB |
testcase_24 | AC | 1,176 ms
260,804 KB |
testcase_25 | AC | 1,681 ms
322,732 KB |
testcase_26 | AC | 1,568 ms
266,224 KB |
testcase_27 | AC | 571 ms
189,764 KB |
testcase_28 | AC | 916 ms
264,392 KB |
testcase_29 | AC | 1,224 ms
263,776 KB |
testcase_30 | AC | 797 ms
259,676 KB |
testcase_31 | AC | 386 ms
159,016 KB |
testcase_32 | AC | 1,119 ms
261,832 KB |
testcase_33 | AC | 1,179 ms
263,284 KB |
testcase_34 | AC | 1,135 ms
262,508 KB |
ソースコード
import os,sys,random,threading #sys.exit() 退出程序 from random import randint,choice,shuffle #randint(a,b)从[a,b]范围随机选择一个数 #choice(seq)seq可以是一个列表,元组或字符串,从seq中随机选取一个元素 #shuffle(x)将一个可变的序列x中的元素打乱 from copy import deepcopy from io import BytesIO,IOBase from types import GeneratorType from functools import lru_cache,reduce #reduce(op,迭代对象) from bisect import bisect_left,bisect_right #bisect_left(x) 大于等于x的第一个下标 #bisect_right(x) 大于x的第一个下标 from collections import Counter,defaultdict,deque from itertools import accumulate,combinations,permutations #accumulate(a)用a序列生成一个累积迭代器,一般list化前面放个[0]做前缀和用 #combinations(a,k)a序列选k个 组合迭代器 #permutations(a,k)a序列选k个 排列迭代器 from heapq import heapify,heappop,heappush #heapify将列表转为堆 from typing import Generic,Iterable,Iterator,TypeVar,Union,List from string import ascii_lowercase,ascii_uppercase,digits #小写字母,大写字母,十进制数字 from math import ceil,floor,sqrt,pi,factorial,gcd,log,log10,log2,inf #ceil向上取整,floor向下取整 ,sqrt开方 ,factorial阶乘 from decimal import Decimal,getcontext #Decimal(s) 实例化Decimal对象,一般使用字符串 #getcontext().prec=100 修改精度 from sys import stdin, stdout, setrecursionlimit input = lambda: sys.stdin.readline().rstrip("\r\n") MI = lambda :map(int,input().split()) li = lambda :list(MI()) ii = lambda :int(input()) mod = int(1e9 + 7) #998244353 inf = int(1e20) py = lambda :print("YES") pn = lambda :print("NO") DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 右下左上 DIRS8 = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),(-1, 1)] # →↘↓↙←↖↑↗ class BIT: """单点修改,区间和查询""" __slots__ = "size", "bit", "tree" def __init__(self, n: int): self.size = n self.bit = n.bit_length() self.tree = [0]*(n+1) def add(self, index: int, delta: int) -> None: # index 必须大于0 while index <= self.size: self.tree[index]+=delta index += index & -index def _query(self, index: int) -> int: res = 0 while index > 0: res += self.tree[index] index -= index & -index return res def query(self, left: int, right: int) -> int: return self._query(right) - self._query(left - 1) def bisectLeft(self, k: int) -> int: """返回第一个前缀和大于等于k的位置pos 1 <= pos <= self.size + 1 """ curSum, pos = 0, 0 for i in range(self.bit, -1, -1): nextPos = pos + (1 << i) if nextPos <= self.size and curSum + self.tree[nextPos] < k: pos = nextPos curSum += self.tree[pos] return pos + 1 n=ii() res=[0]*n a=li() sa=sorted(set(a)) d={x:i+1 for i,x in enumerate(sa)} bit=BIT(len(d)+5) ls=[0]*n lb=[0]*n for i in range(n): ls[i]=bit._query(d[a[i]]-1) lb[i]=bit.query(d[a[i]]+1,len(d)+3) bit.add(d[a[i]],1) bit=BIT(len(d)+5) rs=[0]*n rb=[0]*n for i in range(n-1,-1,-1): rs[i]=bit._query(d[a[i]]-1) rb[i]=bit.query(d[a[i]]+1,len(d)+3) bit.add(d[a[i]],1) for i in range(n): res[i]=ls[i]*rs[i]+lb[i]*rb[i] bit=BIT(len(d)+5) left=Counter() right=Counter(a) for idx,i in enumerate(a): bit.add(d[i],-left[i]*right[i]) right[i]-=1 bit.add(d[i],left[i]*right[i]) res[idx]-=bit.query(1,len(d)+2)-bit.query(d[i],d[i]) bit.add(d[i],-left[i]*right[i]) left[i]+=1 bit.add(d[i],left[i]*right[i]) d=sa[:] q=ii() query=[] for _ in range(q): l,r=li() d+=[l,r] query.append([l,r]) d=sorted(set(d)) d={x:i+1 for i,x in enumerate(d)} bit=BIT(len(d)+5) for i in range(n): bit.add(d[a[i]],res[i]) for l,r in query: print(bit.query(d[l],d[r]))