이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
const ll inf = 1e18;
const int MXN = 3e5+5;
struct node{
pii l, r; // v, pos
pair<ll,pii> m;
ll a;
}t[MXN<<2], nt[MXN<<2];
int A[MXN], lz[MXN];
node operator+(const node &i, const node &j){
node k;
k.l = max(i.l,{i.a+j.l.first,j.l.second});
k.r = max(j.r,{i.r.first+j.a,i.r.second});
k.m = max(i.m,j.m);
k.m = max(k.m,{i.r.first+j.l.first,{i.r.second,j.l.second}});
k.a = i.a+j.a;
return k;
}
void push(int x,int s,int e){
if(!lz[x]) return;
swap(t[x],nt[x]);
if(s!=e){
lz[x*2] ^= lz[x];
lz[x*2+1] ^= lz[x];
}
lz[x] = 0;
}
void upd(int x,int s,int e,int l,int r){
push(x,s,e);
if(e<l || r<s) return;
if(l<=s && e<=r) {
lz[x]^=1;
return push(x,s,e);
}
int mid = s+e>>1;
upd(x*2,s,mid,l,r), upd(x*2+1,mid+1,e,l,r);
t[x] = t[x*2]+t[x*2+1];
nt[x] = nt[x*2]+nt[x*2+1];
}
void init(int x,int s,int e){
if(s==e) {
ll v = A[s];
t[x] = {{v,s},{v,s},{v,{s,s}},v};
nt[x] = {{-v,s},{-v,s},{-v,{s,s}},-v};
return;
}
int mid = s+e>>1;
init(x*2,s,mid), init(x*2+1,mid+1,e);
t[x] = t[x*2]+t[x*2+1];
nt[x] = nt[x*2]+nt[x*2+1];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k; cin >> n >> k;
for(int i = 1;i<=n;i++) cin >> A[i];
init(1,1,n);
ll ans = 0;
for(int i = 0;i<k;i++){
auto [l,r] = t[1].m.second;
ll v = t[1].m.first;
if(v<=0) break;
ans += v;
upd(1,1,n,l,r);
}
cout << ans;
}
컴파일 시 표준 에러 (stderr) 메시지
feast.cpp: In function 'void upd(int, int, int, int, int)':
feast.cpp:38:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = s+e>>1;
| ~^~
feast.cpp: In function 'void init(int, int, int)':
feast.cpp:50:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid = s+e>>1;
| ~^~
feast.cpp: In function 'int main()':
feast.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | auto [l,r] = t[1].m.second;
| ^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |