結果

問題 No.876 Range Compress Query
ユーザー kohei2019kohei2019
提出日時 2022-05-04 22:44:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 5,829 bytes
コンパイル時間 1,213 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 98,836 KB
最終ジャッジ日時 2023-09-17 03:39:44
合計ジャッジ時間 10,184 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,380 KB
testcase_01 WA -
testcase_02 AC 74 ms
71,208 KB
testcase_03 WA -
testcase_04 AC 98 ms
76,500 KB
testcase_05 AC 87 ms
76,492 KB
testcase_06 AC 140 ms
77,552 KB
testcase_07 WA -
testcase_08 AC 132 ms
77,680 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 815 ms
96,420 KB
testcase_12 AC 735 ms
96,116 KB
testcase_13 AC 752 ms
96,636 KB
testcase_14 AC 793 ms
94,972 KB
testcase_15 AC 666 ms
96,724 KB
testcase_16 AC 798 ms
98,548 KB
testcase_17 AC 818 ms
98,836 KB
testcase_18 AC 843 ms
98,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

class SegTree:
    DEFAULT = {
        'min': 1 << 60,
        'max': -(1 << 60),
        'sum': 0,
        'prd': 1,
        'gcd': 0,
        'lmc': 1,
        '^': 0,
        '&': (1 << 60) - 1,
        '|': 0,
    }

    FUNC = {
        'min': min,
        'max': max,
        'sum': (lambda x, y: x + y),
        'prd': (lambda x, y: x * y),
        'gcd': math.gcd,
        'lmc': (lambda x, y: (x * y) // math.gcd(x, y)),
        '^': (lambda x, y: x ^ y),
        '&': (lambda x, y: x & y),
        '|': (lambda x, y: x | y),
    }

    def __init__(self, ls, mode='min', func=None, default=None):
        """
        要素ls, 関数mode (min,max,sum,prd(product),gcd,lmc,^,&,|)
        func,defaultを指定すれば任意の関数、単位元での計算が可能
        """
        N = len(ls)
        if default == None:
            self.default = self.DEFAULT[mode]
        else:
            self.default = default
        if func == None:
            self.func = self.FUNC[mode]
        else:
            self.func = func
        self.N = N
        self.K = (N - 1).bit_length()
        self.N2 = 1 << self.K
        self.dat = [self.default] * (2**(self.K + 1))
        for i in range(self.N):  # 葉の構築
            self.dat[self.N2 + i] = ls[i]
        self.build()

    def build(self):
        for j in range(self.N2 - 1, -1, -1):
            self.dat[j] = self.func(self.dat[j << 1], self.dat[j << 1 | 1])  # 親が持つ条件

    def leafvalue(self, x):  # リストのx番目の値
        return self.dat[x + self.N2]

    def update(self, x, y):  # index(x)をyに変更
        i = x + self.N2
        self.dat[i] = y
        while i > 0:  # 親の値を変更
            i >>= 1
            self.dat[i] = self.func(self.dat[i << 1], self.dat[i << 1 | 1])
        return

    def query(self, L, R):  # [L,R)の区間取得
        L += self.N2
        R += self.N2
        vL = self.default
        vR = self.default
        while L < R:
            if L & 1:
                vL = self.func(vL, self.dat[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.dat[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)

    def __iter__(self):
        for i in range(self.N):
            yield self[i]

    def __getitem__(self, x): return self.leafvalue(x)
    def __setitem__(self, x, val): return self.update(x, val)

class LazySegTree():
    #区間和のセグメント木、要素ls
    def __init__(self,ls):
        self.default = 0
        self.func = (lambda x, y: x + y)
        self.N = len(ls)
        self.K = (self.N-1).bit_length()
        self.N0 = 1 << self.K 
        self.dat = [self.default]*(2**(self.K+1))
        self.lazy = [0]*(2**(self.K+1)) #遅延評価
        for i in range(self.N): #葉の構築
            self.dat[2**self.K+i] = ls[i]
        self.build()

    def build(self):
        for j in range(self.N0-1,0,-1):
            self.dat[j] = self.func(self.dat[j<<1],self.dat[j<<1|1]) #親が持つ条件

    def leafvalue(self,x): #x番目の値を出力
        return self.query(x,x+1)

    def update_add(self,x,y): #x番目にyを足す
        return self.updatel_add(x,x+1,y)

    def gindex(self,l, r): #伝播するインデックス列挙
        L = l + self.N0; R = r + self.N0
        lm = (L // (L & -L)) >> 1
        rm = (R // (R & -R)) >> 1
        lsindex = []
        while L < R:
            if R <= rm:
                lsindex.append(R)
            if L <= lm:
                lsindex.append(L)
            L >>= 1; R >>= 1
        while L:
            lsindex.append(L)
            L >>= 1
        return lsindex

    def propagates(self,ids):
        for i in reversed(ids):
            v = self.lazy[i]
            if not v:
                continue
            self.lazy[i<<1] += v//2; self.lazy[i<<1|1] += v//2
            self.dat[i<<1] += v//2; self.dat[i<<1|1] += v//2
            self.lazy[i] = 0

    def updatel_add(self,l, r, x):#区間にyを足す
        self.propagates(self.gindex(l, r))
        L = self.N0 + l
        R = self.N0 + r
        ii = 0
        while L < R:
            if L & 1:
                self.lazy[L] += x*(2**ii)
                self.dat[L] += x*(2**ii)
                L += 1
            if R & 1:
                R -= 1
                self.lazy[R] += x*(2**ii)
                self.dat[R] += x*(2**ii)
            L >>= 1
            R >>= 1
            ii += 1
        for i in self.gindex(l, r):
            self.dat[i] = self.func(self.dat[i<<1],self.dat[i<<1|1])+self.lazy[i]

    def query(self,l, r):
        self.propagates(self.gindex(l, r))
        L = self.N0 + l
        R = self.N0 + r
        vL = self.default
        vR = self.default
        while L < R:
            if L & 1:
                vL = self.func(vL,self.dat[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.dat[R],vR)
            L >>= 1
            R >>= 1
        return self.func(vL,vR)

N,Q = map(int,input().split())
lsa = list(map(int,input().split()))
lsp = [0]*(N-1)
for i in range(N-1):
    if lsa[i] != lsa[i+1]:
        lsp[i] = 1
SG = SegTree(lsp,mode='sum')
LSG = LazySegTree(lsp)
ans = []
for i in range(Q):
    ls =list(map(int,input().split()))
    if ls[0] == 1:
        _,l,r,x = ls
        l -= 1
        LSG.updatel_add(l, r, x)
        if l != 0:
            if LSG.leafvalue(l-1) == LSG.leafvalue(l):
                SG[l-1] = 0
            else:
                SG[l-1] = 1
        if r != N:
            if LSG.leafvalue(r-1) == LSG.leafvalue(r):
                SG[r-1] = 0
            else:
                SG[r-1] = 1
    else:
        _,l,r = ls
        l -= 1
        r -= 1
        ans.append(SG.query(l, r)+1)
print(*ans,sep='\n')
0