結果
問題 | No.1031 いたずら好きなお姉ちゃん |
ユーザー | はまやんはまやん |
提出日時 | 2020-04-17 23:18:27 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 184 ms / 3,500 ms |
コード長 | 6,135 bytes |
コンパイル時間 | 2,504 ms |
コンパイル使用メモリ | 213,124 KB |
実行使用メモリ | 50,692 KB |
最終ジャッジ日時 | 2024-10-03 15:18:28 |
合計ジャッジ時間 | 11,039 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 8 ms
21,132 KB |
testcase_01 | AC | 8 ms
21,008 KB |
testcase_02 | AC | 9 ms
21,140 KB |
testcase_03 | AC | 12 ms
21,924 KB |
testcase_04 | AC | 12 ms
21,920 KB |
testcase_05 | AC | 13 ms
21,924 KB |
testcase_06 | AC | 12 ms
21,916 KB |
testcase_07 | AC | 12 ms
21,920 KB |
testcase_08 | AC | 12 ms
21,920 KB |
testcase_09 | AC | 12 ms
21,916 KB |
testcase_10 | AC | 12 ms
21,920 KB |
testcase_11 | AC | 127 ms
46,332 KB |
testcase_12 | AC | 128 ms
46,288 KB |
testcase_13 | AC | 149 ms
46,620 KB |
testcase_14 | AC | 131 ms
46,536 KB |
testcase_15 | AC | 140 ms
46,600 KB |
testcase_16 | AC | 128 ms
46,288 KB |
testcase_17 | AC | 143 ms
46,600 KB |
testcase_18 | AC | 136 ms
46,460 KB |
testcase_19 | AC | 142 ms
46,596 KB |
testcase_20 | AC | 148 ms
46,616 KB |
testcase_21 | AC | 137 ms
46,452 KB |
testcase_22 | AC | 125 ms
46,288 KB |
testcase_23 | AC | 127 ms
46,316 KB |
testcase_24 | AC | 142 ms
46,732 KB |
testcase_25 | AC | 146 ms
46,600 KB |
testcase_26 | AC | 139 ms
46,584 KB |
testcase_27 | AC | 130 ms
46,440 KB |
testcase_28 | AC | 148 ms
46,616 KB |
testcase_29 | AC | 150 ms
46,620 KB |
testcase_30 | AC | 148 ms
46,620 KB |
testcase_31 | AC | 149 ms
46,620 KB |
testcase_32 | AC | 149 ms
46,620 KB |
testcase_33 | AC | 149 ms
46,620 KB |
testcase_34 | AC | 172 ms
50,692 KB |
testcase_35 | AC | 168 ms
47,236 KB |
testcase_36 | AC | 168 ms
47,748 KB |
testcase_37 | AC | 169 ms
48,640 KB |
testcase_38 | AC | 164 ms
46,620 KB |
testcase_39 | AC | 163 ms
46,920 KB |
testcase_40 | AC | 166 ms
46,612 KB |
testcase_41 | AC | 165 ms
46,968 KB |
testcase_42 | AC | 167 ms
46,704 KB |
testcase_43 | AC | 165 ms
46,744 KB |
testcase_44 | AC | 167 ms
46,608 KB |
testcase_45 | AC | 162 ms
46,600 KB |
testcase_46 | AC | 170 ms
49,000 KB |
testcase_47 | AC | 164 ms
46,812 KB |
testcase_48 | AC | 165 ms
47,112 KB |
testcase_49 | AC | 167 ms
47,232 KB |
testcase_50 | AC | 166 ms
47,860 KB |
testcase_51 | AC | 183 ms
48,388 KB |
testcase_52 | AC | 184 ms
48,388 KB |
testcase_53 | AC | 182 ms
48,388 KB |
testcase_54 | AC | 8 ms
21,136 KB |
testcase_55 | AC | 8 ms
21,008 KB |
ソースコード
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- #ifdef _MSC_VER inline unsigned int __builtin_clz(unsigned int x){unsigned long r;_BitScanReverse(&r,x);return 31-r;} #endif // _MSC_VER template<class V> struct SparseTable { // [L,R) const V def = make_pair(-1,-1); inline V comp(V a, V b) { return max(a,b); } int n; vector<V> a, b[22]; inline int __lg(int x) { return 32 - 1 - __builtin_clz(x); } void init(vector<V> v) { int nn = v.size(); n = 1; while (n < nn) n *= 2; a.resize(n); rep(i, 0, 22) b[i].resize(n); rep(i, 0, nn) a[i] = v[i]; int d = 1 << __lg(n - 1), e = d << 1; for (int h = 0, f; (f = 1 << h) <= d; ++h) { for (int i = f, r = f << 1; i < e; i += r) { b[h][i - 1] = a[i - 1]; for (int j = i - 2; j >= i - f; --j) b[h][j] = comp(b[h][j + 1], a[j]); b[h][i] = a[i]; for (int j = i + 1; j < i + f; ++j) b[h][j] = comp(b[h][j - 1], a[j]); } } } V get(int L,int R){assert(0<=L&&L<=R);if(L==R)return def;R--;if(L==R)return a[L];int h=__lg(L^R); return comp(b[h][L],b[h][R]);}}; int defmin = inf; template<class V, int NV> struct SegTreeMin { //[l,r) V comp(V& l, V& r) { return min(l, r); }; vector<V> val; SegTreeMin() { val = vector<V>(NV * 2, defmin); } V get(int x, int y, int l = 0, int r = NV, int k = 1) { if (r <= x || y <= l)return defmin; if (x <= l && r <= y)return val[k]; auto a = get(x, y, l, (l + r) / 2, k * 2); auto b = get(x, y, (l + r) / 2, r, k * 2 + 1); return comp(a, b); } void update(int i, V v) { i += NV; val[i] = v; while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); } void add(int i, V v) { update(i, val[i + NV] + v); } V operator[](int x) { return get(x, x + 1); } }; #define def 0 template<class V, int NV> struct SegTreeMax { //[l,r) V comp(V& l, V& r) { return max(l, r); }; vector<V> val; SegTreeMax() { val = vector<V>(NV * 2, def); } V get(int x, int y, int l = 0, int r = NV, int k = 1) { if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k]; auto a = get(x, y, l, (l + r) / 2, k * 2); auto b = get(x, y, (l + r) / 2, r, k * 2 + 1); return comp(a, b); } void update(int i, V v) { i += NV; val[i] = v; while (i > 1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); } void add(int i, V v) { update(i, val[i + NV] + v); } V operator[](int x) { return get(x, x + 1); } }; /*--------------------------------------------------------------------------------------------------- ∧_∧ ∧_∧ (´<_` ) Welcome to My Coding Space! ( ´_ゝ`) / ⌒i @hamayanhamayan0 / \ | | / / ̄ ̄ ̄ ̄/ | __(__ニつ/ _/ .| .|____ \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, A[101010]; SparseTable<pair<int, int>> st; //--------------------------------------------------------------------------------------------------- int pre[20][101010]; int post[20][101010]; SegTreeMin<int, 1 << 17> stmin; SegTreeMax<int, 1 << 17> stmax; void init() { rep(p, 0, 20) rep(idx, 0, 101010) pre[p][idx] = 0; rep(i, 0, N) { pre[0][i + 1] = stmax.get(0, A[i]); stmax.update(A[i], i + 1); } rep(p, 1, 20) rep(idx, 0, N + 1) pre[p][idx] = pre[p - 1][pre[p - 1][idx]]; defmin = N + 1; rep(p, 0, 20) rep(idx, 0, 101010) post[p][idx] = N + 1; rrep(i, N - 1, 0) { post[0][i + 1] = stmin.get(0, A[i]); stmin.update(A[i], i + 1); } rep(p, 1, 20) rep(idx, 0, N + 1) post[p][idx] = post[p - 1][post[p - 1][idx]]; } int getpre(int idx, int cnt) { int res = idx; rep(p, 0, 20) if (cnt & (1 << p)) res = pre[p][res]; return res; } int getpost(int idx, int cnt) { int res = idx; rep(p, 0, 20) if (cnt & (1 << p)) res = post[p][res]; return res; } //--------------------------------------------------------------------------------------------------- ll ans = 0; void f(int L, int R) { if (L == R) return; if (L + 1 == R) return; auto ma = st.get(L, R); int C = ma.second; { int ok = 0, ng = N + 1; while (ok + 1 != ng) { int md = (ok + ng) / 2; if (L <= getpre(C, md)) ok = md; else ng = md; } ans += ok; //printf("[%d %d] left %d\n", L, R, ok); } { int ok = 0, ng = N + 1; while (ok + 1 != ng) { int md = (ok + ng) / 2; if (getpost(C, md) < R) ok = md; else ng = md; } ans += ok; //printf("[%d %d] right %d\n", L, R, ok); } f(L, C); f(C + 1, R); } //--------------------------------------------------------------------------------------------------- void _main() { cin >> N; rep(i, 0, N) cin >> A[i]; init(); vector<pair<int, int>> dic; dic.push_back({ -1, -1 }); rep(i, 0, N) dic.push_back({ A[i], i + 1 }); st.init(dic); f(1, N + 1); cout << ans << endl; }