이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = (int)5e5 + 10;
const int mod = (int)1e9 + 7;
const ll inf = (ll) 2e16;
int n;
int a[N];
ll pref[N];
ll get_sum(int l, int r) {
return pref[r] - pref[l - 1];
}
pair<int, int> dp[N];
struct node {
int l, r;
pair<int, int> val;
ll x;
node() {
l = r = -1;
val = make_pair(0, 0);
}
};
node t[N * 20];
int sz = 1;
void upd(ll x, pair<int, int> val, int v = 1, ll l = 0, ll r = inf) {
if (l == r) {
t[v].val = max(t[v].val, val);
return;
}
ll m = (l + r) / 2;
if (l <= x && x <= m) {
if (t[v].l == -1) t[v].l = ++sz;
assert(sz < 20 * N);
upd(x, val, t[v].l, l, m);
} else if (m + 1 <= x && x <= r) {
if (t[v].r == -1) t[v].r = ++sz;
assert(sz < 20 * N);
upd(x, val, t[v].r, m + 1, r);
}
t[v].val = make_pair(0, 0);
if (t[v].l != -1) t[v].val = max(t[v].val, t[t[v].l].val);
if (t[v].r != -1) t[v].val = max(t[v].val, t[t[v].r].val);
}
pair<int, int> get(ll x, int v = 1, ll l = 0, ll r = inf) {
if (l > x)
return make_pair(0, 0);
// cout << v << ' ' << l << ' ' << r << ' ' << x << endl;
if (r <= x)
return t[v].val;
pair<int, int> ans = make_pair(0, 0);
ll m = (l + r) / 2;
if (t[v].l != -1) ans = max(ans, get(x, t[v].l, l, m));
if (t[v].r != -1) ans = max(ans, get(x, t[v].r, m + 1, r));
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pref[i] = pref[i - 1] + a[i];
}
for (int i = 1; i <= n; i++) {
dp[i] = make_pair(1, 1);
dp[i] = max(dp[i], get(pref[i]));
upd(2 * pref[i] - pref[dp[i].second - 1], make_pair(dp[i].first + 1, i + 1));
}
cout << dp[n].first;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
segments.cpp: In function 'int main()':
segments.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
74 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
segments.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
76 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# | 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... |