結果
問題 | No.1332 Range Nearest Query |
ユーザー | mkawa2 |
提出日時 | 2021-01-09 15:54:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 2,112 ms / 2,500 ms |
コード長 | 5,931 bytes |
コンパイル時間 | 172 ms |
コンパイル使用メモリ | 82,252 KB |
実行使用メモリ | 151,232 KB |
最終ジャッジ日時 | 2024-11-17 20:29:16 |
合計ジャッジ時間 | 60,106 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
53,888 KB |
testcase_01 | AC | 39 ms
53,248 KB |
testcase_02 | AC | 39 ms
53,376 KB |
testcase_03 | AC | 1,862 ms
131,896 KB |
testcase_04 | AC | 1,805 ms
131,508 KB |
testcase_05 | AC | 1,823 ms
131,768 KB |
testcase_06 | AC | 939 ms
146,304 KB |
testcase_07 | AC | 934 ms
146,400 KB |
testcase_08 | AC | 950 ms
146,340 KB |
testcase_09 | AC | 963 ms
146,168 KB |
testcase_10 | AC | 934 ms
146,300 KB |
testcase_11 | AC | 954 ms
146,428 KB |
testcase_12 | AC | 954 ms
146,048 KB |
testcase_13 | AC | 984 ms
146,048 KB |
testcase_14 | AC | 956 ms
146,316 KB |
testcase_15 | AC | 962 ms
146,484 KB |
testcase_16 | AC | 2,045 ms
146,300 KB |
testcase_17 | AC | 2,043 ms
146,536 KB |
testcase_18 | AC | 2,042 ms
146,568 KB |
testcase_19 | AC | 2,048 ms
146,812 KB |
testcase_20 | AC | 2,008 ms
146,588 KB |
testcase_21 | AC | 2,041 ms
147,068 KB |
testcase_22 | AC | 2,112 ms
146,556 KB |
testcase_23 | AC | 2,060 ms
146,820 KB |
testcase_24 | AC | 2,058 ms
146,680 KB |
testcase_25 | AC | 2,109 ms
146,808 KB |
testcase_26 | AC | 446 ms
143,028 KB |
testcase_27 | AC | 448 ms
144,400 KB |
testcase_28 | AC | 516 ms
98,536 KB |
testcase_29 | AC | 585 ms
99,200 KB |
testcase_30 | AC | 626 ms
98,944 KB |
testcase_31 | AC | 330 ms
95,104 KB |
testcase_32 | AC | 616 ms
98,432 KB |
testcase_33 | AC | 618 ms
98,432 KB |
testcase_34 | AC | 452 ms
97,920 KB |
testcase_35 | AC | 547 ms
98,816 KB |
testcase_36 | AC | 544 ms
97,536 KB |
testcase_37 | AC | 595 ms
99,200 KB |
testcase_38 | AC | 1,419 ms
115,272 KB |
testcase_39 | AC | 796 ms
99,584 KB |
testcase_40 | AC | 2,015 ms
151,232 KB |
testcase_41 | AC | 1,028 ms
103,068 KB |
testcase_42 | AC | 1,374 ms
113,640 KB |
testcase_43 | AC | 1,185 ms
106,112 KB |
testcase_44 | AC | 1,733 ms
118,356 KB |
testcase_45 | AC | 1,591 ms
116,440 KB |
testcase_46 | AC | 1,352 ms
108,920 KB |
testcase_47 | AC | 1,535 ms
116,272 KB |
ソースコード
import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def MI1(): return map(int1, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)]] inf = 10**16 # md = 998244353 md = 10**9+7 class SegtreeMin(): def __init__(self, n): self.inf = 10**16 aa = [] if not isinstance(n, int): aa = n n = len(aa) self.tree_width = tw = 1 << (n-1).bit_length() self.tree = [self.inf]*(tw*2-1) if aa: self.tree[tw-1:tw-1+len(aa)] = aa for i in range(tw-2, -1, -1): self.tree[i] = min(self.tree[i*2+1], self.tree[i*2+2]) def update(self, i, a): seg_i = self.tree_width-1+i self.tree[seg_i] = a while seg_i != 0: seg_i = (seg_i-1)//2 self.tree[seg_i] = min(self.tree[seg_i*2+1], self.tree[seg_i*2+2]) def element(self, i): return self.tree[self.tree_width-1+i] # [l,r)の最小値 def min(self, l, r): res = self.inf stack = [(0, 0, self.tree_width)] while stack: seg_i, segL, segR = stack.pop() if l <= segL and segR <= r: if self.tree[seg_i] < res: res = self.tree[seg_i] else: segM = (segL+segR)//2 if r > segL and segM > l: stack.append((seg_i*2+1, segL, segM)) if r > segM and segR > l: stack.append((seg_i*2+2, segM, segR)) return res # [l,r)でaより小さい要素のうち最右のインデックス def LessR(self, l, r, a): stack = [(0, 0, self.tree_width)] while stack: u, ul, ur = stack.pop() if ur-ul == 1: return u-self.tree_width+1 um = (ul+ur)//2 if self.tree[u*2+1] < a and ul < r and l < um: stack.append((u*2+1, ul, um)) if self.tree[u*2+2] < a and um < r and l < ur: stack.append((u*2+2, um, ur)) return -1 # [l,r)でaより小さい要素のうち最左のインデックス def LessL(self, l, r, a): stack = [(0, 0, self.tree_width)] while stack: u, ul, ur = stack.pop() if ur-ul == 1: return u-self.tree_width+1 um = (ul+ur)//2 if self.tree[u*2+2] < a and um < r and l < ur: stack.append((u*2+2, um, ur)) if self.tree[u*2+1] < a and ul < r and l < um: stack.append((u*2+1, ul, um)) return -1 class SegtreeMax(): def __init__(self, n): self.inf = 10**16 aa = [] if not isinstance(n, int): aa = n n = len(aa) self.tree_width = tw = 1 << (n-1).bit_length() self.tree = [-self.inf]*(tw*2-1) if aa: self.tree[tw-1:tw-1+len(aa)] = aa for i in range(tw-2, -1, -1): self.tree[i] = max(self.tree[i*2+1], self.tree[i*2+2]) def update(self, i, a): seg_i = self.tree_width-1+i self.tree[seg_i] = a while seg_i != 0: seg_i = (seg_i-1)//2 self.tree[seg_i] = max(self.tree[seg_i*2+1], self.tree[seg_i*2+2]) def element(self, i): return self.tree[self.tree_width-1+i] # [l,r)の最小値 def max(self, l, r): res = -self.inf stack = [(0, 0, self.tree_width)] while stack: seg_i, segL, segR = stack.pop() if l <= segL and segR <= r: if self.tree[seg_i] > res: res = self.tree[seg_i] else: segM = (segL+segR)//2 if r > segL and segM > l: stack.append((seg_i*2+1, segL, segM)) if r > segM and segR > l: stack.append((seg_i*2+2, segM, segR)) return res # [l,r)でaより大きい要素のうち最右のインデックス def OverR(self, l, r, a): stack = [(0, 0, self.tree_width)] while stack: u, ul, ur = stack.pop() if ur-ul == 1: return u-self.tree_width+1 um = (ul+ur)//2 if self.tree[u*2+1] > a and ul < r and l < um: stack.append((u*2+1, ul, um)) if self.tree[u*2+2] > a and um < r and l < ur: stack.append((u*2+2, um, ur)) return -1 # [l,r)でaより大きい要素のうち最左のインデックス def OverL(self, l, r, a): stack = [(0, 0, self.tree_width)] while stack: u, ul, ur = stack.pop() if ur-ul == 1: return u-self.tree_width+1 um = (ul+ur)//2 if self.tree[u*2+2] > a and um < r and l < ur: stack.append((u*2+2, um, ur)) if self.tree[u*2+1] > a and ul < r and l < um: stack.append((u*2+1, ul, um)) return -1 n=II() xx=LI() q=II() xlri=[] for i in range(q): l,r,x=MI() l-=1 xlri.append((x,l,r,i)) xlri.sort() xj=[(x,j) for j,x in enumerate(xx)] xj.sort() ans=[-1]*q mx=SegtreeMax(n) p=0 for x,l,r,i in xlri: while p<n and xj[p][0]<=x: x0,j=xj[p] mx.update(j,x0) p+=1 ans[i]=x-mx.max(l,r) mn=SegtreeMin(n) p=n-1 for x,l,r,i in xlri[::-1]: while p>=0 and xj[p][0]>=x: x0,j=xj[p] mn.update(j,x0) p-=1 cur=mn.min(l,r)-x if cur<ans[i]:ans[i]=cur print(*ans,sep="\n")