結果

問題 No.1975 Zigzag Sequence
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-06-11 08:23:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,048 ms / 2,000 ms
コード長 4,463 bytes
コンパイル時間 464 ms
コンパイル使用メモリ 81,488 KB
実行使用メモリ 115,784 KB
最終ジャッジ日時 2023-10-21 15:29:34
合計ジャッジ時間 21,399 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,368 KB
testcase_01 AC 45 ms
55,368 KB
testcase_02 AC 45 ms
55,368 KB
testcase_03 AC 256 ms
80,408 KB
testcase_04 AC 208 ms
79,692 KB
testcase_05 AC 891 ms
94,680 KB
testcase_06 AC 641 ms
91,368 KB
testcase_07 AC 243 ms
80,240 KB
testcase_08 AC 633 ms
86,976 KB
testcase_09 AC 877 ms
102,212 KB
testcase_10 AC 655 ms
90,308 KB
testcase_11 AC 710 ms
94,732 KB
testcase_12 AC 279 ms
81,480 KB
testcase_13 AC 45 ms
55,368 KB
testcase_14 AC 44 ms
55,368 KB
testcase_15 AC 45 ms
55,368 KB
testcase_16 AC 44 ms
55,368 KB
testcase_17 AC 44 ms
55,368 KB
testcase_18 AC 539 ms
98,652 KB
testcase_19 AC 539 ms
98,660 KB
testcase_20 AC 662 ms
98,012 KB
testcase_21 AC 659 ms
97,864 KB
testcase_22 AC 641 ms
112,932 KB
testcase_23 AC 652 ms
113,896 KB
testcase_24 AC 780 ms
113,784 KB
testcase_25 AC 790 ms
113,788 KB
testcase_26 AC 947 ms
92,664 KB
testcase_27 AC 1,048 ms
104,788 KB
testcase_28 AC 752 ms
114,228 KB
testcase_29 AC 764 ms
114,228 KB
testcase_30 AC 783 ms
115,784 KB
testcase_31 AC 776 ms
114,272 KB
testcase_32 AC 623 ms
95,452 KB
testcase_33 AC 630 ms
94,936 KB
testcase_34 AC 924 ms
95,064 KB
testcase_35 AC 921 ms
95,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
from collections import *
class BIT:

    def __init__(self,len_A):
        self.N = len_A + 10
        self.bit = [0]*(len_A+10)
        
    # sum(A0 ~ Ai)
    # O(log N)
    def query(self,i):
        res = 0
        idx = i+1
        while idx:
            res += self.bit[idx]
            idx -= idx&(-idx)
        return res

    # Ai += x
    # O(log N)
    def update(self,i,x):
        idx = i+1
        while idx < self.N:
            self.bit[idx] += x
            idx += idx&(-idx)
    
    # min_i satisfying {sum(A0 ~ Ai) >= w} (Ai >= 0)
    # O(log N)
    def lower_left(self,w):
        if (w < 0):
            return -1
        x = 0
        k = 1<<(self.N.bit_length()-1)
        while k > 0:
            if x+k < self.N and self.bit[x+k] < w:
                w -= self.bit[x+k]
                x += k
            k //= 2
        return x


class OrderBIT:

    def __init__(self,all_values,sort_flag = False):
        if sort_flag:
            self.A = all_values
        else:
            self.A = sorted(all_values)
        self.B = BIT(len(all_values))
        self.num = 0
        
    def insert_val(self,x,c=1):
        k = bisect.bisect_left(self.A,x)
        self.B.update(k,c)
        self.num += c
    
    def delete_val(self,x,c=1):
        k = bisect.bisect_left(self.A,x)
        self.B.update(k,-c)
        self.num -= c
    
    # find the k-th min_val (k:0-indexed)
    def find_kth_val(self,k):
        if self.num <= k:
            ##### MINIMUM VAL #######
            return -10**9
        return self.A[self.B.lower_left(k+1)]
    
    # count the number of values lower than or equal to x
    def count_lower(self,x):
        if x < self.A[0]:
            return 0
        return self.B.query(bisect.bisect_right(self.A,x)-1)

    # min_val higher than x
    def find_higher(self,x):
        return self.find_kth_val(self.count_lower(x))
        
    # min_val lower than x
    def find_lower(self,x):
        return self.find_kth_val(self.count_lower(x)-1)
        
N = int(input())
A = list(map(int,input().split()))
mod = 10**9 + 7

def segfunc(x, y):
    return (x+y)%mod

ide_ele = 0

class SegTree:
    """
    Segment Tree
    """

    def __init__(self, init_val, segfunc, ide_ele):
        """
        初期化

        init_val: 配列の初期値
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新

        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る

        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res





c = Counter(A)
ans = 0
X = [(A[i],i) for i in range(N)]
X.sort()
cnd = []
U = SegTree([0 for _ in range(N)],segfunc,ide_ele)
V = SegTree([0 for _ in range(N)],segfunc,ide_ele)

for i in range(N):
    a,t = X[i]
    c[a] -= 1
    cnd.append(t)
    if c[a]==0:
        for j in cnd:
            ans += U.query(j+1,N)*V.query(0,j)
            ans %= mod
        for j in cnd:
            U.update(j,pow(2,N-1-j,mod))
            V.update(j,pow(2,j,mod))
        cnd = []

U = SegTree([0 for _ in range(N)],segfunc,ide_ele)
V = SegTree([0 for _ in range(N)],segfunc,ide_ele)
c = Counter(A)
for i in range(N-1,-1,-1):
    a,t = X[i]
    c[a] -= 1
    cnd.append(t)
    if c[a]==0:
        for j in cnd:
            ans += U.query(j+1,N)*V.query(0,j)
            ans %= mod
        for j in cnd:
            U.update(j,pow(2,N-1-j,mod))
            V.update(j,pow(2,j,mod))
        cnd = []
print(ans)
    
0