結果

問題 No.1673 Lamps on a line
ユーザー titiatitia
提出日時 2021-09-10 22:09:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 388 ms / 2,000 ms
コード長 2,538 bytes
コンパイル時間 1,277 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 98,840 KB
最終ジャッジ日時 2023-09-02 18:00:07
合計ジャッジ時間 5,477 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
70,840 KB
testcase_01 AC 75 ms
70,688 KB
testcase_02 AC 246 ms
80,052 KB
testcase_03 AC 182 ms
79,856 KB
testcase_04 AC 234 ms
81,512 KB
testcase_05 AC 150 ms
78,056 KB
testcase_06 AC 234 ms
80,584 KB
testcase_07 AC 310 ms
95,180 KB
testcase_08 AC 108 ms
92,552 KB
testcase_09 AC 361 ms
83,760 KB
testcase_10 AC 388 ms
87,516 KB
testcase_11 AC 167 ms
86,636 KB
testcase_12 AC 360 ms
98,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,Q=map(int,input().split())

# 非再帰遅延伝搬セグ木

seg_el=1<<(N.bit_length()) # Segment treeの台の要素数
seg_height=1+N.bit_length() # Segment treeの高さ
SEG=[0]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化
LAZY=[0]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化

def indexes(L,R): # 遅延伝搬すべきノードのリストを返す. (つまり, updateやgetvaluesで見るノードより上にあるノードたち)
    INDLIST=[]

    R-=1
    
    L>>=1
    R>>=1

    while L!=R:
        if L>R:
            INDLIST.append(L)
            L>>=1
        else:
            INDLIST.append(R)
            R>>=1

    while L!=0:
        INDLIST.append(L)
        L>>=1

    return INDLIST

def updates(l,r):
        
    L=l+seg_el
    R=r+seg_el

    L//=(L & (-L))
    R//=(R & (-R))

    UPIND=indexes(L,R)
    
    for ind in UPIND[::-1]: # 遅延伝搬
        if LAZY[ind]==1:
            LAZY[ind<<1]^=1
            LAZY[1+(ind<<1)]^=1
            SEG[ind<<1]=(1<<(seg_height - ((ind<<1).bit_length()))) - SEG[ind<<1]
            SEG[1+(ind<<1)]=(1<<(seg_height - ((1+(ind<<1)).bit_length()))) - SEG[1+(ind<<1)]
            LAZY[ind]=0

    while L!=R:
        if L > R:
            SEG[L]=(1<<(seg_height - (L.bit_length()))) - SEG[L]
            LAZY[L]^=1
            L+=1
            L//=(L & (-L))

        else:
            R-=1
            SEG[R]=(1<<(seg_height - (R.bit_length()))) - SEG[R]
            LAZY[R]^=1
            R//=(R & (-R))

    for ind in UPIND:
        SEG[ind]=SEG[ind<<1]+SEG[1+(ind<<1)]

def getvalues(l,r): # 区間[l,r)に関する和を調べる

    L=l+seg_el
    R=r+seg_el

    L//=(L & (-L))
    R//=(R & (-R))

    UPIND=indexes(L,R)
    
    for ind in UPIND[::-1]: # 遅延伝搬
        if LAZY[ind]==1:
            LAZY[ind<<1]^=1
            LAZY[1+(ind<<1)]^=1
            SEG[ind<<1]=(1<<(seg_height - ((ind<<1).bit_length()))) - SEG[ind<<1]
            SEG[1+(ind<<1)]=(1<<(seg_height - ((1+(ind<<1)).bit_length()))) - SEG[1+(ind<<1)]
            LAZY[ind]=0
            
    ANS=0

    while L!=R:
        if L > R:
            ANS+=SEG[L]
            L+=1
            L//=(L & (-L))

        else:
            R-=1
            ANS+=SEG[R]
            R//=(R & (-R))

    return ANS

for queries in range(Q):
    l,r=map(int,input().split())
    updates(l,r+1)

    #print(SEG)
    #print(LAZY)

    print(getvalues(1,N+1))

    
0