結果
問題 | No.1435 Mmm...... |
ユーザー | se1ka2 |
提出日時 | 2021-03-19 22:59:07 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,717 ms / 2,000 ms |
コード長 | 3,662 bytes |
コンパイル時間 | 1,161 ms |
コンパイル使用メモリ | 84,044 KB |
実行使用メモリ | 10,432 KB |
最終ジャッジ日時 | 2023-08-12 03:56:09 |
合計ジャッジ時間 | 26,675 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
4,980 KB |
testcase_01 | AC | 3 ms
6,300 KB |
testcase_02 | AC | 3 ms
5,056 KB |
testcase_03 | AC | 3 ms
6,184 KB |
testcase_04 | AC | 2 ms
5,988 KB |
testcase_05 | AC | 2 ms
5,816 KB |
testcase_06 | AC | 796 ms
9,852 KB |
testcase_07 | AC | 991 ms
9,976 KB |
testcase_08 | AC | 1,167 ms
10,240 KB |
testcase_09 | AC | 1,066 ms
10,208 KB |
testcase_10 | AC | 911 ms
9,860 KB |
testcase_11 | AC | 892 ms
10,340 KB |
testcase_12 | AC | 823 ms
9,944 KB |
testcase_13 | AC | 805 ms
10,216 KB |
testcase_14 | AC | 568 ms
7,640 KB |
testcase_15 | AC | 1,217 ms
10,432 KB |
testcase_16 | AC | 986 ms
10,320 KB |
testcase_17 | AC | 653 ms
7,640 KB |
testcase_18 | AC | 1,234 ms
10,160 KB |
testcase_19 | AC | 873 ms
9,980 KB |
testcase_20 | AC | 1,110 ms
10,100 KB |
testcase_21 | AC | 1,717 ms
10,164 KB |
testcase_22 | AC | 1,643 ms
10,176 KB |
testcase_23 | AC | 1,289 ms
10,164 KB |
testcase_24 | AC | 1,285 ms
10,280 KB |
testcase_25 | AC | 1,288 ms
10,376 KB |
testcase_26 | AC | 1,286 ms
10,212 KB |
testcase_27 | AC | 1,284 ms
10,280 KB |
ソースコード
#include <functional> #include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 2000000005; template <typename T> struct SegmentTree { using F = function<T(T, T)>; const F f; const T e; int n; vector<T> seg; SegmentTree(int nn, const F f, const T e) : f(f), e(e){ n = 1; while(n < nn) n <<= 1; seg.assign(n * 2, e); } void set(int i, T x){ seg[i + n] = x; } void build(){ for(int k = n - 1; k > 0; k--){ seg[k] = f(seg[k * 2], seg[k * 2 + 1]); } } void update(int i, T x){ int k = i + n; seg[k] = x; while(k >>= 1){ seg[k] = f(seg[k * 2], seg[k * 2 + 1]); } } T query(int l, int r){ l += n, r += n; T L = e, R = e; for(; l != r; l >>= 1, r >>= 1){ if(l % 2) L = f(L, seg[l++]); if(r % 2) R = f(seg[--r], R); } return f(L, R); } T operator[](const int i)const { return seg[i + n]; } template<typename C> int right_bound_sub(int k, const C &check, T x){ while(k < n){ if(check(f(x, seg[k * 2]))){ k = k * 2; } else{ x = f(x, seg[k * 2]); k = k * 2 + 1; } } return k - n; } template <typename C> int right_bound(int i, const C &check){ T x = e; for(int l = i + n, r = n * 2; l != r; l >>= 1, r >>= 1){ if(l % 2){ if(check(f(x, seg[l]))){ return right_bound_sub(l, check, x); } x = f(x, seg[l]); l++; } } return -1; } template <typename C> int left_bound_sub(int k, const C &check, T x){ while(k < n){ if(check(f(seg[k * 2 + 1], x))){ k = k * 2 + 1; } else{ x = f(seg[k * 2 + 1], x); k = k * 2; } } return k - n; } template <typename C> int left_bound(int i, const C &check){ T x = e; for(int l = n, r = i + n; l != r; l >>= 1, r >>= 1){ if(r % 2){ if(check(f(seg[--r], x))){ return left_bound_sub(r, check, x); } x = f(x, seg[r]); } } return -1; } }; int main() { int n; cin >> n; int a[200005]; for(int i = 0; i < n; i++) cin >> a[i]; P p[200005]; for(int i = 0; i < n; i++) p[i] = P(a[i], i); sort(p, p + n); int q[200005]; for(int i = 0; i < n; i++) q[p[i].second] = i; SegmentTree<int> sml(n, [](int a, int b){return min(a, b);}, INF); SegmentTree<int> lar(n, [](int a, int b){return max(a, b);}, 0); for(int i = 0; i < n; i++) sml.set(i, q[i]); for(int i = 0; i < n; i++) lar.set(i, a[i]); sml.build(); lar.build(); ll ans = 0; for(int i = 0; i < n - 1; i++){ int left = i + 1, right = n + 1; while(right - left > 1){ int mid = (right + left) / 2; int M = lar.query(i, mid); int id1 = sml.query(i, mid); sml.update(p[id1].second, INF); int id2 = sml.query(i, mid); sml.update(p[id1].second, id1); if(M > p[id1].first + p[id2].first) right = mid; else left = mid; } ans += left - i - 1; } cout << ans << endl; }