結果

問題 No.905 Sorted?
ユーザー hedwig100hedwig100
提出日時 2020-04-12 12:39:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 488 ms / 2,000 ms
コード長 2,434 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 81,764 KB
実行使用メモリ 105,996 KB
最終ジャッジ日時 2023-10-22 01:01:09
合計ジャッジ時間 7,138 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,416 KB
testcase_01 AC 47 ms
55,416 KB
testcase_02 AC 47 ms
55,416 KB
testcase_03 AC 46 ms
55,416 KB
testcase_04 AC 46 ms
55,416 KB
testcase_05 AC 88 ms
76,076 KB
testcase_06 AC 63 ms
68,340 KB
testcase_07 AC 104 ms
76,548 KB
testcase_08 AC 477 ms
84,224 KB
testcase_09 AC 365 ms
87,444 KB
testcase_10 AC 430 ms
93,460 KB
testcase_11 AC 436 ms
98,460 KB
testcase_12 AC 480 ms
95,656 KB
testcase_13 AC 457 ms
104,692 KB
testcase_14 AC 488 ms
104,680 KB
testcase_15 AC 428 ms
105,532 KB
testcase_16 AC 456 ms
105,996 KB
testcase_17 AC 449 ms
105,904 KB
testcase_18 AC 369 ms
105,908 KB
testcase_19 AC 45 ms
55,420 KB
testcase_20 AC 45 ms
55,420 KB
testcase_21 AC 45 ms
55,420 KB
testcase_22 AC 45 ms
55,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from collections import deque
from heapq import heapify,heappush,heappop

class SegmentTree():

    def __init__(self,init_val,n,func): #init_valは長さnの配列 O(2n)
        self.unit = 1
        self.size = pow(2,n-1).bit_length() #n以上の最小の2のべき乗
        self.seg = [self.unit]*2*self.size #セグメントツリー本体
        self.f = func #セグ木により異なる関数
        
        for i in range(n):
            self.seg[i+self.size-1] = init_val[i] 
        
        for i in range(self.size-2,-1,-1):
            self.seg[i] = self.f(self.seg[2*i+1],self.seg[2*i+2])
    
    def update(self,k,x): #k番目の要素をxに変更する O(logN)
        k+=self.size-1
        self.seg[k]=x
        while k:
            k=(k-1)//2
            self.seg[k]=self.f(self.seg[2*k+1],self.seg[2*k+2])
    
    def query(self,p,q): #[p,q)のクエリに答える 半開区間であることに注意 O(logN)
        if q<=p:
            return self.unit

        p+=self.size-1
        q+=self.size-2
        res=self.unit

        while q-p>1:

            if (p&1)==0:
                res=self.f(res,self.seg[p])

            if (q&1)==1:
                res=self.f(res,self.seg[q])
                q-=1
            p//=2
            q=(q-1)//2
        
        if p==q:
            res=self.f(res,self.seg[p])
        else:
            res=self.f(self.f(res,self.seg[p]),self.seg[q])
        
        return res

def f(x,y):
    return (x&y)

def main():
    n = int(input())
    a = list(map(int,input().split()))

    if n == 1:
        q = int(input())
        for _ in range(q):
            l,r = map(int,input().split())
            if l == r:
                print(1,1)
        return
    
    inc = []
    dec = []
    for i in range(n - 1):
        if a[i] == a[i + 1]:
            inc.append(1)
            dec.append(1)
        elif a[i] < a[i + 1]:
            inc.append(1)
            dec.append(0)
        else:
            inc.append(0)
            dec.append(1)

    st_inc = SegmentTree(inc,n - 1,f)
    st_dec = SegmentTree(dec,n - 1,f)

    q = int(input())
    for _ in range(q):
        l,r = map(int,input().split())
        if l == r:
            print(1,1)
        else:
            print(st_inc.query(l,r),st_dec.query(l,r))

if __name__ =='__main__':
    main()  
0