結果
問題 | No.905 Sorted? |
ユーザー | stng |
提出日時 | 2022-08-13 18:02:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 388 ms / 2,000 ms |
コード長 | 9,279 bytes |
コンパイル時間 | 517 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 96,768 KB |
最終ジャッジ日時 | 2024-09-24 21:00:54 |
合計ジャッジ時間 | 6,480 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,504 KB |
testcase_01 | AC | 40 ms
54,528 KB |
testcase_02 | AC | 40 ms
54,016 KB |
testcase_03 | AC | 40 ms
54,016 KB |
testcase_04 | AC | 40 ms
54,400 KB |
testcase_05 | AC | 72 ms
74,000 KB |
testcase_06 | AC | 58 ms
66,944 KB |
testcase_07 | AC | 88 ms
77,308 KB |
testcase_08 | AC | 386 ms
91,904 KB |
testcase_09 | AC | 314 ms
87,828 KB |
testcase_10 | AC | 351 ms
94,444 KB |
testcase_11 | AC | 373 ms
94,064 KB |
testcase_12 | AC | 373 ms
95,340 KB |
testcase_13 | AC | 297 ms
93,916 KB |
testcase_14 | AC | 388 ms
95,340 KB |
testcase_15 | AC | 256 ms
96,768 KB |
testcase_16 | AC | 273 ms
96,692 KB |
testcase_17 | AC | 274 ms
96,256 KB |
testcase_18 | AC | 270 ms
93,952 KB |
testcase_19 | AC | 40 ms
53,760 KB |
testcase_20 | AC | 39 ms
53,888 KB |
testcase_21 | AC | 40 ms
53,888 KB |
testcase_22 | AC | 40 ms
53,504 KB |
ソースコード
import sysinput = sys.stdin.buffer.readlinesys.setrecursionlimit(10 ** 7)class SegTree(object):def __init__(self, N, op_data, u_data):self._n = Nself.log = (N-1).bit_length()self.size = 1 << self.logself.op = op_dataself.e = u_dataself.data = [u_data] * (2 * self.size)# self.len = [1] * (2 * self.size)def _update(self, i):self.data[i] = self.op(self.data[i << 1], self.data[i << 1 | 1])def initialize(self, arr=None):""" segtreeをarrで初期化する。len(arr) == Nにすること """if arr:for i, a in enumerate(arr, self.size):self.data[i] = afor i in reversed(range(1, self.size)):self._update(i)# self.len[i] = self.len[i << 1] + self.len[i << 1 | 1]def update(self, p, x):""" data[p] = x とする (0-indexed)"""p += self.sizeself.data[p] = xfor i in range(1, self.log + 1):self._update(p >> i)def get(self, p):""" data[p]を返す """return self.data[p + self.size]def prod(self, l, r):"""op_data(data[l], data[l+1], ..., data[r-1])を返す (0-indexed)"""sml = self.esmr = self.el += self.sizer += self.sizewhile l < r:if l & 1:sml = self.op(sml, self.data[l])l += 1if r & 1:r -= 1smr = self.op(self.data[r], smr)l >>= 1r >>= 1return self.op(sml, smr)def all_prod(self):""" op(data[0], data[1], ... data[N-1])を返す """return self.data[1]def max_right(self, l, func):"""func(l, l+1, ..., r-1) = True,func(l, l+1, ..., r-1, r) = Falseとなる r を返す"""if l == self._n:return self._nl += self.sizesm = self.ewhile True:while l % 2 == 0:l >>= 1if not func(self.op(sm, self.data[l])):while l < self.size:l <<= 1if func(self.op(sm, self.data[l])):sm = self.op(sm, self.data[l])l += 1return l - self.sizesm = self.op(sm, self.data[l])l += 1if (l & -l) == l:breakreturn self._ndef min_left(self, r, func):"""func( l, l+1, ..., r-1) = True,func(l-1, l, l+1, ..., r-1) = Falseとなる l を返す"""if r == 0:return 0r += self.sizesm = self.ewhile True:r -= 1while r > 1 and r & 1:r >>= 1if not func(self.op(self.data[r], sm)):while r < self.size:r = r << 1 | 1if func(self.op(self.data[r], sm)):sm = self.op(self.data[r], sm)r -= 1return r + 1 - self.sizesm = self.op(self.data[r], sm)if (r & -r) == r:breakreturn 0class LazySegTree(SegTree):def __init__(self, N, op_data, u_data, op_lazy, u_lazy, op_merge):super().__init__(N, op_data, u_data)self.composition = op_lazyself.mapping = op_mergeself.id = u_lazyself.lazy = [u_lazy] * self.sizedef _all_apply(self, i, F):# self.data[i] = self.mapping(F, self.data[i], self.len[i])self.data[i] = self.mapping(F, self.data[i])if i < self.size:self.lazy[i] = self.composition(F, self.lazy[i])def _push(self, i):self._all_apply(i << 1, self.lazy[i])self._all_apply(i << 1 | 1, self.lazy[i])self.lazy[i] = self.iddef update(self, p, x):""" data[p] = x とする (0-indexed)"""p += self.sizefor i in reversed(range(1, self.log + 1)):self._push(p >> i)self.data[p] = xfor i in range(1, self.log + 1):self._update(p >> i)def apply(self, p, F):""" data[p]にFを作用させる(data[p] = op_merge(F, data[p])とする, 0-indexed) """p += self.sizefor i in reversed(range(1, self.log + 1)):self._push(p >> i)# self.data[p] = self.mapping(F, self.data[p], self.len[p])self.data[p] = self.mapping(F, self.data[p])for i in range(1, self.log + 1):self._update(p >> i)def range_apply(self, l, r, F):""" i = l, l+1, ..., r-1 について、Fを作用させる(op_merge(F, data[i]), 0-indexed) """if l == r:returnl += self.sizer += self.sizefor i in reversed(range(1, self.log + 1)): # too->downif ((l >> i) << i) != l:self._push(l >> i)if ((r >> i) << i) != r:self._push((r - 1) >> i)l2, r2 = l, rwhile l < r:if l & 1:self._all_apply(l, F)l += 1if r & 1:r -= 1self._all_apply(r, F)l >>= 1r >>= 1l, r = l2, r2for i in range(1, self.log + 1):if ((l >> i) << i) != l:self._update(l >> i)if ((r >> i) << i) != r:self._update((r - 1) >> i)def get(self, p):""" data[p]を返す """p += self.sizefor i in reversed(range(1, self.log + 1)):self._push(p >> i)return self.data[p]def prod(self, l, r):"""op_data(data[l], data[l+1], ..., data[r-1])を返す (0-indexed)l == rの時は単位元u_dataを返す"""if l == r:return self.el += self.sizer += self.sizefor i in reversed(range(1, self.log + 1)):if ((l >> i) << i) != l:self._push(l >> i)if ((r >> i) << i) != r:self._push(r >> i)sml = self.esmr = self.ewhile l < r:if l & 1:sml = self.op(sml, self.data[l])l += 1if r & 1:r -= 1smr = self.op(self.data[r], smr)l >>= 1r >>= 1return self.op(sml, smr)def max_right(self, l, func):"""func(l, l+1, ..., r-1) = True,func(l, l+1, ..., r-1, r) = Falseとなる r を返す"""if l == self._n:return self._nl += self.sizefor i in reversed(range(1, self.log + 1)):self._push(l >> i)sm = self.ewhile True:while l % 2 == 0:l >>= 1if not func(self.op(sm, self.data[[l]])):while l < self.size:self._push(l)l <<= 1if func(self.op(sm, self.data[l])):sm = self.op(sm, self.data[l])l += 1return l - self.sizesm = self.op(sm, self.data[l])l += 1if (l & -l) == l:breakreturn self._ndef min_left(self, r, func):"""func( l, l+1, ..., r-1) = True,func(l-1, l, l+1, ..., r-1) = Falseとなる l を返す"""if r == 0:return 0r += self.sizefor i in reversed(range(1, self.log + 1)):self._push((r - 1) >> i)sm = self.ewhile True:r -= 1while r > 1 and r & 1:r >>= 1if not func(self.op(self.data[r], sm)):while r < self.size:self._push(r)r = r << 1 | 1if func(self.op(self.data[r], sm)):sm = self.op(self.data[r], sm)r -= 1return r + 1 - self.sizesm = self.op(self.data[r], sm)if (r & -r) == r:breakreturn 0"""遅延セグ木(ac-library移植)op_data(d_L, d_R) : d_Lとd_Rの二項演算, dataを返すop_lazy(lz_new, lz_orig) : lz_origにlz_newを作用させる, lazyを返すop_merge(lz, d) : dにlzを作用させる, dataを返す"""n = int(input())a = [int(i) for i in input().split()]q = int(input())lr = [[int(i) for i in input().split()] for j in range(q)]segmax = SegTree(n, max, -1)segmin = SegTree(n, min, 1)old = a[0]li = []for i in range(n):if old < a[i]:li.append(1)elif old == a[i]:li.append(0)else:li.append(-1)old = a[i]#print(li)segmax.initialize(li)segmin.initialize(li)for i in range(q):l,r = lr[i]#if l == r:# print(1,1)# continue#print(li[l+1:r+1])if segmin.prod(l+1,r+1) >= 0:ans = 1else:ans = 0#print(li[l+1:])if segmax.prod(l+1,r+1) <= 0:print(ans,1)else:print(ans,0)