def reader(inFile):
dummy = inFile.getInt()
return (inFile.getInts(), inFile.getInts())
class Queue:
def __init__(self, x):
self.q = x
self.qat = 0
self.qlen = len(x)
def pop(self):
self.qat += 1
return self.q[self.qat - 1]
def elements(self):
while (not self.empty()):
yield self.pop()
def push(self, x):
self.q.append(x)
self.qlen += 1
def empty(self):
return self.qat == self.qlen
def solver((a,b)):
N = len(a)
lwrs = [[] for i in xrange(N)]
uprs = [[] for i in xrange(N)]
last = {}
for i in xrange(N):
if last.has_key(a[i]):
lwrs[last[a[i]]] += [i]
last[a[i]] = i
if a[i] > 1:
lwrs[i] += [last[a[i]-1]]
uprs[last[a[i]-1]] += [i]
last = {}
for i in xrange(N - 1, -1, -1):
if last.has_key(b[i]):
lwrs[last[b[i]]] += [i]
last[b[i]] = i
if b[i] > 1:
lwrs[i] += [last[b[i]-1]]
uprs[last[b[i]-1]] += [i]
ans = [0] * N
for i in xrange(N):
inQ = [False] * N
q = Queue([i])
inQ[i] = True
for j in q.elements():
for l in lwrs[j]:
if not inQ[l]:
q.push(l)
inQ[l] = True
ans[i] = q.qlen
for j in xrange(N):
if not inQ[j]:
lwrs[j] += [i]
return " ".join([str(z) for z in ans])
if __name__ == "__main__":
from GCJ import GCJ
GCJ(reader, solver, "/Users/lpebody/gcj/2013_2/c/", "c").run()
ZGVmIHJlYWRlcihpbkZpbGUpOgogICAgZHVtbXkgPSBpbkZpbGUuZ2V0SW50KCkKICAgIHJldHVybiAoaW5GaWxlLmdldEludHMoKSwgaW5GaWxlLmdldEludHMoKSkKCmNsYXNzIFF1ZXVlOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHgpOgogICAgICAgIHNlbGYucSA9IHgKICAgICAgICBzZWxmLnFhdCA9IDAKICAgICAgICBzZWxmLnFsZW4gPSBsZW4oeCkKICAgIAogICAgZGVmIHBvcChzZWxmKToKICAgICAgICBzZWxmLnFhdCArPSAxCiAgICAgICAgcmV0dXJuIHNlbGYucVtzZWxmLnFhdCAtIDFdCiAgICAKICAgIGRlZiBlbGVtZW50cyhzZWxmKToKICAgICAgICB3aGlsZSAobm90IHNlbGYuZW1wdHkoKSk6CiAgICAgICAgICAgIHlpZWxkIHNlbGYucG9wKCkKCiAgICBkZWYgcHVzaChzZWxmLCB4KToKICAgICAgICBzZWxmLnEuYXBwZW5kKHgpCiAgICAgICAgc2VsZi5xbGVuICs9IDEKCiAgICBkZWYgZW1wdHkoc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYucWF0ID09IHNlbGYucWxlbgoKZGVmIHNvbHZlcigoYSxiKSk6CiAgICBOID0gbGVuKGEpCiAgICBsd3JzID0gW1tdIGZvciBpIGluIHhyYW5nZShOKV0gCiAgICB1cHJzID0gW1tdIGZvciBpIGluIHhyYW5nZShOKV0KICAgIGxhc3QgPSB7fQogICAgZm9yIGkgaW4geHJhbmdlKE4pOgogICAgICAgIGlmIGxhc3QuaGFzX2tleShhW2ldKToKICAgICAgICAgICAgbHdyc1tsYXN0W2FbaV1dXSArPSBbaV0KICAgICAgICBsYXN0W2FbaV1dID0gaQogICAgICAgIGlmIGFbaV0gPiAxOgogICAgICAgICAgICBsd3JzW2ldICs9IFtsYXN0W2FbaV0tMV1dCiAgICAgICAgICAgIHVwcnNbbGFzdFthW2ldLTFdXSArPSBbaV0KICAgIGxhc3QgPSB7fQogICAgZm9yIGkgaW4geHJhbmdlKE4gLSAxLCAtMSwgLTEpOgogICAgICAgIGlmIGxhc3QuaGFzX2tleShiW2ldKToKICAgICAgICAgICAgbHdyc1tsYXN0W2JbaV1dXSArPSBbaV0KICAgICAgICBsYXN0W2JbaV1dID0gaQogICAgICAgIGlmIGJbaV0gPiAxOgogICAgICAgICAgICBsd3JzW2ldICs9IFtsYXN0W2JbaV0tMV1dCiAgICAgICAgICAgIHVwcnNbbGFzdFtiW2ldLTFdXSArPSBbaV0KICAgIAogICAgYW5zID0gWzBdICogTgogICAgZm9yIGkgaW4geHJhbmdlKE4pOgogICAgICAgIGluUSA9IFtGYWxzZV0gKiBOCiAgICAgICAgcSA9IFF1ZXVlKFtpXSkKICAgICAgICBpblFbaV0gPSBUcnVlCiAgICAgICAgZm9yIGogaW4gcS5lbGVtZW50cygpOgogICAgICAgICAgICBmb3IgbCBpbiBsd3JzW2pdOgogICAgICAgICAgICAgICAgaWYgbm90IGluUVtsXToKICAgICAgICAgICAgICAgICAgICBxLnB1c2gobCkKICAgICAgICAgICAgICAgICAgICBpblFbbF0gPSBUcnVlCiAgICAgICAgYW5zW2ldID0gcS5xbGVuCiAgICAgICAgZm9yIGogaW4geHJhbmdlKE4pOgogICAgICAgICAgICBpZiBub3QgaW5RW2pdOgogICAgICAgICAgICAgICAgbHdyc1tqXSArPSBbaV0KICAgIHJldHVybiAiICIuam9pbihbc3RyKHopIGZvciB6IGluIGFuc10pCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgZnJvbSBHQ0ogaW1wb3J0IEdDSgogICAgR0NKKHJlYWRlciwgc29sdmVyLCAiL1VzZXJzL2xwZWJvZHkvZ2NqLzIwMTNfMi9jLyIsICJjIikucnVuKCkK