#include <bits/stdc++.h>
#define file ""
#define all(x) x.begin(), x.end()
#define sc second
#define fr first
#define pb push_back
#define mp make_pair
#define pss pair < line * , line * >
#define pptreap pair < treap * , treap * >
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const ll inf = 1e16;
const int MOD = 1e9 + 7;
int const N = 7e6;
struct treap {
ll x, y;
pii mx, val;
treap *l, *r;
treap(ll new_x, ll new_mx, ll pos) {
x = new_x;
y = (rand() << 15) | rand();
mx = {new_mx, pos};
val = {new_mx, pos};
l = r = nullptr;
}
};
pii getmx(treap *a) {
if (a == nullptr)
return {0, 0};
return a->mx;
}
void update(treap *&a) {
a->mx = max(a->val, max(getmx(a->l), getmx(a->r)));
}
treap * merge(treap *t1, treap *t2) {
if (t1 == nullptr)
return t2;
if (t2 == nullptr)
return t1;
if (t1->y > t2->y) {
t1->r = merge(t1->r, t2);
update(t1);
return t1;
} else {
t2->l = merge(t1, t2->l);
update(t2);
return t2;
}
}
pptreap split(treap *t, ll k) {
if (t == nullptr)
return mp(nullptr, nullptr);
if (t->x <= k) {
pptreap cnt = split(t->r, k);
t->r = cnt.fr;
update(t);
return mp(t, cnt.sc);
} else {
pptreap cnt = split(t->l, k);
t->l = cnt.sc;
update(t);
return mp(cnt.fr, t);
}
}
bool exist(treap *t, ll pos, pii new_val) {
if (t == nullptr)
return 0;
if (pos < t->x)
return exist(t->l, pos, new_val);
if (pos > t->x)
return exist(t->r, pos, new_val);
if (pos == t->x) {
t->val = max(t->val, new_val);
t->mx = max(t->mx, t->val);
return 1;
}
update(t);
}
void data() {
#ifdef PC
freopen("rofl.in", "r", stdin);
freopen("rofl.out", "w", stdout);
#endif
}
int main() {
data();
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
srand(time(nullptr));
int n;
cin >> n;
vector < ll > a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
vector < ll > dp(n + 1), sum(n + 1), pref(n + 1);
for (int i = 1; i <= n; i++)
pref[i] = pref[i - 1] + a[i];
treap * t = new treap(0, 0, 0);
for (int i = 1; i <= n; i++) {
pptreap k = split(t, pref[i]);
pii pos = getmx(k.fr);
t = merge(k.fr, k.sc);
dp[i] = pos.fr + 1;
sum[i] = pref[i] - pref[pos.sc];
treap * nw = new treap(sum[i] + pref[i], dp[i], i);
if (!exist(t, sum[i] + pref[i], mp(dp[i], i))) {
k = split(t, sum[i] + pref[i]);
t = merge(merge(k.fr, nw), k.sc);
}
}
cout << dp[n];
return 0;
}
Compilation message
segments.cpp: In function 'bool exist(treap*, ll, pii)':
segments.cpp:96:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
6 ms |
640 KB |
Output is correct |
32 |
Correct |
7 ms |
640 KB |
Output is correct |
33 |
Correct |
7 ms |
640 KB |
Output is correct |
34 |
Correct |
7 ms |
640 KB |
Output is correct |
35 |
Correct |
6 ms |
640 KB |
Output is correct |
36 |
Correct |
7 ms |
640 KB |
Output is correct |
37 |
Correct |
6 ms |
640 KB |
Output is correct |
38 |
Correct |
6 ms |
512 KB |
Output is correct |
39 |
Correct |
6 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
640 KB |
Output is correct |
43 |
Correct |
6 ms |
640 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
6 ms |
640 KB |
Output is correct |
32 |
Correct |
7 ms |
640 KB |
Output is correct |
33 |
Correct |
7 ms |
640 KB |
Output is correct |
34 |
Correct |
7 ms |
640 KB |
Output is correct |
35 |
Correct |
6 ms |
640 KB |
Output is correct |
36 |
Correct |
7 ms |
640 KB |
Output is correct |
37 |
Correct |
6 ms |
640 KB |
Output is correct |
38 |
Correct |
6 ms |
512 KB |
Output is correct |
39 |
Correct |
6 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
640 KB |
Output is correct |
43 |
Correct |
6 ms |
640 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
6 ms |
640 KB |
Output is correct |
46 |
Correct |
76 ms |
9796 KB |
Output is correct |
47 |
Correct |
116 ms |
9720 KB |
Output is correct |
48 |
Correct |
98 ms |
9828 KB |
Output is correct |
49 |
Correct |
89 ms |
9720 KB |
Output is correct |
50 |
Correct |
65 ms |
9720 KB |
Output is correct |
51 |
Correct |
107 ms |
9720 KB |
Output is correct |
52 |
Correct |
53 ms |
5112 KB |
Output is correct |
53 |
Correct |
31 ms |
3192 KB |
Output is correct |
54 |
Correct |
99 ms |
9720 KB |
Output is correct |
55 |
Correct |
102 ms |
9720 KB |
Output is correct |
56 |
Correct |
62 ms |
9720 KB |
Output is correct |
57 |
Correct |
68 ms |
9720 KB |
Output is correct |
58 |
Correct |
77 ms |
9720 KB |
Output is correct |
59 |
Correct |
60 ms |
9724 KB |
Output is correct |
60 |
Correct |
79 ms |
9720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
6 ms |
640 KB |
Output is correct |
32 |
Correct |
7 ms |
640 KB |
Output is correct |
33 |
Correct |
7 ms |
640 KB |
Output is correct |
34 |
Correct |
7 ms |
640 KB |
Output is correct |
35 |
Correct |
6 ms |
640 KB |
Output is correct |
36 |
Correct |
7 ms |
640 KB |
Output is correct |
37 |
Correct |
6 ms |
640 KB |
Output is correct |
38 |
Correct |
6 ms |
512 KB |
Output is correct |
39 |
Correct |
6 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
640 KB |
Output is correct |
43 |
Correct |
6 ms |
640 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
6 ms |
640 KB |
Output is correct |
46 |
Correct |
76 ms |
9796 KB |
Output is correct |
47 |
Correct |
116 ms |
9720 KB |
Output is correct |
48 |
Correct |
98 ms |
9828 KB |
Output is correct |
49 |
Correct |
89 ms |
9720 KB |
Output is correct |
50 |
Correct |
65 ms |
9720 KB |
Output is correct |
51 |
Correct |
107 ms |
9720 KB |
Output is correct |
52 |
Correct |
53 ms |
5112 KB |
Output is correct |
53 |
Correct |
31 ms |
3192 KB |
Output is correct |
54 |
Correct |
99 ms |
9720 KB |
Output is correct |
55 |
Correct |
102 ms |
9720 KB |
Output is correct |
56 |
Correct |
62 ms |
9720 KB |
Output is correct |
57 |
Correct |
68 ms |
9720 KB |
Output is correct |
58 |
Correct |
77 ms |
9720 KB |
Output is correct |
59 |
Correct |
60 ms |
9724 KB |
Output is correct |
60 |
Correct |
79 ms |
9720 KB |
Output is correct |
61 |
Correct |
399 ms |
47352 KB |
Output is correct |
62 |
Correct |
466 ms |
47352 KB |
Output is correct |
63 |
Correct |
588 ms |
47448 KB |
Output is correct |
64 |
Correct |
485 ms |
47352 KB |
Output is correct |
65 |
Correct |
340 ms |
47356 KB |
Output is correct |
66 |
Correct |
544 ms |
47356 KB |
Output is correct |
67 |
Correct |
405 ms |
37880 KB |
Output is correct |
68 |
Correct |
217 ms |
19200 KB |
Output is correct |
69 |
Correct |
495 ms |
47352 KB |
Output is correct |
70 |
Correct |
525 ms |
47480 KB |
Output is correct |
71 |
Correct |
308 ms |
47480 KB |
Output is correct |
72 |
Correct |
389 ms |
47608 KB |
Output is correct |
73 |
Correct |
375 ms |
47480 KB |
Output is correct |
74 |
Correct |
318 ms |
47488 KB |
Output is correct |
75 |
Correct |
351 ms |
47480 KB |
Output is correct |