結果

問題 No.1332 Range Nearest Query
ユーザー aaaaaaaaaa2230
提出日時 2021-01-08 22:55:47
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,464 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 434,632 KB
最終ジャッジ日時 2024-11-16 14:51:45
合計ジャッジ時間 85,809 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34 TLE * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from bisect import bisect_left

class SegTree:
    """ define what you want to do with 0 index, ex) size = tree_size, func = min or max, sta = default_value """
    
    def __init__(self,size):
        self.n = size
        self.size = 1 << size.bit_length()
        self.tree = [[-10**10,10**10]]*(2*self.size)

    def build(self, list):
        """ set list and update tree"""
        for i,x in enumerate(list,self.size):
            self.tree[i] = sorted(self.tree[i]+[x])

        for i in range(self.size-1,0,-1):
            self.tree[i] = sorted(self.tree[i<<1]+self.tree[i<<1 | 1])
  
    def get(self,l,r,x):
        """ take the value of [l r) with func (min or max)"""
        l += self.size
        r += self.size
        res = 10**10

        while l < r:
            if l & 1:
                t = bisect_left(self.tree[l],x)
                res = min(res,x-self.tree[l][t-1],self.tree[l][t]-x)
                l += 1
            if r & 1:
                t = bisect_left(self.tree[r-1],x)
                res = min(res,x-self.tree[r-1][t-1],self.tree[r-1][t]-x)
            l >>= 1
            r >>= 1
            if res == 0:
                return res
        return res

n = int(input())
X = list(map(int,input().split()))
Q = int(input())
q = [tuple(map(int,input().split())) for i in range(Q)]
seg = SegTree(n)
seg.build(X)
ans = []
for l,r,x in q:
    ans.append(seg.get(l-1,r,x))

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