結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー risujirohrisujiroh
提出日時 2020-04-17 21:55:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 146 ms / 3,500 ms
コード長 2,073 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 208,952 KB
実行使用メモリ 24,832 KB
最終ジャッジ日時 2024-04-14 14:04:36
合計ジャッジ時間 10,157 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 5 ms
6,944 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 5 ms
6,944 KB
testcase_08 AC 5 ms
6,940 KB
testcase_09 AC 5 ms
6,944 KB
testcase_10 AC 5 ms
6,940 KB
testcase_11 AC 115 ms
20,864 KB
testcase_12 AC 114 ms
20,736 KB
testcase_13 AC 131 ms
24,704 KB
testcase_14 AC 120 ms
21,504 KB
testcase_15 AC 132 ms
23,296 KB
testcase_16 AC 112 ms
20,632 KB
testcase_17 AC 140 ms
23,504 KB
testcase_18 AC 130 ms
22,656 KB
testcase_19 AC 138 ms
23,552 KB
testcase_20 AC 143 ms
24,576 KB
testcase_21 AC 126 ms
22,656 KB
testcase_22 AC 112 ms
20,504 KB
testcase_23 AC 118 ms
21,120 KB
testcase_24 AC 137 ms
23,680 KB
testcase_25 AC 141 ms
24,448 KB
testcase_26 AC 132 ms
23,040 KB
testcase_27 AC 127 ms
21,632 KB
testcase_28 AC 144 ms
24,832 KB
testcase_29 AC 145 ms
24,704 KB
testcase_30 AC 145 ms
24,704 KB
testcase_31 AC 144 ms
24,832 KB
testcase_32 AC 146 ms
24,704 KB
testcase_33 AC 146 ms
24,704 KB
testcase_34 AC 139 ms
24,832 KB
testcase_35 AC 141 ms
24,704 KB
testcase_36 AC 141 ms
24,704 KB
testcase_37 AC 140 ms
24,832 KB
testcase_38 AC 132 ms
24,448 KB
testcase_39 AC 133 ms
23,808 KB
testcase_40 AC 130 ms
24,192 KB
testcase_41 AC 136 ms
24,320 KB
testcase_42 AC 134 ms
24,192 KB
testcase_43 AC 136 ms
24,576 KB
testcase_44 AC 141 ms
24,320 KB
testcase_45 AC 133 ms
23,680 KB
testcase_46 AC 135 ms
24,320 KB
testcase_47 AC 128 ms
24,064 KB
testcase_48 AC 133 ms
24,704 KB
testcase_49 AC 135 ms
24,576 KB
testcase_50 AC 137 ms
24,448 KB
testcase_51 AC 133 ms
24,704 KB
testcase_52 AC 134 ms
24,832 KB
testcase_53 AC 133 ms
24,704 KB
testcase_54 AC 2 ms
6,944 KB
testcase_55 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T> struct disjoint_sparse_table {
  vector<vector<T>> t;
  disjoint_sparse_table(const vector<T>& v) : t(1, v) {
    for (int k = 1, n = v.size(); 1 << k < n; ++k) {
      t.push_back(v);
      for (int m = 1 << k; m < n; m += 1 << (k + 1)) {
        partial_sum(rend(v) - m, rend(v) - (m - (1 << k)),
                    rend(t[k]) - m, [](T a, T b) { return b * a; });
        partial_sum(begin(v) + m, begin(v) + min(m + (1 << k), n),
                    begin(t[k]) + m, multiplies<>());
      }
    }
  }
  T fold(int l, int r) const {
    assert(l < r);
    if (l == --r) return t[0][l];
    int k = __lg(l ^ r);
    return t[k][l] * t[k][r];
  }
};

struct node {
  int mn, mx;
  node operator*(node b) const {
    return {min(mn, b.mn), max(mx, b.mx)};
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> p(n);
  for (auto&& e : p) {
    cin >> e;
    --e;
  }
  long long res = 0;
  for (int _ = 2; _--; ) {
    vector<node> _v(n);
    for (int i = 0; i < n; ++i) {
      _v[i] = {p[i], p[i]};
    }
    disjoint_sparse_table<node> st(_v);
    auto nxt = [&](int i, bool gt = true) {
      int ok = i + 1, ng = n + 1;
      while (ng - ok > 1) {
        int mid = (ok + ng) / 2;
        if (gt) {
          (st.fold(i, mid).mx <= p[i] ? ok : ng) = mid;
        } else {
          (st.fold(i, mid).mn >= p[i] ? ok : ng) = mid;
        }
      }
      return ok;
    };
    vector dp(17, vector<int>(n + 1, n));
    for (int i = 0; i < n; ++i) {
      dp[0][i] = nxt(i);
    }
    for (int k = 0; k < 16; ++k) {
      for (int i = 0; i < n; ++i) {
        dp[k + 1][i] = dp[k][dp[k][i]];
      }
    }
    for (int i = 0; i < n; ++i) {
      int j = nxt(i, false);
      int pos = i;
      for (int k = 17; k--; ) {
        if (dp[k][pos] < j) {
          res += 1 << k;
          pos = dp[k][pos];
        }
      }
    }
    reverse(begin(p), end(p));
  }
  cout << res << '\n';
}
0