結果

問題 No.905 Sorted?
ユーザー hedwig100hedwig100
提出日時 2020-04-12 12:38:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,696 ms / 2,000 ms
コード長 2,434 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 12,172 KB
実行使用メモリ 23,840 KB
最終ジャッジ日時 2023-10-22 01:00:49
合計ジャッジ時間 15,527 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,420 KB
testcase_01 AC 31 ms
10,420 KB
testcase_02 AC 32 ms
10,416 KB
testcase_03 AC 31 ms
10,420 KB
testcase_04 AC 32 ms
10,420 KB
testcase_05 AC 39 ms
10,864 KB
testcase_06 AC 35 ms
10,660 KB
testcase_07 AC 51 ms
11,760 KB
testcase_08 AC 1,309 ms
16,856 KB
testcase_09 AC 862 ms
16,044 KB
testcase_10 AC 1,195 ms
22,052 KB
testcase_11 AC 756 ms
19,556 KB
testcase_12 AC 881 ms
18,956 KB
testcase_13 AC 782 ms
23,840 KB
testcase_14 AC 1,363 ms
23,832 KB
testcase_15 AC 1,696 ms
20,012 KB
testcase_16 AC 1,638 ms
22,400 KB
testcase_17 AC 1,584 ms
21,892 KB
testcase_18 AC 1,152 ms
21,892 KB
testcase_19 AC 30 ms
10,416 KB
testcase_20 AC 31 ms
10,416 KB
testcase_21 AC 31 ms
10,416 KB
testcase_22 AC 30 ms
10,416 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