結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー 👑 emthrmemthrm
提出日時 2020-04-18 00:24:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 3,500 ms
コード長 3,347 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 200,956 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 16:19:30
合計ジャッジ時間 6,350 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 4 ms
6,940 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 44 ms
6,940 KB
testcase_12 AC 43 ms
6,940 KB
testcase_13 AC 53 ms
6,944 KB
testcase_14 AC 45 ms
6,940 KB
testcase_15 AC 50 ms
6,940 KB
testcase_16 AC 44 ms
6,940 KB
testcase_17 AC 50 ms
6,940 KB
testcase_18 AC 48 ms
6,944 KB
testcase_19 AC 51 ms
6,944 KB
testcase_20 AC 54 ms
6,940 KB
testcase_21 AC 49 ms
6,940 KB
testcase_22 AC 44 ms
6,940 KB
testcase_23 AC 45 ms
6,944 KB
testcase_24 AC 51 ms
6,940 KB
testcase_25 AC 53 ms
6,940 KB
testcase_26 AC 49 ms
6,940 KB
testcase_27 AC 46 ms
6,944 KB
testcase_28 AC 54 ms
6,940 KB
testcase_29 AC 54 ms
6,940 KB
testcase_30 AC 53 ms
6,944 KB
testcase_31 AC 54 ms
6,944 KB
testcase_32 AC 54 ms
6,944 KB
testcase_33 AC 54 ms
6,940 KB
testcase_34 AC 44 ms
6,940 KB
testcase_35 AC 43 ms
6,940 KB
testcase_36 AC 43 ms
6,944 KB
testcase_37 AC 45 ms
6,944 KB
testcase_38 AC 45 ms
6,944 KB
testcase_39 AC 43 ms
6,944 KB
testcase_40 AC 45 ms
6,940 KB
testcase_41 AC 44 ms
6,944 KB
testcase_42 AC 44 ms
6,944 KB
testcase_43 AC 44 ms
6,944 KB
testcase_44 AC 43 ms
6,944 KB
testcase_45 AC 42 ms
6,944 KB
testcase_46 AC 47 ms
6,940 KB
testcase_47 AC 44 ms
6,940 KB
testcase_48 AC 47 ms
6,944 KB
testcase_49 AC 47 ms
6,944 KB
testcase_50 AC 47 ms
6,944 KB
testcase_51 AC 45 ms
6,944 KB
testcase_52 AC 44 ms
6,944 KB
testcase_53 AC 44 ms
6,944 KB
testcase_54 AC 2 ms
6,944 KB
testcase_55 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Monoid>
struct RMQ {
  RMQ(int sz, const Monoid UNITY) : UNITY(UNITY) {
    init(sz);
    dat.assign((n << 1) - 1, UNITY);
  }

  RMQ(const vector<Monoid> &a, const Monoid UNITY) : UNITY(UNITY) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize((n << 1) - 1);
    REP(i, a_sz) dat[n - 1 + i] = a[i];
    for (int i = n - 2; i >= 0; --i) dat[i] = min(dat[(i << 1) + 1], dat[(i << 1) + 2]);
  }

  void update(int node, Monoid val) {
    node += n - 1;
    dat[node] = val;
    while (node > 0) {
      node = (node - 1) >> 1;
      dat[node] = min(dat[(node << 1) + 1], dat[(node << 1) + 2]);
    }
  }

  Monoid query(int a, int b) { return query(a, b, 0, 0, n); }

  int find(int a, int b, Monoid val) { return find(a, b, val, 0, 0, n); }

  Monoid operator[](const int idx) const { return dat[idx + n - 1]; }

private:
  int n = 1;
  const Monoid UNITY;
  vector<Monoid> dat;

  void init(int sz) { while (n < sz) n <<= 1; }

  Monoid query(int a, int b, int node, int left, int right) {
    if (right <= a || b <= left) return UNITY;
    if (a <= left && right <= b) return dat[node];
    return min(query(a, b, (node << 1) + 1, left, (left + right) >> 1), query(a, b, (node << 1) + 2, (left + right) >> 1, right));
  }

  int find(int a, int b, Monoid val, int node, int left, int right) {
    if (dat[node] > val || right <= a || b <= left) return -1;
    if (right - left == 1) return node - (n - 1);
    int res_l = find(a, b, val, (node << 1) + 1, left, (left + right) >> 1);
    if (res_l != -1) return res_l;
    return find(a, b, val, (node << 1) + 2, (left + right) >> 1, right);
  }
};

ll solve(const vector<int> &p) {
  int n = p.size();
  ll ans = 0;
  RMQ<int> rmq(n, INF);
  rmq.update(p[n - 1], n - 1);
  vector<int> v{n - 1};
  for (int i = n - 2; i >= 0; --i) {
    while (!v.empty() && p[v.back()] < p[i]) v.pop_back();
    // cout << i << ": ";
    // for (int e : v) cout << e << ' ';
    // cout << '\n';
    int idx = rmq.query(0, p[i] + 1), lb = -1, ub = v.size();
    // cout << i << ' ' << idx << '\n';
    while (ub - lb > 1) {
      int mid = (lb + ub) >> 1;
      (v[mid] < idx ? ub : lb) = mid;
    }
    // cout << ub << '\n';
    ans += v.size() - ub;
    // cout << i << ' ' << ans << '\n';
    rmq.update(p[i], i);
    v.emplace_back(i);
  }
  return ans;
}

int main() {
  int n; cin >> n;
  vector<int> p(n); REP(i, n) cin >> p[i], --p[i];
  ll ans = solve(p);
  reverse(ALL(p));
  ans += solve(p);
  cout << ans << '\n';
  return 0;
}
0