# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1087097 |
2024-09-12T08:08:19 Z |
ezluci |
Feast (NOI19_feast) |
C++17 |
|
430 ms |
38596 KB |
#ifdef EZ
#include "./ez/ez.h"
#else
#include <bits/stdc++.h>
#endif
#define mp make_pair
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
void EZinit() {
#ifndef EZ
cin.tie(0)->sync_with_stdio(0);
#else
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
}
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
/*
vom afla raspunsul secvential: j=1,2,..k secvente. pt j=1 folosim cea mai mare subsecventa initial.
pt a trece de la j-1 la j, ori folosim o noua subsecventa, ori
spargem o subsecventa deja folosita (scoatem sub-subsecventa de suma MINIMA).
*/
void get(int &x)
{
static char c;
static bool neg;
while (!isdigit(c = getchar_unlocked()) && c != '-');
if (c == '-') neg = 1, x = 0;
else neg = 0, x = c-'0';
while (isdigit(c = getchar_unlocked())) x = x*10 + c-'0';
if (neg) x *= -1;
}
int n, k;
int v[300001];
ll sp[300001];
#define sum(x,y) (sp[min(n,y)]-sp[min(n,x)-1])
struct alexaint {
int x, y, xp, xs;
};
const int ainth = 19;
const int aintlim = 1<<ainth;
alexaint aintmi[2*aintlim], aintma[2*aintlim];
bool minmx; alexaint* aint;
inline alexaint combine(alexaint &A, alexaint &B, int st, int dr)
{
alexaint Q; ll a, b, c;
a = sum(st, A.xp); b = sum(st, B.xp);
Q.xp = (((minmx) ? (a<=b) : (a>=b)) ? A.xp : B.xp);
a = sum(B.xs, dr); b = sum(A.xs, dr);
Q.xs = (((minmx) ? (a<=b) : (a>=b)) ? B.xs : A.xs);
a = sum(A.x, A.y); b = sum(B.x, B.y); c = sum(A.xs, B.xp);
if (minmx)
{
if (a <= min(b,c)) Q.x = A.x, Q.y = A.y;
else if (b <= min(a,c)) Q.x = B.x, Q.y = B.y;
else Q.x = A.xs, Q.y = B.xp;
}
else
{
if (a >= max(b,c)) Q.x = A.x, Q.y = A.y;
else if (b >= max(a,c)) Q.x = B.x, Q.y = B.y;
else Q.x = A.xs, Q.y = B.xp;
}
return Q;
}
void build(int nod = 1, int st = 1, int dr = aintlim)
{
if (st == dr) return (void) (aintma[nod] = aintmi[nod] = {st, st, st, st});
int mj = st+dr >> 1;
build(nod*2, st, mj);
build(nod*2+1, mj+1, dr);
minmx = false;
aintma[nod] = combine(aintma[nod*2], aintma[nod*2+1], st, dr);
minmx = true;
aintmi[nod] = combine(aintmi[nod*2], aintmi[nod*2+1], st, dr);
}
alexaint que(int qst, int qdr)
{
//cerr<<qst << ' ' << qdr << '\n';
int QST = qst, QDR = qdr;
if (qst == qdr) return {qst, qst, qst, qst};
alexaint ansst = {qst, qst, qst, qst};
alexaint ansdr = {qdr, qdr, qdr, qdr};
qst += aintlim-1;
qdr += aintlim-1;
qst++; qdr--;
while (qst <= qdr)
{
//cerr << qst << ' ' << qdr << '\n';
if (qst%2 == 1)
ansst = combine(ansst, aint[qst], QST, (qst+1-(1<<__lg(qst)))*(1<<ainth-__lg(qst))),
qst++;
if (qdr%2 == 0)
ansdr = combine(aint[qdr], ansdr, 1 + (qdr-(1<<__lg(qdr)))*(1<<ainth-__lg(qdr)), QDR),
qdr--;
qst >>= 1; qdr >>= 1;
}
//cerr<<'a';
return combine(ansst, ansdr, QST, QDR);
}
struct alexmax {
int fi, se;
bool operator<(const alexmax &B) const
{
minmx = false; aint = aintma;
auto Xq = que(fi, se);
int X1 = Xq.x, X2 = Xq.y;
auto Yq = que(B.fi, B.se);
int Y1 = Yq.x, Y2 = Yq.y;
return sum(X1,X2) < sum(Y1,Y2);
}
};
struct alexmin {
int fi, se;
bool operator<(const alexmin &B) const
{
minmx = true; aint = aintmi;
auto Xq = que(fi, se);
int X1 = Xq.x, X2 = Xq.y;
auto Yq = que(B.fi, B.se);
int Y1 = Yq.x, Y2 = Yq.y;
return sum(X1,X2) > sum(Y1,Y2);
}
};
int main()
{
EZinit();
get(n); get(k);
for (int i = 1; i <= n; ++i) get(v[i]), sp[i] = sp[i-1] + v[i];
build();
aint = aintma; minmx = false;
//cerr<<aint[524290].x<<' ' << aint[524290].y << '\n';
auto q = que(1,3);
//cerr<<q.x<<' ' << q.y << '\n';
ll ans = 0;
priority_queue<alexmin> ivuse;
priority_queue<alexmax> ivno;
ivno.push({1, n});
for (int j = 1; j <= k; ++j)
{
// ce putem scoate
ll Ss = LLONG_MAX; int xs, ys, Xs, Ys;
if (!ivuse.empty())
{
xs = ivuse.top().fi, ys = ivuse.top().se;
minmx = true; aint = aintmi;
auto q = que(xs, ys);
Xs = q.x, Ys = q.y;
Ss = sp[Ys]-sp[Xs-1];
if (-Ss <= 0) ivuse.pop();
}
// ce putem pune
ll Sp = LLONG_MIN; int xp, yp, Xp, Yp;
if (!ivno.empty())
{
xp = ivno.top().fi, yp = ivno.top().se;
minmx = false; aint = aintma;
auto q = que(xp, yp);
Xp = q.x, Yp = q.y;
Sp = sp[Yp]-sp[Xp-1];
if (Sp <= 0) ivno.pop();
}
if (Ss != LLONG_MAX && -Ss >= 1 && (-Ss > Sp || Sp == LLONG_MIN))
{ // scoatem
ivuse.pop();
if (xs != Xs) ivuse.push({xs,Xs-1});
if (ys != Ys) ivuse.push({Ys+1, ys});
ivno.push({Xs,Ys});
ans += -Ss;
}
if (Sp != LLONG_MIN && Sp >= 1 && (Sp >= -Ss || Ss == LLONG_MAX))
{ // punem
ivno.pop();
if (xp != Xp) ivno.push({xp,Xp-1});
if (yp != Yp) ivno.push({Yp+1, yp});
ivuse.push({Xp,Yp});
ans += Sp;
}
}
cout << ans << '\n';
}
Compilation message
feast.cpp: In function 'void build(int, int, int)':
feast.cpp:85:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int mj = st+dr >> 1;
| ~~^~~
feast.cpp: In function 'alexaint que(int, int)':
feast.cpp:108:81: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
108 | ansst = combine(ansst, aint[qst], QST, (qst+1-(1<<__lg(qst)))*(1<<ainth-__lg(qst))),
| ~~~~~^~~~~~~~~~
feast.cpp:111:78: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
111 | ansdr = combine(aint[qdr], ansdr, 1 + (qdr-(1<<__lg(qdr)))*(1<<ainth-__lg(qdr)), QDR),
| ~~~~~^~~~~~~~~~
feast.cpp: In function 'int main()':
feast.cpp:156:9: warning: variable 'q' set but not used [-Wunused-but-set-variable]
156 | auto q = que(1,3);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
36432 KB |
Output is correct |
2 |
Correct |
33 ms |
36692 KB |
Output is correct |
3 |
Correct |
34 ms |
36696 KB |
Output is correct |
4 |
Correct |
33 ms |
36700 KB |
Output is correct |
5 |
Correct |
33 ms |
36692 KB |
Output is correct |
6 |
Correct |
34 ms |
36532 KB |
Output is correct |
7 |
Correct |
41 ms |
36688 KB |
Output is correct |
8 |
Correct |
33 ms |
36696 KB |
Output is correct |
9 |
Correct |
38 ms |
36500 KB |
Output is correct |
10 |
Correct |
40 ms |
36696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
36700 KB |
Output is correct |
2 |
Correct |
35 ms |
36692 KB |
Output is correct |
3 |
Correct |
30 ms |
36692 KB |
Output is correct |
4 |
Correct |
30 ms |
36696 KB |
Output is correct |
5 |
Correct |
35 ms |
36436 KB |
Output is correct |
6 |
Correct |
30 ms |
36688 KB |
Output is correct |
7 |
Correct |
38 ms |
36740 KB |
Output is correct |
8 |
Correct |
39 ms |
36688 KB |
Output is correct |
9 |
Correct |
35 ms |
36436 KB |
Output is correct |
10 |
Correct |
29 ms |
36620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
36688 KB |
Output is correct |
2 |
Correct |
37 ms |
36692 KB |
Output is correct |
3 |
Correct |
44 ms |
36636 KB |
Output is correct |
4 |
Correct |
41 ms |
36564 KB |
Output is correct |
5 |
Correct |
37 ms |
36692 KB |
Output is correct |
6 |
Correct |
36 ms |
36700 KB |
Output is correct |
7 |
Correct |
35 ms |
36644 KB |
Output is correct |
8 |
Correct |
39 ms |
36688 KB |
Output is correct |
9 |
Correct |
39 ms |
36692 KB |
Output is correct |
10 |
Correct |
40 ms |
36624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
33068 KB |
Output is correct |
2 |
Correct |
22 ms |
33268 KB |
Output is correct |
3 |
Correct |
22 ms |
33140 KB |
Output is correct |
4 |
Correct |
22 ms |
33116 KB |
Output is correct |
5 |
Correct |
26 ms |
33116 KB |
Output is correct |
6 |
Correct |
23 ms |
33104 KB |
Output is correct |
7 |
Correct |
23 ms |
33276 KB |
Output is correct |
8 |
Correct |
24 ms |
33116 KB |
Output is correct |
9 |
Correct |
22 ms |
33112 KB |
Output is correct |
10 |
Correct |
30 ms |
33100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
33068 KB |
Output is correct |
2 |
Correct |
22 ms |
33268 KB |
Output is correct |
3 |
Correct |
22 ms |
33140 KB |
Output is correct |
4 |
Correct |
22 ms |
33116 KB |
Output is correct |
5 |
Correct |
26 ms |
33116 KB |
Output is correct |
6 |
Correct |
23 ms |
33104 KB |
Output is correct |
7 |
Correct |
23 ms |
33276 KB |
Output is correct |
8 |
Correct |
24 ms |
33116 KB |
Output is correct |
9 |
Correct |
22 ms |
33112 KB |
Output is correct |
10 |
Correct |
30 ms |
33100 KB |
Output is correct |
11 |
Correct |
22 ms |
33112 KB |
Output is correct |
12 |
Correct |
22 ms |
33116 KB |
Output is correct |
13 |
Correct |
22 ms |
33088 KB |
Output is correct |
14 |
Correct |
22 ms |
33168 KB |
Output is correct |
15 |
Correct |
22 ms |
33116 KB |
Output is correct |
16 |
Correct |
22 ms |
33116 KB |
Output is correct |
17 |
Correct |
27 ms |
33060 KB |
Output is correct |
18 |
Correct |
24 ms |
33208 KB |
Output is correct |
19 |
Correct |
23 ms |
33104 KB |
Output is correct |
20 |
Correct |
23 ms |
33116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
33068 KB |
Output is correct |
2 |
Correct |
22 ms |
33268 KB |
Output is correct |
3 |
Correct |
22 ms |
33140 KB |
Output is correct |
4 |
Correct |
22 ms |
33116 KB |
Output is correct |
5 |
Correct |
26 ms |
33116 KB |
Output is correct |
6 |
Correct |
23 ms |
33104 KB |
Output is correct |
7 |
Correct |
23 ms |
33276 KB |
Output is correct |
8 |
Correct |
24 ms |
33116 KB |
Output is correct |
9 |
Correct |
22 ms |
33112 KB |
Output is correct |
10 |
Correct |
30 ms |
33100 KB |
Output is correct |
11 |
Correct |
22 ms |
33112 KB |
Output is correct |
12 |
Correct |
22 ms |
33116 KB |
Output is correct |
13 |
Correct |
22 ms |
33088 KB |
Output is correct |
14 |
Correct |
22 ms |
33168 KB |
Output is correct |
15 |
Correct |
22 ms |
33116 KB |
Output is correct |
16 |
Correct |
22 ms |
33116 KB |
Output is correct |
17 |
Correct |
27 ms |
33060 KB |
Output is correct |
18 |
Correct |
24 ms |
33208 KB |
Output is correct |
19 |
Correct |
23 ms |
33104 KB |
Output is correct |
20 |
Correct |
23 ms |
33116 KB |
Output is correct |
21 |
Correct |
30 ms |
33116 KB |
Output is correct |
22 |
Correct |
24 ms |
33108 KB |
Output is correct |
23 |
Correct |
24 ms |
33104 KB |
Output is correct |
24 |
Correct |
27 ms |
33116 KB |
Output is correct |
25 |
Correct |
24 ms |
33112 KB |
Output is correct |
26 |
Correct |
26 ms |
33100 KB |
Output is correct |
27 |
Correct |
24 ms |
33112 KB |
Output is correct |
28 |
Correct |
23 ms |
33112 KB |
Output is correct |
29 |
Correct |
29 ms |
33200 KB |
Output is correct |
30 |
Correct |
23 ms |
33116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
36432 KB |
Output is correct |
2 |
Correct |
33 ms |
36692 KB |
Output is correct |
3 |
Correct |
34 ms |
36696 KB |
Output is correct |
4 |
Correct |
33 ms |
36700 KB |
Output is correct |
5 |
Correct |
33 ms |
36692 KB |
Output is correct |
6 |
Correct |
34 ms |
36532 KB |
Output is correct |
7 |
Correct |
41 ms |
36688 KB |
Output is correct |
8 |
Correct |
33 ms |
36696 KB |
Output is correct |
9 |
Correct |
38 ms |
36500 KB |
Output is correct |
10 |
Correct |
40 ms |
36696 KB |
Output is correct |
11 |
Correct |
29 ms |
36700 KB |
Output is correct |
12 |
Correct |
35 ms |
36692 KB |
Output is correct |
13 |
Correct |
30 ms |
36692 KB |
Output is correct |
14 |
Correct |
30 ms |
36696 KB |
Output is correct |
15 |
Correct |
35 ms |
36436 KB |
Output is correct |
16 |
Correct |
30 ms |
36688 KB |
Output is correct |
17 |
Correct |
38 ms |
36740 KB |
Output is correct |
18 |
Correct |
39 ms |
36688 KB |
Output is correct |
19 |
Correct |
35 ms |
36436 KB |
Output is correct |
20 |
Correct |
29 ms |
36620 KB |
Output is correct |
21 |
Correct |
36 ms |
36688 KB |
Output is correct |
22 |
Correct |
37 ms |
36692 KB |
Output is correct |
23 |
Correct |
44 ms |
36636 KB |
Output is correct |
24 |
Correct |
41 ms |
36564 KB |
Output is correct |
25 |
Correct |
37 ms |
36692 KB |
Output is correct |
26 |
Correct |
36 ms |
36700 KB |
Output is correct |
27 |
Correct |
35 ms |
36644 KB |
Output is correct |
28 |
Correct |
39 ms |
36688 KB |
Output is correct |
29 |
Correct |
39 ms |
36692 KB |
Output is correct |
30 |
Correct |
40 ms |
36624 KB |
Output is correct |
31 |
Correct |
22 ms |
33068 KB |
Output is correct |
32 |
Correct |
22 ms |
33268 KB |
Output is correct |
33 |
Correct |
22 ms |
33140 KB |
Output is correct |
34 |
Correct |
22 ms |
33116 KB |
Output is correct |
35 |
Correct |
26 ms |
33116 KB |
Output is correct |
36 |
Correct |
23 ms |
33104 KB |
Output is correct |
37 |
Correct |
23 ms |
33276 KB |
Output is correct |
38 |
Correct |
24 ms |
33116 KB |
Output is correct |
39 |
Correct |
22 ms |
33112 KB |
Output is correct |
40 |
Correct |
30 ms |
33100 KB |
Output is correct |
41 |
Correct |
22 ms |
33112 KB |
Output is correct |
42 |
Correct |
22 ms |
33116 KB |
Output is correct |
43 |
Correct |
22 ms |
33088 KB |
Output is correct |
44 |
Correct |
22 ms |
33168 KB |
Output is correct |
45 |
Correct |
22 ms |
33116 KB |
Output is correct |
46 |
Correct |
22 ms |
33116 KB |
Output is correct |
47 |
Correct |
27 ms |
33060 KB |
Output is correct |
48 |
Correct |
24 ms |
33208 KB |
Output is correct |
49 |
Correct |
23 ms |
33104 KB |
Output is correct |
50 |
Correct |
23 ms |
33116 KB |
Output is correct |
51 |
Correct |
30 ms |
33116 KB |
Output is correct |
52 |
Correct |
24 ms |
33108 KB |
Output is correct |
53 |
Correct |
24 ms |
33104 KB |
Output is correct |
54 |
Correct |
27 ms |
33116 KB |
Output is correct |
55 |
Correct |
24 ms |
33112 KB |
Output is correct |
56 |
Correct |
26 ms |
33100 KB |
Output is correct |
57 |
Correct |
24 ms |
33112 KB |
Output is correct |
58 |
Correct |
23 ms |
33112 KB |
Output is correct |
59 |
Correct |
29 ms |
33200 KB |
Output is correct |
60 |
Correct |
23 ms |
33116 KB |
Output is correct |
61 |
Correct |
254 ms |
37576 KB |
Output is correct |
62 |
Correct |
430 ms |
38596 KB |
Output is correct |
63 |
Correct |
71 ms |
36688 KB |
Output is correct |
64 |
Correct |
251 ms |
37732 KB |
Output is correct |
65 |
Correct |
195 ms |
37452 KB |
Output is correct |
66 |
Correct |
177 ms |
37328 KB |
Output is correct |
67 |
Correct |
179 ms |
37396 KB |
Output is correct |
68 |
Correct |
39 ms |
36696 KB |
Output is correct |
69 |
Correct |
36 ms |
36512 KB |
Output is correct |
70 |
Correct |
37 ms |
36436 KB |
Output is correct |