結果
問題 | No.1031 いたずら好きなお姉ちゃん |
ユーザー | kcvlex |
提出日時 | 2020-04-19 00:19:17 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 301 ms / 3,500 ms |
コード長 | 4,196 bytes |
コンパイル時間 | 1,840 ms |
コンパイル使用メモリ | 156,256 KB |
実行使用メモリ | 29,172 KB |
最終ジャッジ日時 | 2024-10-04 00:33:39 |
合計ジャッジ時間 | 13,395 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 8 ms
5,248 KB |
testcase_04 | AC | 8 ms
5,248 KB |
testcase_05 | AC | 8 ms
5,248 KB |
testcase_06 | AC | 8 ms
5,248 KB |
testcase_07 | AC | 8 ms
5,248 KB |
testcase_08 | AC | 8 ms
5,248 KB |
testcase_09 | AC | 8 ms
5,248 KB |
testcase_10 | AC | 8 ms
5,248 KB |
testcase_11 | AC | 223 ms
24,448 KB |
testcase_12 | AC | 221 ms
24,228 KB |
testcase_13 | AC | 283 ms
28,940 KB |
testcase_14 | AC | 249 ms
25,196 KB |
testcase_15 | AC | 268 ms
27,400 KB |
testcase_16 | AC | 223 ms
24,024 KB |
testcase_17 | AC | 275 ms
27,544 KB |
testcase_18 | AC | 265 ms
26,540 KB |
testcase_19 | AC | 274 ms
27,616 KB |
testcase_20 | AC | 291 ms
28,828 KB |
testcase_21 | AC | 256 ms
26,604 KB |
testcase_22 | AC | 221 ms
24,020 KB |
testcase_23 | AC | 233 ms
24,728 KB |
testcase_24 | AC | 275 ms
27,844 KB |
testcase_25 | AC | 297 ms
28,772 KB |
testcase_26 | AC | 265 ms
26,888 KB |
testcase_27 | AC | 231 ms
25,316 KB |
testcase_28 | AC | 295 ms
29,044 KB |
testcase_29 | AC | 291 ms
29,044 KB |
testcase_30 | AC | 290 ms
29,048 KB |
testcase_31 | AC | 301 ms
29,172 KB |
testcase_32 | AC | 301 ms
28,916 KB |
testcase_33 | AC | 290 ms
29,048 KB |
testcase_34 | AC | 190 ms
29,024 KB |
testcase_35 | AC | 178 ms
29,020 KB |
testcase_36 | AC | 185 ms
29,020 KB |
testcase_37 | AC | 183 ms
29,016 KB |
testcase_38 | AC | 171 ms
28,664 KB |
testcase_39 | AC | 164 ms
27,856 KB |
testcase_40 | AC | 169 ms
28,472 KB |
testcase_41 | AC | 172 ms
28,600 KB |
testcase_42 | AC | 171 ms
28,488 KB |
testcase_43 | AC | 168 ms
28,692 KB |
testcase_44 | AC | 164 ms
28,488 KB |
testcase_45 | AC | 161 ms
27,788 KB |
testcase_46 | AC | 178 ms
28,236 KB |
testcase_47 | AC | 166 ms
28,136 KB |
testcase_48 | AC | 172 ms
28,884 KB |
testcase_49 | AC | 172 ms
28,956 KB |
testcase_50 | AC | 172 ms
28,680 KB |
testcase_51 | AC | 183 ms
28,956 KB |
testcase_52 | AC | 182 ms
29,044 KB |
testcase_53 | AC | 182 ms
28,920 KB |
testcase_54 | AC | 2 ms
5,248 KB |
testcase_55 | AC | 2 ms
5,248 KB |
ソースコード
#include <limits> #include <initializer_list> #include <utility> #include <bitset> #include <tuple> #include <type_traits> #include <functional> #include <string> #include <array> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <iterator> #include <algorithm> #include <complex> #include <random> #include <numeric> #include <iostream> #include <iomanip> #include <sstream> #include <regex> #include <cassert> #include <cstddef> #define endl codeforces #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair<int, int>; using tii = std::tuple<int, int, int>; using pll = std::pair<ll, ll>; using tll = std::tuple<ll, ll, ll>; template <typename T> using vec = std::vector<T>; template <typename T> using vvec = vec<vec<T>>; template <typename T> const T& var_min(const T &t) { return t; } template <typename T> const T& var_max(const T &t) { return t; } template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); } template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); } template <typename T> T make_v(T init) { return init; } template <typename T, typename... Tail> auto make_v(T init, std::size_t s, Tail... tail) { auto v = std::move(make_v(init, tail...)); return vec<decltype(v)>(s, v); } template <typename T, std::size_t Head, std::size_t ...Tail> struct multi_dem_array { using type = std::array<typename multi_dem_array<T, Tail...>::type, Head>; }; template <typename T, std::size_t Head> struct multi_dem_array<T, Head> { using type = std::array<T, Head>; }; template <typename T, std::size_t ...Args> using mdarray = typename multi_dem_array<T, Args...>::type; namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } const ll inf = 5e15; vec<ll> cnt_l(vec<ll> p) { ll n = p.size(); p.push_back(n); vec<ll> rp(n + 1); for (ll i = 0; i < n; i++) rp[p[i]] = i; auto adjust = [&] { std::set<ll> unused; vec<ll> ret(n + 1, n); for (ll i = 0; i <= n; i++) unused.insert(i); for (ll e = n - 1; 0 <= e; e--) { ll pos = rp[e]; unused.erase(pos); auto ite = unused.lower_bound(pos); ret[e] = *ite; } return ret; }(); auto dub = make_v<ll>(0, 20, n + 1); for (ll i = 0; i <= n; i++) dub[0][i] = adjust[i]; for (ll i = 0; i + 1 < 20; i++) for (ll j = 0; j <= n; j++) { ll pos = dub[i][j]; ll val = p[pos]; dub[i + 1][j] = dub[i][val]; } auto get_cnt = [&](ll pos, ll r) { ll ret = 0; for (ll i = 19; 0 <= i; i--) { ll mask = 1ll << i; ll nxt = dub[i][p[pos]]; if (r <= nxt) continue; ret += mask; pos = nxt; } return ret; }; vec<ll> ret(n); std::set<ll> used; used.insert(n); for (ll e = n - 1; 0 <= e; e--) { ll pos = rp[e]; ll r = *used.lower_bound(pos); ret[e] = get_cnt(pos, r); used.insert(pos); } return ret; } int main() { ll n; std::cin >> n; vec<ll> p(n); for (ll &e : p) { std::cin >> e; e--; } auto lv = cnt_l(p); std::reverse(ALL(p)); auto rv = cnt_l(p); ll ans = 0; for (ll i = 0; i < n; i++) ans += lv[i] + rv[i]; std::cout << ans << '\n'; return 0; }