#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
struct node
{
long long pre_max = 0,pre_min = 0,suf_max = 0,suf_min = 0,best = 0,worst = 0,sum = 0;
int l_best = 0,r_best = 0,l_worst = 0,r_worst = 0,r_pre_max = 0,r_pre_min = 0,l_suf_max = 0,l_suf_min = 0,lazy = 0;
node operator + (const node &o) const
{
node res;
res.sum = sum + o.sum;
res.pre_max = max(pre_max,sum+o.pre_max);
if(res.pre_max == pre_max) res.r_pre_max = r_pre_max;
else res.r_pre_max = o.r_pre_max;
res.pre_min = min(pre_min,sum+o.pre_min);
if(res.pre_min == pre_min) res.r_pre_min = r_pre_min;
else res.r_pre_min = o.r_pre_min;
res.suf_max = max(o.sum+suf_max,o.suf_max);
if(res.suf_max == o.sum+suf_max) res.l_suf_max = l_suf_max;
else res.l_suf_max = o.l_suf_max;
res.suf_min = min(o.sum+suf_min,o.suf_min);
if(res.suf_min == o.sum+suf_min) res.l_suf_min = l_suf_min;
else res.l_suf_min = o.l_suf_min;
res.best = max({best,o.best,suf_max+o.pre_max});
if(res.best == best){
res.l_best = l_best;
res.r_best = r_best;
}
else if(res.best == o.best){
res.l_best = o.l_best;
res.r_best = o.r_best;
}
else{
res.l_best = l_suf_max;
res.r_best = o.r_pre_max;
}
res.worst = min({worst,o.worst,suf_min+o.pre_min});
if(res.worst == worst){
res.l_worst = l_worst;
res.r_worst = r_worst;
}
else if(res.worst == o.worst){
res.l_worst = o.l_worst;
res.r_worst = o.r_worst;
}
else{
res.l_worst = l_suf_min;
res.r_worst = o.r_pre_min;
}
return res;
}
};
node rev(node x)
{
x.pre_max = -x.pre_max;
x.pre_min = -x.pre_min;
x.suf_max = -x.suf_max;
x.suf_min = -x.suf_min;
x.best = -x.best;
x.worst = -x.worst;
x.sum = -x.sum;
swap(x.pre_max,x.pre_min);
swap(x.suf_max,x.suf_min);
swap(x.best,x.worst);
swap(x.l_best,x.l_worst);
swap(x.r_best,x.r_worst);
swap(x.r_pre_max,x.r_pre_min);
swap(x.l_suf_max,x.l_suf_min);
return x;
}
const int MN = 3e5+5;
const long long inf = 1e18;
int n,k,a[MN];
long long ans;
node st[MN*4];
void fix(int v,int l,int r)
{
if(st[v].lazy%2==0) return;
st[v] = rev(st[v]);
st[v].lazy = 0;
if(l==r) return;
st[v*2].lazy++;
st[v*2+1].lazy++;
}
void build(int v,int l,int r)
{
if(l==r){
st[v].pre_max = st[v].pre_min = st[v].suf_max = st[v].suf_min = st[v].best = st[v].worst = st[v].sum = a[l];
st[v].l_best = st[v].r_best = st[v].l_worst = st[v].r_worst = st[v].r_pre_max = st[v].r_pre_min = st[v].l_suf_max = st[v].l_suf_min = l;
return;
}
int mid = (l+r) >> 1;
build(v*2,l,mid);
build(v*2+1,mid+1,r);
st[v] = st[v*2] + st[v*2+1];
}
void upd(int v,int l,int r,int x,int y)
{
fix(v,l,r);
if(x>r or y<l) return;
if(x<=l and r<=y){
ans += st[v].sum;
st[v].lazy++;
fix(v,l,r);
return;
}
int mid = (l+r) >> 1;
upd(v*2,l,mid,x,y);
upd(v*2+1,mid+1,r,x,y);
st[v] = st[v*2] + st[v*2+1];
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#define task "NOI19_feastt"
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k;
for(int i=1; i<=n; i++) cin >> a[i];
build(1,1,n);
while(k--){
fix(1,1,n);
int l = st[1].l_best;
int r = st[1].r_best;
//cout << l << ' ' << r << ' ' << st[1].best << ' ' << st[1].worst << endl;
if(st[1].best<=0) break;
upd(1,1,n,l,r);
}
cout << ans;
cerr << "\nTime: " << clock();
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
feast.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
104404 KB |
Output is correct |
2 |
Correct |
63 ms |
102736 KB |
Output is correct |
3 |
Correct |
67 ms |
102772 KB |
Output is correct |
4 |
Correct |
73 ms |
102872 KB |
Output is correct |
5 |
Correct |
61 ms |
102736 KB |
Output is correct |
6 |
Correct |
63 ms |
102740 KB |
Output is correct |
7 |
Correct |
64 ms |
102824 KB |
Output is correct |
8 |
Correct |
63 ms |
102732 KB |
Output is correct |
9 |
Correct |
64 ms |
104528 KB |
Output is correct |
10 |
Correct |
92 ms |
104464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
101204 KB |
Output is correct |
2 |
Correct |
59 ms |
102792 KB |
Output is correct |
3 |
Correct |
61 ms |
102740 KB |
Output is correct |
4 |
Correct |
52 ms |
101204 KB |
Output is correct |
5 |
Correct |
67 ms |
102740 KB |
Output is correct |
6 |
Correct |
51 ms |
102736 KB |
Output is correct |
7 |
Correct |
92 ms |
101040 KB |
Output is correct |
8 |
Correct |
68 ms |
102808 KB |
Output is correct |
9 |
Correct |
66 ms |
102696 KB |
Output is correct |
10 |
Correct |
78 ms |
102844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
103068 KB |
Output is correct |
2 |
Correct |
70 ms |
104788 KB |
Output is correct |
3 |
Correct |
104 ms |
104792 KB |
Output is correct |
4 |
Correct |
71 ms |
104656 KB |
Output is correct |
5 |
Correct |
71 ms |
104660 KB |
Output is correct |
6 |
Correct |
75 ms |
104788 KB |
Output is correct |
7 |
Correct |
69 ms |
102944 KB |
Output is correct |
8 |
Correct |
64 ms |
103248 KB |
Output is correct |
9 |
Correct |
69 ms |
104804 KB |
Output is correct |
10 |
Correct |
70 ms |
102996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2516 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2476 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2516 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2476 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2524 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
2516 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2516 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2476 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2524 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
2516 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2856 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
104404 KB |
Output is correct |
2 |
Correct |
63 ms |
102736 KB |
Output is correct |
3 |
Correct |
67 ms |
102772 KB |
Output is correct |
4 |
Correct |
73 ms |
102872 KB |
Output is correct |
5 |
Correct |
61 ms |
102736 KB |
Output is correct |
6 |
Correct |
63 ms |
102740 KB |
Output is correct |
7 |
Correct |
64 ms |
102824 KB |
Output is correct |
8 |
Correct |
63 ms |
102732 KB |
Output is correct |
9 |
Correct |
64 ms |
104528 KB |
Output is correct |
10 |
Correct |
92 ms |
104464 KB |
Output is correct |
11 |
Correct |
58 ms |
101204 KB |
Output is correct |
12 |
Correct |
59 ms |
102792 KB |
Output is correct |
13 |
Correct |
61 ms |
102740 KB |
Output is correct |
14 |
Correct |
52 ms |
101204 KB |
Output is correct |
15 |
Correct |
67 ms |
102740 KB |
Output is correct |
16 |
Correct |
51 ms |
102736 KB |
Output is correct |
17 |
Correct |
92 ms |
101040 KB |
Output is correct |
18 |
Correct |
68 ms |
102808 KB |
Output is correct |
19 |
Correct |
66 ms |
102696 KB |
Output is correct |
20 |
Correct |
78 ms |
102844 KB |
Output is correct |
21 |
Correct |
73 ms |
103068 KB |
Output is correct |
22 |
Correct |
70 ms |
104788 KB |
Output is correct |
23 |
Correct |
104 ms |
104792 KB |
Output is correct |
24 |
Correct |
71 ms |
104656 KB |
Output is correct |
25 |
Correct |
71 ms |
104660 KB |
Output is correct |
26 |
Correct |
75 ms |
104788 KB |
Output is correct |
27 |
Correct |
69 ms |
102944 KB |
Output is correct |
28 |
Correct |
64 ms |
103248 KB |
Output is correct |
29 |
Correct |
69 ms |
104804 KB |
Output is correct |
30 |
Correct |
70 ms |
102996 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2392 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2516 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2476 KB |
Output is correct |
40 |
Correct |
1 ms |
472 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
0 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2524 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
2516 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
860 KB |
Output is correct |
52 |
Correct |
1 ms |
4700 KB |
Output is correct |
53 |
Correct |
2 ms |
4700 KB |
Output is correct |
54 |
Correct |
1 ms |
2908 KB |
Output is correct |
55 |
Correct |
1 ms |
2652 KB |
Output is correct |
56 |
Correct |
1 ms |
2908 KB |
Output is correct |
57 |
Correct |
1 ms |
2856 KB |
Output is correct |
58 |
Correct |
1 ms |
4700 KB |
Output is correct |
59 |
Correct |
1 ms |
4700 KB |
Output is correct |
60 |
Correct |
1 ms |
4700 KB |
Output is correct |
61 |
Correct |
147 ms |
104796 KB |
Output is correct |
62 |
Correct |
160 ms |
104756 KB |
Output is correct |
63 |
Correct |
76 ms |
104788 KB |
Output is correct |
64 |
Correct |
148 ms |
104752 KB |
Output is correct |
65 |
Correct |
115 ms |
104724 KB |
Output is correct |
66 |
Correct |
106 ms |
104796 KB |
Output is correct |
67 |
Correct |
104 ms |
103004 KB |
Output is correct |
68 |
Correct |
68 ms |
102996 KB |
Output is correct |
69 |
Correct |
69 ms |
102820 KB |
Output is correct |
70 |
Correct |
67 ms |
104536 KB |
Output is correct |