結果

問題 No.2077 Get Minimum Algorithm
ユーザー chineristACchineristAC
提出日時 2022-01-04 02:57:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,116 ms / 3,000 ms
コード長 3,294 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 82,828 KB
実行使用メモリ 124,236 KB
最終ジャッジ日時 2024-06-01 11:38:10
合計ジャッジ時間 28,405 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
57,460 KB
testcase_01 AC 45 ms
57,764 KB
testcase_02 AC 46 ms
57,448 KB
testcase_03 AC 110 ms
78,204 KB
testcase_04 AC 124 ms
79,000 KB
testcase_05 AC 115 ms
78,832 KB
testcase_06 AC 110 ms
78,220 KB
testcase_07 AC 113 ms
78,324 KB
testcase_08 AC 130 ms
78,824 KB
testcase_09 AC 112 ms
79,696 KB
testcase_10 AC 103 ms
78,524 KB
testcase_11 AC 112 ms
79,648 KB
testcase_12 AC 1,090 ms
120,712 KB
testcase_13 AC 1,114 ms
121,800 KB
testcase_14 AC 1,116 ms
120,912 KB
testcase_15 AC 1,099 ms
120,884 KB
testcase_16 AC 1,099 ms
120,920 KB
testcase_17 AC 1,115 ms
120,504 KB
testcase_18 AC 1,107 ms
121,100 KB
testcase_19 AC 1,103 ms
120,984 KB
testcase_20 AC 1,096 ms
120,468 KB
testcase_21 AC 1,092 ms
120,644 KB
testcase_22 AC 799 ms
121,680 KB
testcase_23 AC 774 ms
121,808 KB
testcase_24 AC 756 ms
121,652 KB
testcase_25 AC 803 ms
122,036 KB
testcase_26 AC 793 ms
121,808 KB
testcase_27 AC 910 ms
118,540 KB
testcase_28 AC 686 ms
109,704 KB
testcase_29 AC 443 ms
101,144 KB
testcase_30 AC 823 ms
117,568 KB
testcase_31 AC 650 ms
107,688 KB
testcase_32 AC 481 ms
100,184 KB
testcase_33 AC 572 ms
120,084 KB
testcase_34 AC 418 ms
109,732 KB
testcase_35 AC 302 ms
100,656 KB
testcase_36 AC 735 ms
124,236 KB
testcase_37 AC 962 ms
121,144 KB
testcase_38 AC 45 ms
56,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self, init_val, segfunc, ide_ele):
        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
        self.size = n
        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] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        k += self.num
        self.tree[k] = self.segfunc(self.tree[k],x)
        while k > 1:
            k >>= 1
            self.tree[k] = self.segfunc(self.tree[2*k], self.tree[2*k+1])

    def query(self, l, r):
        if r==self.size:
            r = self.num

        res = self.ide_ele

        l += self.num
        r += self.num
        right = []
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                right.append(self.tree[r-1])
            l >>= 1
            r >>= 1

        for e in right[::-1]:
            res = self.segfunc(res,e)
        return res
    
    def bisect(self,l,r,x):
        if r==self.size:
            r = self.num
        l += self.num
        r += self.num
        segment = []
        segment_r = []
        while l<r:
            if l&1:
                segment.append(l)
                l += 1
            if r&1:
                segment_r.append(r-1)
            l >>= 1
            r >>= 1
        segment += segment_r[::-1]
        tmp = self.ide_ele
        for pos in segment:
            check = self.segfunc(tmp,self.tree[pos])
            if check >= x:
                break
            else:
                tmp = check
        else:
            return -1
        
        while pos < self.num:
            check = self.segfunc(tmp,self.tree[2*pos])
            if check >= x:
                pos = 2 * pos
            else:
                tmp = check
                pos = 2 * pos + 1
        return pos - self.num

import sys,random
from collections import deque

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

N = int(input())
P = li()
Q = int(input())
query = [[] for i in range(N+1)]
for i in range(Q):
    x,y = mi()
    query[x].append((y,i))

pos = [-1] * (N+1)
for i in range(N):
    pos[P[i]] = i

fw = SegmentTree([0]*(N+1),lambda x,y:x+y,0)
add_time = [[] for i in range(N+1)]
for i in range(N):
    t = fw.query(0,P[i])
    add_time[t].append(i)
    fw.update(P[i],1)

idx_set = SegmentTree([0]*(N+1),lambda x,y:x+y,0)
val_set = SegmentTree([0]*(N+1),lambda x,y:x+y,0)
cond = [0] * (N+1)
n = 0
for i in add_time[0]:
    idx_set.update(i,1)
    val_set.update(P[i],1)
    cond[P[i]] = 1
    n += 1

ans = [-1]*Q
for t in range(1,N+1):
    idx_set.update(t-1,-1)
    val_set.update(t,-1)
    cond[t] = 2
    n -= 1
    for i in add_time[t]:
        idx_set.update(i,1)
        val_set.update(P[i],1)
        cond[P[i]] = 1
        n += 1
    for y,idx in query[t]:
        if cond[y]==0:
            ans[idx] = pos[y] + 1
        elif cond[y]==1:
            k = val_set.query(0,y+1)
            ans[idx] = idx_set.bisect(0,N+1,n-k+1) + 1

print(*ans,sep="\n")
0