結果
問題 | No.1332 Range Nearest Query |
ユーザー | penguinman |
提出日時 | 2020-11-23 05:40:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,583 ms / 2,500 ms |
コード長 | 3,511 bytes |
コンパイル時間 | 323 ms |
コンパイル使用メモリ | 82,480 KB |
実行使用メモリ | 158,192 KB |
最終ジャッジ日時 | 2024-07-23 17:18:29 |
合計ジャッジ時間 | 46,843 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
52,736 KB |
testcase_01 | AC | 40 ms
52,608 KB |
testcase_02 | AC | 39 ms
52,864 KB |
testcase_03 | AC | 1,408 ms
150,912 KB |
testcase_04 | AC | 1,419 ms
150,884 KB |
testcase_05 | AC | 1,398 ms
150,640 KB |
testcase_06 | AC | 791 ms
155,628 KB |
testcase_07 | AC | 789 ms
155,880 KB |
testcase_08 | AC | 771 ms
155,628 KB |
testcase_09 | AC | 776 ms
155,628 KB |
testcase_10 | AC | 768 ms
155,884 KB |
testcase_11 | AC | 775 ms
155,756 KB |
testcase_12 | AC | 774 ms
155,628 KB |
testcase_13 | AC | 760 ms
155,960 KB |
testcase_14 | AC | 776 ms
155,636 KB |
testcase_15 | AC | 777 ms
155,636 KB |
testcase_16 | AC | 1,581 ms
157,684 KB |
testcase_17 | AC | 1,493 ms
157,428 KB |
testcase_18 | AC | 1,516 ms
157,820 KB |
testcase_19 | AC | 1,541 ms
157,772 KB |
testcase_20 | AC | 1,537 ms
158,156 KB |
testcase_21 | AC | 1,520 ms
157,936 KB |
testcase_22 | AC | 1,531 ms
158,192 KB |
testcase_23 | AC | 1,534 ms
157,548 KB |
testcase_24 | AC | 1,529 ms
157,456 KB |
testcase_25 | AC | 1,583 ms
157,948 KB |
testcase_26 | AC | 498 ms
148,672 KB |
testcase_27 | AC | 517 ms
155,848 KB |
testcase_28 | AC | 307 ms
85,888 KB |
testcase_29 | AC | 312 ms
86,332 KB |
testcase_30 | AC | 321 ms
85,744 KB |
testcase_31 | AC | 245 ms
85,504 KB |
testcase_32 | AC | 324 ms
85,296 KB |
testcase_33 | AC | 324 ms
85,800 KB |
testcase_34 | AC | 279 ms
85,376 KB |
testcase_35 | AC | 304 ms
85,632 KB |
testcase_36 | AC | 302 ms
85,428 KB |
testcase_37 | AC | 317 ms
85,492 KB |
testcase_38 | AC | 970 ms
109,640 KB |
testcase_39 | AC | 470 ms
87,440 KB |
testcase_40 | AC | 1,555 ms
155,536 KB |
testcase_41 | AC | 618 ms
92,168 KB |
testcase_42 | AC | 971 ms
114,392 KB |
testcase_43 | AC | 732 ms
98,308 KB |
testcase_44 | AC | 1,147 ms
121,376 KB |
testcase_45 | AC | 1,071 ms
116,080 KB |
testcase_46 | AC | 890 ms
107,788 KB |
testcase_47 | AC | 1,092 ms
110,320 KB |
ソースコード
# Refernce: https://atcoder.jp/contests/practice2/submissions/16814531 class SegTree(): def __init__(self, a, func, idem): # the number of nodes is 2n - 1 self.n = 1 << len(a).bit_length() self.size = len(a) self.func = func self.idem = idem # Identity element self.node = [idem] * (2 * self.n - 1) for i in range(len(a) + self.n - 2, -1, -1): if i >= self.n - 1: self.node[i] = a[i - self.n + 1] else: self.node[i] = self.func(self.node[2 * i + 1], self.node[2 * i + 2]) def get(self, x): return self.node[x + self.n - 1] def set(self, x, val): x += self.n - 1 self.node[x] = val while x > 0: x = (x - 1) >> 1 self.node[x] = self.func(self.node[2*x+1], self.node[2*x+2]) return def prod(self, l, r): L, R = l + self.n, r + self.n s_l, s_r = self.idem, self.idem while L < R: if R & 1: R -= 1 s_r = self.func(self.node[R-1], s_r) if L & 1: s_l = s_l = self.func(s_l, self.node[L-1]) L += 1 L >>= 1 R >>= 1 return self.func(s_l, s_r) def max_right(self, l, val): if l == self.size: return self.size L = l + self.n s = self.idem while 1: while L % 2 == 0: L >>= 1 if not val(self.func(s, self.node[L - 1])): while L < self.n: L <<= 1 if val(self.func(s, self.node[L-1])): s = self.func(s, self.node[L-1]) L += 1 return L - self.n s = self.func(s, self.node[L - 1]) L += 1 if (L & -L) == L: return self.size def min_left(self, r, val): if r == 0: return 0 R = r + self.n s = self.idem while True: R -= 1 while R > 1 and R % 2: R >>= 1 if not val(self.func(self.node[R - 1], s)): while R < self.n: R = (R << 1) + 1 if val(self.func(self.node[R - 1], s)): s = self.func(self.node[R - 1], s) R -= 1 return R + 1 - self.n s = self.func(self.node[R - 1], s) if (R & -R) == R: return 0 import sys input=sys.stdin.readline inf=int(1e9) N=int(input()) X=list(map(int,input().split())) Q=int(input()) #data[i][1] が正ならクエリ、そうでなければ座標 data=[[] for i in range(N+Q)] left=[0]*Q right=[0]*Q for i in range(N): data[i]=(X[i],-i) for i in range(Q): left[i],right[i],x=map(int, input().split()) data[i+N]=(x,i+1) ans=[inf]*Q A=[-inf]*N seg=SegTree(A,max,-inf) data.sort(key=lambda val:val[0]) #前から for i in range(N+Q): if data[i][1]<=0: seg.set(-data[i][1],data[i][0]) else: index=data[i][1]-1 ans[index]=min(ans[index],data[i][0]-seg.prod(left[index]-1,right[index])) seg=SegTree(A,max,-inf) #後ろから for a in range(N+Q): i=N+Q-1-a if data[i][1]<=0: seg.set(-data[i][1],inf-data[i][0]) else: index=data[i][1]-1 ans[index]=min(ans[index],inf-data[i][0]-seg.prod(left[index]-1,right[index])) for i in range(Q): print(ans[i])