結果

問題 No.2220 Range Insert & Point Mex
ユーザー tassei903
提出日時 2024-09-20 15:44:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 803 ms / 2,000 ms
コード長 2,570 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 82,368 KB
実行使用メモリ 211,028 KB
最終ジャッジ日時 2024-09-20 15:44:50
合計ジャッジ時間 18,491 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

inf = 10**18
class SegmentTree:
    # 初期化処理
    # f : SegmentTreeにのせるモノイド
    # default : fに対する単位元
    def __init__(self, size, f=lambda x,y : min(x,y), default=inf):
        self.size = 2**(size-1).bit_length() # 簡単のため要素数Nを2冪にする
        self.default = default
        self.dat = [default]*(self.size*2) # 要素を単位元で初期化
        self.f = f
 
    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 1:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])
    def updatef(self, i, x):
        i += self.size
        self.dat[i] = self.f(self.dat[i],x)
        while i > 1:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])
 
    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1
 
            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres) # モノイドでは可換律は保証されていないので演算の方向に注意
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res
 
    def query2(self):
        s = 1
        #print(self.size)
        while s<self.size:
            #print(s)
            if self.dat[s*2]>self.dat[s*2+1]:
                s = s*2
            else:
                s = s*2+1
        return s-self.size
from collections import defaultdict

n = int(input())

eve_in = defaultdict(list)
eve_out = defaultdict(list)

for i in range(n):
    l, r, a = map(int, input().split())
    eve_in[l].append(a)
    eve_out[r].append(a)
q = int(input())
eve_que = defaultdict(lambda:-1)
x = list(map(int, input().split()))
for i in range(q):
    eve_que[x[i]] = i

ans = [-1] * q

def f(x, y):
    if x[0] == x[1]:
        return x[0] + y[0], x[1] + y[1]
    return x[0], x[1] + y[1]

st = SegmentTree(n, f, (0, 0))
for i in range(n):
    st.update(i, (0, 1))
cnt = [0] * n

for x in sorted(set(list(eve_in)+list(eve_out)+list(eve_que))):
    
    for a in eve_in[x]:
        if a < n:
            cnt[a] += 1
            if cnt[a] == 1:
                st.update(a, (1, 1))
    
    if eve_que[x] != -1:
        ans[eve_que[x]] = st.query(0, n)[0]
    
    for a in eve_out[x]:
        if a < n:
            cnt[a] -= 1
            if cnt[a] == 0:
                st.update(a, (0, 1))


for i in ans:
    print(i)
0