結果

問題 No.1786 Maximum Suffix Median (Online)
ユーザー Kiri8128Kiri8128
提出日時 2021-11-23 15:18:26
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 10,304 bytes
コンパイル時間 512 ms
コンパイル使用メモリ 82,280 KB
実行使用メモリ 250,092 KB
最終ジャッジ日時 2024-07-23 19:43:45
合計ジャッジ時間 40,925 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
56,508 KB
testcase_01 AC 43 ms
56,776 KB
testcase_02 AC 47 ms
57,552 KB
testcase_03 AC 63 ms
69,584 KB
testcase_04 AC 58 ms
66,992 KB
testcase_05 AC 64 ms
71,184 KB
testcase_06 AC 56 ms
65,928 KB
testcase_07 AC 54 ms
64,872 KB
testcase_08 TLE -
testcase_09 AC 1,516 ms
139,456 KB
testcase_10 AC 1,560 ms
198,624 KB
testcase_11 AC 1,311 ms
169,700 KB
testcase_12 TLE -
testcase_13 AC 1,454 ms
213,956 KB
testcase_14 AC 1,158 ms
213,220 KB
testcase_15 AC 1,263 ms
210,236 KB
testcase_16 AC 1,314 ms
222,844 KB
testcase_17 AC 1,226 ms
158,964 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 AC 1,329 ms
226,900 KB
testcase_24 AC 994 ms
165,620 KB
testcase_25 AC 1,477 ms
224,936 KB
testcase_26 AC 1,336 ms
218,756 KB
testcase_27 AC 1,267 ms
208,028 KB
testcase_28 AC 1,414 ms
206,448 KB
testcase_29 AC 45 ms
57,184 KB
testcase_30 AC 44 ms
56,312 KB
testcase_31 AC 1,575 ms
237,568 KB
testcase_32 AC 1,602 ms
195,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class LazySegmentTree():
    def __init__(self, init, unitX, unitA, f, g, h, Z = None):
        self.f = f # (X, X) -> X
        self.g = g # (X, A, size) -> X
        self.h = h # (A, A) -> A
        self.unitX = unitX
        self.unitA = unitA
        self.f = f
        if type(init) == int:
            self.n = init
            self.n = 1 << (self.n - 1).bit_length()
            self.X = [unitX] * (self.n * 2)
            if not Z:
                self.size = [1] * (self.n * 2)
            else:
                self.size = [0] * self.n + [b - a for a, b in zip(Z, Z[1:])]
                self.size += [0] * (self.n * 2 - len(self.size))
        else:
            self.n = len(init)
            self.n = 1 << (self.n - 1).bit_length()
            self.X = [unitX] * self.n + init + [unitX] * (self.n - len(init))
            if not Z:
                self.size = [0] * self.n + [1] * len(init) + [0] * (self.n - len(init))
            else:
                self.size = [0] * self.n + [b - a for a, b in zip(Z, Z[1:])]
                self.size += [0] * (self.n * 2 - len(self.size))
            for i in range(self.n-1, 0, -1):
                self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
    
        for i in range(self.n - 1, 0, -1):
            self.size[i] = self.size[i*2] + self.size[i*2|1]
        
        self.A = [unitA] * (self.n * 2)
        
    def update(self, i, x):
        i += self.n
        self.propagate_above(i)
        self.X[i] = x
        self.A[i] = unitA
        self.calc_above(i)
    
    def calc(self, i):
        return self.g(self.X[i], self.A[i], self.size[i])
    
    def calc_above(self, i):
        i >>= 1
        while i:
            self.X[i] = self.f(self.calc(i*2), self.calc(i*2|1))
            i >>= 1
    
    def propagate(self, i):
        self.X[i] = self.g(self.X[i], self.A[i], self.size[i])
        self.A[i*2] = self.h(self.A[i*2], self.A[i])
        self.A[i*2|1] = self.h(self.A[i*2|1], self.A[i])
        self.A[i] = self.unitA
        
    def propagate_above(self, i):
        H = i.bit_length()
        for h in range(H, 0, -1):
            self.propagate(i >> h)
    
    def propagate_all(self):
        for i in range(1, self.n):
            self.propagate(i)
    
    def getrange(self, l, r):
        l += self.n
        r += self.n
        l0, r0 = l // (l & -l), r // (r & -r) - 1
        self.propagate_above(l0)
        self.propagate_above(r0)
        
        al = self.unitX
        ar = self.unitX
        while l < r:
            if l & 1:
                al = self.f(al, self.calc(l))
                l += 1
            if r & 1:
                r -= 1
                ar = self.f(self.calc(r), ar)
            l >>= 1
            r >>= 1
        return self.f(al, ar)
    
    def getrange_l(self, r):
        if r == self.n: return self.calc(1)
        r += self.n
        r //= r & -r
        r0 = r
        self.propagate_above(r0)
        ar = self.unitX
        while r > 1:
            r -= 1
            ar = self.f(self.calc(r), ar)
            r //= r & -r
        return ar
    
    def getvalue(self, i):
        i += self.n
        self.propagate_above(i)
        return self.calc(i)
    
    def operate_range(self, l, r, a):
        l += self.n
        r += self.n
        l0, r0 = l // (l & -l), r // (r & -r) - 1
        self.propagate_above(l0)
        self.propagate_above(r0)
        while l < r:
            if l & 1:
                self.A[l] = self.h(self.A[l], a)
                l += 1
            if r & 1:
                r -= 1
                self.A[r] = self.h(self.A[r], a)
            l >>= 1
            r >>= 1
        
        self.calc_above(l0)
        self.calc_above(r0)
    
    def operate_range_l(self, r, a):
        if r == self.n:
            self.A[1] = self.h(self.A[1], a)
            return
        r += self.n
        r //= r & -r
        r0 = r - 1
        self.propagate_above(r0)
        while r > 1:
            r -= 1
            self.A[r] = self.h(self.A[r], a)
            r //= r & -r
        
        self.calc_above(r0)
    
    def operate_range_r(self, l, a):
        if l >= self.n: return
        if not l:
            self.A[1] = self.h(self.A[1], a)
            return
        l += self.n
        l //= l & -l
        l0 = l
        self.propagate_above(l0)
        while l > 1:
            self.A[l] = self.h(self.A[l], a)
            l += 1
            l //= l & -l
        self.calc_above(l0)

    def check(self, randX, randA, maxs, rep):
        from random import randrange
        f, g, h = self.f, self.g, self.h
        for _ in range(rep):
            x = randX()
            y = randX()
            z = randX()
            a = randA()
            b = randA()
            c = randA()
            s = randrange(1, maxs + 1)
            t = randrange(1, maxs + 1)
            err = 0
            if not f(x, unitX) == f(unitX, x) == x:
                err = 1
                print("!!!!! unitX Error !!!!!")
                print("unitX =", unitX)
                print("x =", x)
                print("f(x, unitX) =", f(x, unitX))
                print("f(unitX, x) =", f(unitX, x))
            
            if not h(a, unitA) == h(unitA, a) == a:
                err = 1
                print("!!!!! unitA Error !!!!!")
                print("unitA =", unitA)
                print("a =", a)
                print("h(a, unitA) =", h(a, unitA))
                print("h(unitA, a) =", h(unitA, a))
                
            if not f(f(x, y), z) == f(x, f(y, z)):
                err = 1
                print("!!!!! Associativity Error X !!!!!")
                print("x, y, z, f(x, y), f(y, x) =", x, y, z, f(x, y), f(y, x))
                print("f(f(x, y), z) =", f(f(x, y), z))
                print("f(x, f(y, z)) =", f(x, f(y, z)))
            
            if not h(h(a, b), c) == h(a, h(b, c)):
                err = 1
                print("!!!!! Associativity Error A !!!!!")
                print("a, b, c, h(a, b), h(b, c) =", a, b, c, h(a, b), h(b, c))
                print("h(h(a, b), c) =", h(h(a, b), c))
                print("h(a, h(b, c)) =", h(a, h(b, c)))
            
            if not g(x, unitA, s) == x:
                err = 1
                print("!!!!! Identity Error !!!!!")
                print("unitA, x, s =", unitA, x, s)
                print("g(x, unitA, s) =", g(x, unitA, s))
            
            if not g(g(x, a, s), b, s) == g(x, h(a, b), s):
                err = 1
                print("!!!!! Act Error 1 !!!!!")
                print("x, a, b, s, g(x, a, s), h(a, b) =", x, a, b, s, g(x, a, s), h(a, b))
                print("g(g(x, a, s), b, s) =", g(g(x, a, s), b, s))
                print("g(x, h(a, b), s)    =", g(x, h(a, b), s))
            
            if not g(f(x, y), a, s + t) == f(g(x, a, s), g(y, a, t)):
                err = 1
                print("!!!!! Act Error 2 !!!!!")
                print("x, y, a, s, t, f(x, y), g(x, a, s), g(y, a, t) =", x, y, a, s, t, f(x, y), g(x, a, s), g(y, a, t))
                print("g(f(x, y), a, s + t)      =", g(f(x, y), a, s + t))
                print("f(g(x, a, s), g(y, a, t)) =", f(g(x, a, s), g(y, a, t)))
            
            if err:
                break
                assert f(x, unitX) == f(unitX, x) == x
                assert h(a, unitA) == h(unitA, a) == a
                assert f(f(x, y), z) == f(x, f(y, z))
                assert h(h(a, b), c) == h(a, h(b, c))
                assert g(x, unitA, s) == x
                assert g(g(x, a, s), b, s) == g(x, h(a, b), s)
                assert g(f(x, y), a, s + t) == f(g(x, a, s), g(y, a, t))
        else:
            pass
            print("Monoid Check OK!")
    
    def debug1(self):
        print("self.n =", self.n)
        deX = []
        deA = []
        deS = []
        a, b = self.n, self.n * 2
        while b:
            deX.append(self.X[a:b])
            deA.append(self.A[a:b])
            deS.append(self.size[a:b])
            a, b = a//2, a
        print("--- debug ---")
        for d in deX[::-1]:
            print(d)
        print("--- ---")
        for d in deA[::-1]:
            print(d)
        print("--- ---")
        for d in deS[::-1]:
            print(d)
        print("--- ---")
    
    def debug(self, k = 10):
        print("--- debug ---")
        print("point")
        for i in range(min(self.n - 1, k)):
            print(i, self.getvalue(i))
        print("prod")
        for i in range(min(self.n, k)):
            print(i, self.getrange(0, i))
        print("--- ---")
    
    def debug(self):
        print([self.getvalue(i) for i in range(self.n)])

import sys
input = lambda: sys.stdin.readline().rstrip()

from heapq import heappush, heappop
class MaxHeapQ():
    def __init__(self):
        self.H = []
    def hpush(self, x):
        heappush(self.H, -x)
    def hpop(self):
        return -heappop(self.H)
    def hmax(self):
        return -self.H[0]

N = int(input())
A = [int(input()) for _ in range(N)]
ANS = [-1] * N
ans = 0
MH = [MaxHeapQ() for _ in range(2)]
for i in range(0, N):
    H = MH[i%2]
    H.hpush(A[i-1])
    H.hpop()
    A[i] ^= ans
    H.hpush(A[i])
    ans = H.hmax()
    ANS[i] = ans

SA = sorted(set(A))
DA = {a: i for i, a in enumerate(SA)}

# 区間 add 区間 max
from operator import add
f = max
g = lambda x, a, s: x + a
h = add
inf = 10 ** 18
unitX = -inf
unitA = 0
IN = [i - N for i in range(N + 1)]
st1 = LazySegmentTree(IN, unitX, unitA, f, g, h)

# 区間 add 区間 min
f = min
unitX = inf
IN = [((i - N) << 18) + i for i in range(N + 1)]
st2 = LazySegmentTree(IN, unitX, unitA, f, g, h)

ANS = [-1] * N
mo = 1 << 18
mmm = (1 << 18) - 1
IA = [[] for _ in range(N + 1)]
for i, a in enumerate(A):
    IA[DA[a]].append(i)

for a in range(N + 1)[::-1]:
    iii = IA[a]
    if not iii: continue
    for i in iii:
        st1.operate_range_l(i + 1, 2)
        st2.operate_range_l(i + 1, 2 << 18)
        ma = st1.getrange_l(i + 1)
        while 1:
            mii = st2.getrange(i, N + 1)
            mi = mii // mo
            j = mii % mo
            if mi >= ma:
                break
            ANS[j-1] = SA[a]
            st2.update(j, 10 ** 18)
        
        if ANS[i] < 0:
            ANS[i] = a
            st2.update(i, 10 ** 18)

print("\n".join(map(str, ANS)))
0