結果

問題 No.1975 Zigzag Sequence
ユーザー magstamagsta
提出日時 2021-08-07 19:13:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 2,438 bytes
コンパイル時間 2,124 ms
コンパイル使用メモリ 183,864 KB
実行使用メモリ 21,976 KB
最終ジャッジ日時 2023-10-21 06:04:15
合計ジャッジ時間 7,152 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 20 ms
4,620 KB
testcase_04 AC 9 ms
4,348 KB
testcase_05 AC 153 ms
9,636 KB
testcase_06 AC 118 ms
10,164 KB
testcase_07 AC 22 ms
4,672 KB
testcase_08 AC 95 ms
6,996 KB
testcase_09 AC 180 ms
13,332 KB
testcase_10 AC 115 ms
9,108 KB
testcase_11 AC 125 ms
9,636 KB
testcase_12 AC 28 ms
4,656 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 71 ms
8,052 KB
testcase_19 AC 71 ms
8,052 KB
testcase_20 AC 116 ms
9,372 KB
testcase_21 AC 117 ms
9,372 KB
testcase_22 AC 175 ms
20,392 KB
testcase_23 AC 173 ms
20,656 KB
testcase_24 AC 190 ms
20,656 KB
testcase_25 AC 194 ms
20,656 KB
testcase_26 AC 159 ms
9,108 KB
testcase_27 AC 232 ms
15,444 KB
testcase_28 AC 192 ms
21,976 KB
testcase_29 AC 188 ms
21,976 KB
testcase_30 AC 196 ms
21,976 KB
testcase_31 AC 191 ms
21,976 KB
testcase_32 AC 74 ms
8,052 KB
testcase_33 AC 74 ms
8,052 KB
testcase_34 AC 161 ms
10,692 KB
testcase_35 AC 161 ms
10,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct SegTree {
    const T INF = numeric_limits<T>::max();
    int n;
    vector<T> dat;
    SegTree(int n_) : dat(n_ * 4, 0) {
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
    }
    void init(T x) {
        for (int i = 0; i < 2 * n - 1; i++) dat[i] = x;
    }
    void add(int i, T x) {
        i += n - 1;
        dat[i] += x;
        while (i > 0) {
            i = (i - 1) / 2;
            dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];
        }
    }
    T query(int a, int b) {
        return query_sub(a, b, 0, 0, n);
    }
    T query_sub(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) {
            return 0;
        }
        else if (a <= l && r <= b) {
            return dat[k];
        }
        else {
            T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return vl + vr;
        }
    }
};

#define MOD 1000000007

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main() {
    long long N; cin >> N;
    vector<long long> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    set<long long> Set;
    for (int i = 0; i < N; i++) {
        Set.insert(A[i]);
    }

    long long count = 0;
    map<long long, long long> Map;
    while (!Set.empty()) {
        long long a = *Set.begin();
        Set.erase(Set.begin());
        Map[a] = count;
        count++;
    }

    vector<long long> A_(N);
    for (int i = 0; i < N; i++) A_[i] = Map[A[i]];

    vector<long long> dp(N), dp2(N), dp3(N), dp4(N);

    SegTree<long long> Seg1(count + 10);
    for (int i = 0; i < N; i++) {
        dp[i] = Seg1.query(0, A_[i]) % MOD;
        dp2[i] = Seg1.query(A_[i] + 1, count + 5) % MOD;
        Seg1.add(A_[i], modpow(2, i, MOD));
    }
    SegTree<long long> Seg2(count + 10);
    for (int i = N - 1; i >= 0; i--) {
        dp3[i] = Seg2.query(0, A_[i]) % MOD;
        dp4[i] = Seg2.query(A_[i] + 1, count + 5) % MOD;
        Seg2.add(A_[i], modpow(2, N - i - 1, MOD));
    }

    long long ans = 0;
    for (int i = 0; i < N; i++) {
        ans += dp[i] * dp3[i] % MOD;
        ans += dp2[i] * dp4[i] % MOD;
    }

    cout << ans % MOD << endl;
}
0