結果

問題 No.743 Segments on a Polygon
ユーザー kohei2019kohei2019
提出日時 2022-04-01 00:01:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 889 ms / 2,000 ms
コード長 2,971 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 92,032 KB
最終ジャッジ日時 2024-04-28 21:46:05
合計ジャッジ時間 10,782 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 868 ms
92,032 KB
testcase_01 AC 866 ms
91,768 KB
testcase_02 AC 885 ms
91,816 KB
testcase_03 AC 889 ms
91,692 KB
testcase_04 AC 860 ms
91,828 KB
testcase_05 AC 885 ms
91,448 KB
testcase_06 AC 881 ms
91,892 KB
testcase_07 AC 887 ms
91,516 KB
testcase_08 AC 888 ms
91,920 KB
testcase_09 AC 499 ms
91,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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,M = map(int,input().split())
ans = 0
LSG = LazySegTree([0]*(M))
lsab = []
for i in range(N):
    a,b = map(int, input().split())
    if a > b:
        a,b = b,a
    lsab.append((a,b))
lsab.sort()
for i in range(N):
    a,b = lsab[i]
    if b-a == 1:
        continue
    ans += abs(LSG.leafvalue(a)-LSG.leafvalue(b))
    LSG.updatel_add(a+1, b, 1)

print(ans)
0