Submission #166807

#TimeUsernameProblemLanguageResultExecution timeMemory
166807YaroslaffBigger segments (IZhO19_segments)C++14
100 / 100
675 ms60020 KiB
#include <iostream> #include <string> #include <unordered_map> #include <cstring> #include <chrono> #include <vector> #include <map> #include <random> #include <set> #include <algorithm> #include <math.h> #include <cstdio> #include <stdio.h> #include <queue> #include <bitset> #include <cstdlib> #include <deque> #include <cassert> #include <stack> using namespace std; #define pii pair<long long, long long> #define mp make_pair #define f first #define se second #define max3(a, b, c) max(a, max(b, c)) #define int long long const int inf = 2e9; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); struct node { node *l, *r; int key, pr; pii val, mx; node(int _key, pii _val) { pr = uniform_int_distribution<>(0, inf)(rng); l = r = NULL; key = _key, mx = val = _val; } }; typedef node* pnode; inline pii val(pnode t) { return t ? t->mx : mp(0LL, 0LL); } inline void upd(pnode &t) { if(t) t->mx = max3(t->val, val(t->l), val(t->r)); } void split(pnode t, pnode &l, pnode &r, int key) { if(!t) return void(l = r = t); if(t->key <= key) split(t->r, t->r, r, key), l = t; else split(t->l, l, t->l, key), r = t; upd(l); upd(r); } void merge(pnode &t, pnode l, pnode r) { if(!l || !r) t = l ? l : r; else if(l->pr > r->pr) merge(l->r, l->r, r), t = l; else merge(r->l, l, r->l), t = r; upd(t); } pnode root = NULL; const int mod = 1e9 + 7; const int N = 5e5 + 15; int n, a[N], p[N]; pii dp[N]; inline void insert(pnode &t, pnode nw) { pnode t1, t2, t3, t4; split(root, t1, t2, nw->key - 1); split(t2, t3, t4, nw->key); if(t3) t3->val = max(t3->val, nw->val); else t3 = nw; merge(t2, t3, t4); merge(root, t1, t2); } main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; ++i) { cin >> a[i]; p[i] = p[i-1] + a[i]; } root = new node(0, mp(1, 0)); for(int i = 1; i <= n; ++i) { pnode t1, t2; split(root, t1, t2, p[i]); dp[i] = val(t1); merge(root, t1, t2); insert(root, new node(2 * p[i] - p[dp[i].se], mp(dp[i].f + 1, i))); } cout << dp[n].f << endl; return 0; }

Compilation message (stderr)

segments.cpp:95:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...