結果

問題 No.1975 Zigzag Sequence
ユーザー 👑 kakel-sankakel-san
提出日時 2023-10-03 22:14:58
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 2,983 bytes
コンパイル時間 2,206 ms
コンパイル使用メモリ 67,820 KB
実行使用メモリ 49,192 KB
最終ジャッジ日時 2023-10-03 22:15:11
合計ジャッジ時間 12,399 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
22,644 KB
testcase_01 AC 69 ms
22,668 KB
testcase_02 AC 70 ms
24,568 KB
testcase_03 AC 91 ms
24,304 KB
testcase_04 AC 84 ms
23,848 KB
testcase_05 AC 177 ms
35,848 KB
testcase_06 AC 155 ms
33,736 KB
testcase_07 AC 93 ms
24,268 KB
testcase_08 AC 146 ms
32,432 KB
testcase_09 AC 190 ms
37,628 KB
testcase_10 AC 149 ms
33,680 KB
testcase_11 AC 157 ms
34,996 KB
testcase_12 AC 98 ms
25,048 KB
testcase_13 AC 73 ms
22,624 KB
testcase_14 AC 70 ms
24,656 KB
testcase_15 AC 69 ms
22,608 KB
testcase_16 AC 69 ms
22,688 KB
testcase_17 AC 69 ms
22,560 KB
testcase_18 AC 155 ms
33,060 KB
testcase_19 AC 159 ms
35,188 KB
testcase_20 AC 174 ms
38,348 KB
testcase_21 AC 179 ms
38,392 KB
testcase_22 AC 232 ms
49,192 KB
testcase_23 AC 232 ms
47,876 KB
testcase_24 AC 234 ms
44,960 KB
testcase_25 AC 237 ms
49,156 KB
testcase_26 AC 185 ms
33,920 KB
testcase_27 AC 220 ms
39,820 KB
testcase_28 AC 234 ms
49,152 KB
testcase_29 AC 237 ms
48,996 KB
testcase_30 AC 234 ms
47,892 KB
testcase_31 AC 236 ms
47,920 KB
testcase_32 AC 158 ms
37,180 KB
testcase_33 AC 156 ms
35,132 KB
testcase_34 AC 180 ms
38,656 KB
testcase_35 AC 179 ms
34,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var a = NList;
        WriteLine(Zig(n, a));
    }
    static long Zig(int n, int[] a)
    {
        var list = new List<int>(new HashSet<int>(a));
        list.Add(0);
        list.Sort();
        var dic = new Dictionary<int, int>();
        for (var i = 0; i < list.Count; ++i) dic[list[i]] = i;
        var mod = 1_000_000_007;
        var two = new long[n];
        two[0] = 1;
        for (var i = 1; i < two.Length; ++i) two[i] = two[i - 1] * 2 % mod;
        var ld = new long[n];
        var lu = new long[n];
        var ft = new FenwickTree(list.Count);
        for (var i = 0; i < n; ++i)
        {
            ld[i] = (ft.Sum(list.Count - 1) % mod + mod - ft.Sum(dic[a[i]]) % mod) % mod;
            lu[i] = ft.Sum(dic[a[i]] - 1) % mod;
            ft.Add(dic[a[i]], two[i]);
        }
        ft = new FenwickTree(list.Count);
        var rd = new long[n];
        var ru = new long[n];
        for (var i = n - 1; i >= 0; --i)
        {
            rd[i] = (ft.Sum(list.Count - 1) % mod + mod - ft.Sum(dic[a[i]]) % mod) % mod;
            ru[i] = ft.Sum(dic[a[i]] - 1) % mod;
            ft.Add(dic[a[i]], two[n - i - 1]);
        }
        var ans = 0L;
        for (var i = 0; i < n; ++i) ans = (ans + ld[i] * rd[i] % mod + lu[i] * ru[i] % mod) % mod;
        return ans;
    }
    class FenwickTree
    {
        int size;
        long[] tree;
        public FenwickTree(int size)
        {
            this.size = size;
            tree = new long[size + 2];
        }
        public void Add(int index, long value)
        {
            ++index;
            for (var x = index; x <= size; x += (x & -x)) tree[x] += value;
        }
        /// <summary>先頭からindexまでの和(include index)</summary>
        public long Sum(int index)
        {
            if (index < 0) return 0;
            ++index;
            var sum = 0L;
            for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];
            return sum;
        }
        /// <summary>Sum(x) >= value となる最小のxを求める</summary>
        // 各要素は非負であること
        public int LowerBound(long value)
        {
            if (value < 0) return -1;
            var x = 0;
            var b = 1;
            while (b * 2 <= size) b <<= 1;
            for (var k = b; k > 0; k >>= 1)
            {
                if (x + k <= size && tree[x + k] < value)
                {
                    value -= tree[x + k];
                    x += k;
                }
            }
            return x;
        }
    }
}
0