結果
| 問題 |
No.2796 Small Matryoshka
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-06-28 22:44:54 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,123 ms / 2,000 ms |
| コード長 | 2,438 bytes |
| コンパイル時間 | 153 ms |
| コンパイル使用メモリ | 82,164 KB |
| 実行使用メモリ | 116,036 KB |
| 最終ジャッジ日時 | 2024-06-28 22:45:07 |
| 合計ジャッジ時間 | 12,719 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
from bisect import bisect_left as lower_bound
from bisect import bisect_right as upper_bound
class FenwickTree:
def __init__(self,x):
bit=self.bit=list(x)
size=self.size=len(bit)
for i in range(size):
j=i|(i+1)
if j<size:
bit[j]+=bit[i]
def update(self,idx,x):
"""updates bit[idx]+=x"""
while idx<self.size:
self.bit[idx]+=x
idx|=idx+1
def __call__(self,end):
"""calc sum(bit[:end])"""
x=0
while end:
x+=self.bit[end-1]
end&=end-1
return x
def find_kth(self,k):
"""Find largest idx such that sum(bit[:idx])<=k"""
idx=-1
for d in reversed(range(self.size.bit_length())):
right_idx=idx+(1<<d)
if right_idx<self.size and self.bit[right_idx]<=k:
idx=right_idx
k-=self.bit[idx]
return idx+1,k
class SortedList:
block_size=700
def __init__(self,iterable=()):
self.macro=[]
self.micros=[[]]
self.micro_size=[0]
self.fenwick=FenwickTree([0])
self.size=0
for item in iterable:
self.insert(item)
def insert(self,x):
i=lower_bound(self.macro,x)
j=upper_bound(self.micros[i],x)
self.micros[i].insert(j,x)
self.size+=1
self.micro_size[i]+=1
self.fenwick.update(i,1)
if len(self.micros[i]) >= self.block_size:
self.micros[i:i+1]=self.micros[i][:self.block_size >> 1],self.micros[i][self.block_size >> 1:]
self.micro_size[i:i+1]=self.block_size >> 1,self.block_size >> 1
self.fenwick=FenwickTree(self.micro_size)
self.macro.insert(i,self.micros[i+1][0])
def pop(self,k=-1):
i,j=self._find_kth(k)
self.size-=1
self.micro_size[i]-=1
self.fenwick.update(i,-1)
return self.micros[i].pop(j)
def __getitem__(self,k):
i,j=self._find_kth(k)
return self.micros[i][j]
def count(self,x):
return self.upper_bound(x)-self.lower_bound(x)
def __contains__(self,x):
return self.count(x)>0
def lower_bound(self,x):
i=lower_bound(self.macro,x)
return self.fenwick(i)+lower_bound(self.micros[i],x)
def upper_bound(self,x):
i=upper_bound(self.macro,x)
return self.fenwick(i)+upper_bound(self.micros[i],x)
def _find_kth(self,k):
return self.fenwick.find_kth(k+self.size if k<0 else k)
def __len__(self):
return self.size
def __iter__(self):
return (x for micro in self.micros for x in micro)
def __repr__(self):
return str(list(self))
(n,),*e=[[*map(int,s.split())]for s in open(0)]
p=SortedList()
for r,R in sorted(e):
i=p.upper_bound(r)-1
if 0<=i<len(p):
p.pop(i)
p.insert(R)
print(len(p)-1)