This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int O = 5e3 + 5;
const int N = 5e5 + 5;
int n, a[N], sum[N], lim[O], Max[O], f[O][O], tree[O][O * 4];
vector <int> val[O];
void Update(int id, int l, int r, int u, int x, int t){
if (l > u || r < u) return ;
if (l == r){
tree[t][id] = x;
return;
}
int mid = (l + r) / 2;
Update(id << 1, l, mid, u, x, t);
Update(id << 1 | 1, mid + 1, r, u, x, t);
tree[t][id] = max(tree[t][id << 1], tree[t][id << 1 | 1]);
}
int Get(int id, int l, int r, int u, int v, int t){
if (l > v || r < u) return 0;
if (u <= l && r <= v) return tree[t][id];
int mid = (l + r) / 2;
return max(Get(id << 1, l, mid, u, v, t), Get(id << 1 | 1, mid + 1, r, u, v, t));
}
void sub2(){
for (int i = 1; i <= n; ++ i){
int x = 0;
for (int j = i; j >= 1; -- j){
x += a[j];
val[i].push_back(x);
}
sort(val[i].begin(), val[i].end());
lim[i] = val[i].size();
}
lim[0] = 1;
val[0].push_back(0);
for (int i = 1; i <= n; ++ i){
int x = 0;
for (int j = i; j >= 1; -- j){
x += a[j];
int id = lower_bound(val[j - 1].begin(), val[j - 1].end(), x) - val[j - 1].begin();
f[i][j] = max(f[i][j], Get(1, 0, lim[j - 1], id, lim[j - 1],j - 1) + 1);
id = lower_bound(val[i].begin(), val[i].end(), x) - val[i].begin();
Update(1, 0, lim[i], id, f[i][j], i);
}
}
cout << Get(1, 0, lim[n], 0, lim[n], n);
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); srand(time(NULL));
cin >> n;
for (int i = 1; i <= n; ++ i) cin >> a[i], sum[i] = sum[i - 1] + a[i];
reverse(a + 1, a + n + 1);
if (n <= 5e3 + 5){
sub2();
return 0;
}
cout << rand() % n + 1;
}
Compilation message (stderr)
segments.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |