結果
問題 | No.2743 Twisted Lattice |
ユーザー | PNJ |
提出日時 | 2024-04-21 00:03:41 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,959 ms / 3,000 ms |
コード長 | 3,587 bytes |
コンパイル時間 | 178 ms |
コンパイル使用メモリ | 81,884 KB |
実行使用メモリ | 270,128 KB |
最終ジャッジ日時 | 2024-10-12 22:22:49 |
合計ジャッジ時間 | 16,650 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,909 ms
262,252 KB |
testcase_01 | AC | 538 ms
93,248 KB |
testcase_02 | AC | 498 ms
95,232 KB |
testcase_03 | AC | 1,901 ms
262,060 KB |
testcase_04 | AC | 1,959 ms
270,128 KB |
testcase_05 | AC | 1,914 ms
269,620 KB |
testcase_06 | AC | 1,940 ms
264,180 KB |
testcase_07 | AC | 1,905 ms
269,656 KB |
testcase_08 | AC | 41 ms
53,936 KB |
testcase_09 | AC | 41 ms
53,560 KB |
ソースコード
class segtree(): n=1 size=1 log=2 d=[0] op=None e=10**15 def __init__(self,V,OP,E): self.n=len(V) self.op=OP self.e=E self.log=(self.n-1).bit_length() self.size=1<<self.log self.d=[E for i in range(2*self.size)] for i in range(self.n): self.d[self.size+i]=V[i] for i in range(self.size-1,0,-1): self.update(i) def set(self,p,x): assert 0<=p and p<self.n p+=self.size self.d[p]=x for i in range(1,self.log+1): self.update(p>>i) def get(self,p): assert 0<=p and p<self.n return self.d[p+self.size] def prod(self,l,r): assert 0<=l and l<=r and r<=self.n sml=self.e smr=self.e l+=self.size r+=self.size while(l<r): if (l&1): sml=self.op(sml,self.d[l]) l+=1 if (r&1): smr=self.op(self.d[r-1],smr) r-=1 l>>=1 r>>=1 return self.op(sml,smr) def all_prod(self): return self.d[1] def max_right(self,l,f): assert 0<=l and l<=self.n assert f(self.e) if l==self.n: return self.n l+=self.size sm=self.e while(1): while(l%2==0): l>>=1 if not(f(self.op(sm,self.d[l]))): while(l<self.size): l=2*l if f(self.op(sm,self.d[l])): sm=self.op(sm,self.d[l]) l+=1 return l-self.size sm=self.op(sm,self.d[l]) l+=1 if (l&-l)==l: break return self.n def min_left(self,r,f): assert 0<=r and r<=self.n assert f(self.e) if r==0: return 0 r+=self.size sm=self.e while(1): r-=1 while(r>1 and (r%2)): r>>=1 if not(f(self.op(self.d[r],sm))): while(r<self.size): r=(2*r+1) if f(self.op(self.d[r],sm)): sm=self.op(self.d[r],sm) r-=1 return r+1-self.size sm=self.op(self.d[r],sm) if (r& -r)==r: break return 0 def update(self,k): self.d[k]=self.op(self.d[2*k],self.d[2*k+1]) def __str__(self): return str([self.get(i) for i in range(self.n)]) import bisect inf = 10 ** 15 def solve(a,b): ans = inf x = d[b] P = inf for j in range(x-1,x+2,2): P = min(P,G[j][1]) P = max(P,a) ans = P hh = G[x][bisect.bisect_left(G[x],a) - 1] ans = min(ans,a - hh) hh = G[x][bisect.bisect_right(G[x],a)] ans = min(ans,hh - a) res = seg.prod(x+2,m) + a - b res = min(res,segg.prod(0,x) + a + b) ans = min(ans,res-2) return ans H,W,N = map(int,input().split()) A,B = [0]*N,[0]*N T = set() for i in range(N): A[i],B[i] = map(int,input().split()) T.add(B[i] - 1) T.add(B[i]) T.add(B[i] + 1) T = list(T) T.sort() m = len(T) d = {} for i in range(m): d[T[i]] = i G = [[-2*inf,2*inf] for i in range(m)] for i in range(N): a,b = A[i],B[i] G[d[b]].append(a) for i in range(m): G[i].sort() seg = segtree([2*inf for i in range(m + 1)],min,10*inf) segg = segtree([2*inf for i in range(m + 1)],min,10*inf) for i in range(m): r = G[i][1] + T[i] seg.set(i,r) r = G[i][1] - T[i] segg.set(i,r) for i in range(N): print(solve(A[i],B[i]))