이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define N 100005
#define wr cout << "Continue debugging\n";
#define all(x) (x).begin(), (x).end()
#define ll long long int
#define pii pair <int, int>
#define pb push_back
#define ff first
#define ss second
using namespace std;
int a[N], mx, n, c[N], lazy[4*N], t[4*N];
ll pref[N];
ll dp[N][5];
int binary(int i, ll x){
int l = i+1, r = n, idx=i;
while(l <= r){
int md = (l+r)/2;
if (pref[md]-pref[i] >= x) r = md - 1;
else{
idx = md;
l = md + 1;
}
}
return idx+1;
}
void upd(int x, int u, int v, int l, int r, int idx){
if (lazy[idx]){
t[idx] = lazy[idx];
if (l != r){
lazy[idx*2] = lazy[idx];
lazy[idx*2+1] = lazy[idx];
}
lazy[idx] = 0;
}
if (l > r or l > v or r < u) return;
if (u <= l and r <= v){
t[idx] = x;
if (l != r){
lazy[idx*2] = x;
lazy[idx*2+1] = x;
}
return;
}
int md = (l+r)/2;
upd(x, u, v, l, md, idx*2);
upd(x, u, v, md+1, r, idx*2+1);
t[idx] = max(t[idx*2], t[idx*2+1]);
}
int range(int pos, int l, int r, int idx){
if (lazy[idx]){
t[idx] = lazy[idx];
if (l != r){
lazy[idx*2] = lazy[idx];
lazy[idx*2+1] = lazy[idx];
}
lazy[idx]=0;
}
int md = (l+r)/2;
if (l == r) return t[idx];
if (md >= pos) range(pos, l, md, idx*2);
else range(pos, md+1, r, idx*2+1);
}
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
pref[i] = pref[i-1]+a[i];
}
dp[1][1] = 1;
dp[1][2] = a[1];
int pos = binary(1, a[1]);
if (pos <= n) upd(1, pos, n, 1, n, 1);
for (int i = 2; i <= n; i++){
int pos = range(i, 1, n, 1);
if (!pos){
dp[i][1] = 1;
dp[i][2] = pref[i];
}else{
dp[i][1] = dp[pos][1]+1;
dp[i][2] = pref[i]-pref[pos];
}
pos = binary(i, dp[i][2]);
if (pos <= n) upd(i, pos, n, 1, n, 1);
}
cout << dp[n][1];
}
컴파일 시 표준 에러 (stderr) 메시지
segments.cpp: In function 'int range(int, int, int, int)':
segments.cpp:64:1: warning: control reaches end of non-void function [-Wreturn-type]
64 | }
| ^
# | 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... |