結果

問題 No.1975 Zigzag Sequence
ユーザー ygd.ygd.
提出日時 2022-06-16 21:20:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 425 ms / 2,000 ms
コード長 3,383 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 140,544 KB
最終ジャッジ日時 2024-04-16 10:47:45
合計ジャッジ時間 10,844 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
66,688 KB
testcase_01 AC 50 ms
66,688 KB
testcase_02 AC 51 ms
66,560 KB
testcase_03 AC 149 ms
84,096 KB
testcase_04 AC 131 ms
84,480 KB
testcase_05 AC 330 ms
100,992 KB
testcase_06 AC 287 ms
104,320 KB
testcase_07 AC 149 ms
83,840 KB
testcase_08 AC 253 ms
92,800 KB
testcase_09 AC 371 ms
120,192 KB
testcase_10 AC 282 ms
99,840 KB
testcase_11 AC 314 ms
102,528 KB
testcase_12 AC 155 ms
84,352 KB
testcase_13 AC 52 ms
66,816 KB
testcase_14 AC 51 ms
66,560 KB
testcase_15 AC 51 ms
66,560 KB
testcase_16 AC 52 ms
66,816 KB
testcase_17 AC 52 ms
66,432 KB
testcase_18 AC 102 ms
97,152 KB
testcase_19 AC 102 ms
97,152 KB
testcase_20 AC 284 ms
99,456 KB
testcase_21 AC 307 ms
99,456 KB
testcase_22 AC 381 ms
140,160 KB
testcase_23 AC 351 ms
140,544 KB
testcase_24 AC 388 ms
137,728 KB
testcase_25 AC 389 ms
138,112 KB
testcase_26 AC 329 ms
98,304 KB
testcase_27 AC 424 ms
123,904 KB
testcase_28 AC 377 ms
137,600 KB
testcase_29 AC 385 ms
137,344 KB
testcase_30 AC 397 ms
137,344 KB
testcase_31 AC 425 ms
137,472 KB
testcase_32 AC 184 ms
97,280 KB
testcase_33 AC 186 ms
97,152 KB
testcase_34 AC 367 ms
105,472 KB
testcase_35 AC 339 ms
105,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]

#和のセグ木にするときは以下の関数を定義する必要あり。
def segfunc(x,y):
  return x+y

class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v):
        self.update(i, self[i] + v)

    def sum(self, l, r): #これで[l,r)から取り出す。
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)


def compress(S):
    S = set(S); S = list(S)
    S.sort()
    dic_comp_ori = {}
    dic_ori_comp = {}
    for i,a in enumerate(S):
        dic_ori_comp[a] = i
        dic_comp_ori[i] = a
    return dic_comp_ori, dic_ori_comp 

MAX = 110000

two = [1]
for i in range(MAX):
    two.append(two[-1]*2%MOD)

def main():
    N = int(input())
    A = list(map(int,input().split()))
    dic_comp_ori, dic_ori_comp = compress(A)

    A = [dic_ori_comp[a] for a in A]

    # print(A)
    n = max(A) + 1 #Aの最大値

    left_tree = SegmentTree([0 for _ in range(n)], segfunc, 0)
    right_tree = SegmentTree([0 for _ in range(n)], segfunc, 0)

    for i,a in enumerate(A):
        val = two[N-1-i]
        # print("i",i,"val",val)
        right_tree.add(a,val)
    
    ans = 0

    for i,a in enumerate(A):
        # TL = []; TR = []
        # for j in range(n):
        #     TL.append(left_tree[j])
        #     TR.append(right_tree[j])
        # print(TL)
        # print(TR)

        left_small = left_tree.sum(0,a) #[0,a)
        right_small = right_tree.sum(0,a) #[a+1,MAX)
        left_large = left_tree.sum(a+1,n) #[0,a)
        right_large = right_tree.sum(a+1,n) #[a+1,MAX)

        # print("small",left_small,right_small)
        # print("large",left_large,right_large)
        temp = (left_small*right_small + left_large*right_large)%MOD
        ans += temp
        ans %= MOD
        # print(left,right,temp)
        left_val = two[i]
        right_val = two[N-1-i]
        left_tree.add(a,left_val)
        right_tree.add(a,-right_val)

    print(ans)


if __name__ == '__main__':
    main()
0