結果

問題 No.1975 Zigzag Sequence
ユーザー SSRSSSRS
提出日時 2022-06-10 21:35:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,541 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 177,828 KB
実行使用メモリ 9,708 KB
最終ジャッジ日時 2023-10-21 04:55:09
合計ジャッジ時間 7,537 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 10 ms
4,348 KB
testcase_04 AC 5 ms
4,348 KB
testcase_05 AC 73 ms
7,860 KB
testcase_06 AC 50 ms
6,540 KB
testcase_07 AC 11 ms
4,360 KB
testcase_08 AC 50 ms
6,540 KB
testcase_09 AC 74 ms
7,860 KB
testcase_10 AC 51 ms
6,604 KB
testcase_11 AC 58 ms
7,068 KB
testcase_12 AC 15 ms
4,348 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 50 ms
8,124 KB
testcase_19 AC 50 ms
8,124 KB
testcase_20 AC 65 ms
8,124 KB
testcase_21 AC 63 ms
8,124 KB
testcase_22 AC 75 ms
9,708 KB
testcase_23 AC 75 ms
9,708 KB
testcase_24 AC 79 ms
9,708 KB
testcase_25 AC 77 ms
9,708 KB
testcase_26 AC 78 ms
8,124 KB
testcase_27 AC 88 ms
8,916 KB
testcase_28 AC 79 ms
9,708 KB
testcase_29 AC 79 ms
9,708 KB
testcase_30 AC 80 ms
9,708 KB
testcase_31 AC 79 ms
9,708 KB
testcase_32 AC 50 ms
8,124 KB
testcase_33 AC 50 ms
8,124 KB
testcase_34 AC 74 ms
8,388 KB
testcase_35 AC 75 ms
8,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
template <typename T>
struct binary_indexed_tree{
  int N;
  vector<T> BIT;
  binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
  }
  void add(int i, T x){
    i++;
    while (i <= N){
      BIT[i] += x;
      BIT[i] %= MOD;
      i += i & -i;
    }
  }
  T sum(int i){
    T ans = 0;
    while (i > 0){
      ans += BIT[i];
      ans %= MOD;
      i -= i & -i;
    }
    return ans;
  }
  T sum(int L, int R){
    return (sum(R) - sum(L) + MOD) % MOD;
  }
};
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<int> B = A;
  sort(B.begin(), B.end());
  B.erase(unique(B.begin(), B.end()), B.end());
  int cnt = B.size();
  for (int i = 0; i < N; i++){
    A[i] = lower_bound(B.begin(), B.end(), A[i]) - B.begin();
  }
  vector<long long> pow2(N);
  pow2[0] = 1;
  for (int i = 0; i < N - 1; i++){
    pow2[i + 1] = pow2[i] * 2 % MOD;
  }
  vector<long long> L1(N), L2(N), R1(N), R2(N);
  binary_indexed_tree<long long> BIT1(cnt);
  for (int i = 0; i < N; i++){
    L1[i] = BIT1.sum(0, A[i]);
    L2[i] = BIT1.sum(A[i] + 1, cnt);
    BIT1.add(A[i], pow2[i]);
  }
  binary_indexed_tree<long long> BIT2(cnt);
  for (int i = N - 1; i >= 0; i--){
    R1[i] = BIT2.sum(0, A[i]);
    R2[i] = BIT2.sum(A[i] + 1, cnt);
    BIT2.add(A[i], pow2[N - 1 - i]);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++){
    ans += L1[i] * R1[i];
    ans += L2[i] * R2[i];
    ans %= MOD;
  }
  cout << ans << endl;
}
0