Submission #604302

# Submission time Handle Problem Language Result Execution time Memory
604302 2022-07-25T03:12:25 Z lunchbox Feast (NOI19_feast) C++17
100 / 100
815 ms 210980 KB
#include <bits/stdc++.h>
using namespace std;

template<class T, class F>
T last_true(T lower, T upper, const F& f) {
  lower--;
  assert(lower <= upper);
  while (lower < upper) {
    T t = lower + (upper - lower + 1) / 2;

    if (f(t))
      lower = t;
    else
      upper = t - 1;
  }
  return lower;
}

const int N = 300000, N_ = 1 << 19;

#define int long long

array<int, 3> add(const array<int, 3> &l, const array<int, 3> &r) {
  assert(l[2] + 1 == r[1]);
  return { l[0] + r[0], l[1], r[2] };
}
 
struct T {
  array<int, 3> pref[2], suff[2], ans[2], sum[2];
  int empty;
 
  T() {
    for (int i = 0; i < 2; i++)
      pref[i] = suff[i] = ans[i] = sum[i] = { 0, 0, 0 };
    empty = 1;
  }
  void init(int i, int x) {
    sum[0] = { x, i, i }, sum[1] = { -x, i, i };
    if (x >= 0) {
      pref[0] = suff[0] = { +x, i, i };
      pref[1] = { 0, i, i - 1 }, suff[1] = { 0, i + 1, i };
    } else {
      pref[1] = suff[1] = {-x, i, i};
      pref[0] = { 0, i, i - 1 }, suff[0] = { 0, i + 1, i };
    }
    ans[0] = pref[0], ans[1] = pref[1];
    empty = 0;
  }
  void flip() {
    swap(pref[0], pref[1]);
    swap(suff[0], suff[1]);
    swap(ans[0], ans[1]);
    swap(sum[0], sum[1]);
  }
  friend T add(const T &l, const T &r) {
    T k = T();
 
    if (l.empty)
      return r;
    if (r.empty)
      return l;
    k.sum[0] = add(l.sum[0], r.sum[0]);
    k.pref[0] = max(l.pref[0], add(l.sum[0], r.pref[0]));
    k.suff[0] = max(r.suff[0], add(l.suff[0], r.sum[0]));
    k.ans[0] = max({ l.ans[0], r.ans[0], add(l.suff[0], r.pref[0]) });
    k.sum[1] = add(l.sum[1], r.sum[1]);
    k.pref[1] = max(l.pref[1], add(l.sum[1], r.pref[1]));
    k.suff[1] = max(r.suff[1], add(l.suff[1], r.sum[1]));
    k.ans[1] = max({ l.ans[1], r.ans[1], add(l.suff[1], r.pref[1]) });
    k.empty = 0;
    return k;
  }
} st[N_ * 2];
 
int lz[N_ * 2], n_;
 
void put(int k, int l, int r) {
  st[k].flip();
  if (l != r)
    lz[k] ^= 1;
}
 
void pus(int k, int l, int r) {
  if (lz[k]) {
    int m = (l + r) / 2;
 
    put(k << 1 | 0, l, m), put(k << 1 | 1, m + 1, r);
    lz[k] = 0;
  }
}
 
void pul(int k, int, int) {
  st[k] = add(st[k << 1 | 0], st[k << 1 | 1]);
}
 
T query(int ql, int qr, int k = 1, int l = 0, int r = n_ - 1) {
  if (ql > r || qr < l)
    return T();
  else if (ql <= l && qr >= r)
    return st[k];
  else {
    int m = (l + r) / 2;
 
    pus(k, l, r);
    return add(query(ql, qr, k << 1 | 0, l, m), query(ql, qr, k << 1 | 1, m + 1, r));
  }
}
 
void update1(int i, int x, int k = 1, int l = 0, int r = n_ - 1) {
  if (l == r)
    st[k].init(i, x);
  else {
    int m = (l + r) / 2;
 
    pus(k, l, r);
    if (i <= m)
      update1(i, x, k << 1 | 0, l, m);
    else
      update1(i, x, k << 1 | 1, m + 1, r);
    pul(k, l, r);
  }
}
 
void update2(int ql, int qr, int k = 1, int l = 0, int r = n_ - 1) {
  if (ql > r || qr < l)
    return;
  else if (ql <= l && qr >= r)
    put(k, l, r);
  else {
    int m = (l + r) / 2;
 
    pus(k, l, r);
    update2(ql, qr, k << 1 | 0, l, m), update2(ql, qr, k << 1 | 1, m + 1, r);
    pul(k, l, r);
  }
}

signed main() {
  int n, k;
  scanf("%lld%lld", &n, &k);
  n_ = 1;
  while (n_ < n)
    n_ <<= 1;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%lld", &a);
    update1(i, a);
  }
  int sum = 0;
  for (int i = 0; i < k; i++) {
    auto t = query(0, n - 1).ans[0];
    
    if (t[0] == 0)
      break;
    // printf("got %d\n", t[0]);
    sum += t[0];
    update2(t[1], t[2]);
  }
  printf("%lld\n", sum);
  return 0;
}

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:140:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |   scanf("%lld%lld", &n, &k);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
feast.cpp:146:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |     scanf("%lld", &a);
      |     ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 485 ms 205488 KB Output is correct
2 Correct 500 ms 205472 KB Output is correct
3 Correct 500 ms 205584 KB Output is correct
4 Correct 506 ms 205492 KB Output is correct
5 Correct 515 ms 205616 KB Output is correct
6 Correct 483 ms 205564 KB Output is correct
7 Correct 489 ms 205516 KB Output is correct
8 Correct 490 ms 205520 KB Output is correct
9 Correct 482 ms 205552 KB Output is correct
10 Correct 506 ms 205504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 480 ms 205488 KB Output is correct
2 Correct 504 ms 205560 KB Output is correct
3 Correct 474 ms 205516 KB Output is correct
4 Correct 475 ms 205564 KB Output is correct
5 Correct 486 ms 205488 KB Output is correct
6 Correct 465 ms 205496 KB Output is correct
7 Correct 488 ms 205496 KB Output is correct
8 Correct 490 ms 205616 KB Output is correct
9 Correct 483 ms 205560 KB Output is correct
10 Correct 481 ms 205516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 491 ms 205516 KB Output is correct
2 Correct 483 ms 208460 KB Output is correct
3 Correct 492 ms 208552 KB Output is correct
4 Correct 497 ms 208480 KB Output is correct
5 Correct 504 ms 208416 KB Output is correct
6 Correct 487 ms 208444 KB Output is correct
7 Correct 523 ms 208588 KB Output is correct
8 Correct 494 ms 208500 KB Output is correct
9 Correct 496 ms 208520 KB Output is correct
10 Correct 503 ms 208552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 205460 KB Output is correct
2 Correct 95 ms 205452 KB Output is correct
3 Correct 91 ms 205400 KB Output is correct
4 Correct 91 ms 205476 KB Output is correct
5 Correct 96 ms 205492 KB Output is correct
6 Correct 96 ms 205496 KB Output is correct
7 Correct 91 ms 205384 KB Output is correct
8 Correct 89 ms 205452 KB Output is correct
9 Correct 92 ms 205452 KB Output is correct
10 Correct 96 ms 205388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 205460 KB Output is correct
2 Correct 95 ms 205452 KB Output is correct
3 Correct 91 ms 205400 KB Output is correct
4 Correct 91 ms 205476 KB Output is correct
5 Correct 96 ms 205492 KB Output is correct
6 Correct 96 ms 205496 KB Output is correct
7 Correct 91 ms 205384 KB Output is correct
8 Correct 89 ms 205452 KB Output is correct
9 Correct 92 ms 205452 KB Output is correct
10 Correct 96 ms 205388 KB Output is correct
11 Correct 91 ms 205384 KB Output is correct
12 Correct 94 ms 205396 KB Output is correct
13 Correct 99 ms 205392 KB Output is correct
14 Correct 92 ms 205368 KB Output is correct
15 Correct 92 ms 205484 KB Output is correct
16 Correct 92 ms 205496 KB Output is correct
17 Correct 91 ms 205476 KB Output is correct
18 Correct 100 ms 205436 KB Output is correct
19 Correct 91 ms 205436 KB Output is correct
20 Correct 91 ms 205412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 205460 KB Output is correct
2 Correct 95 ms 205452 KB Output is correct
3 Correct 91 ms 205400 KB Output is correct
4 Correct 91 ms 205476 KB Output is correct
5 Correct 96 ms 205492 KB Output is correct
6 Correct 96 ms 205496 KB Output is correct
7 Correct 91 ms 205384 KB Output is correct
8 Correct 89 ms 205452 KB Output is correct
9 Correct 92 ms 205452 KB Output is correct
10 Correct 96 ms 205388 KB Output is correct
11 Correct 91 ms 205384 KB Output is correct
12 Correct 94 ms 205396 KB Output is correct
13 Correct 99 ms 205392 KB Output is correct
14 Correct 92 ms 205368 KB Output is correct
15 Correct 92 ms 205484 KB Output is correct
16 Correct 92 ms 205496 KB Output is correct
17 Correct 91 ms 205476 KB Output is correct
18 Correct 100 ms 205436 KB Output is correct
19 Correct 91 ms 205436 KB Output is correct
20 Correct 91 ms 205412 KB Output is correct
21 Correct 93 ms 205420 KB Output is correct
22 Correct 94 ms 205500 KB Output is correct
23 Correct 94 ms 205512 KB Output is correct
24 Correct 92 ms 205424 KB Output is correct
25 Correct 94 ms 205488 KB Output is correct
26 Correct 93 ms 205632 KB Output is correct
27 Correct 94 ms 205408 KB Output is correct
28 Correct 97 ms 205516 KB Output is correct
29 Correct 99 ms 205420 KB Output is correct
30 Correct 93 ms 205512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 485 ms 205488 KB Output is correct
2 Correct 500 ms 205472 KB Output is correct
3 Correct 500 ms 205584 KB Output is correct
4 Correct 506 ms 205492 KB Output is correct
5 Correct 515 ms 205616 KB Output is correct
6 Correct 483 ms 205564 KB Output is correct
7 Correct 489 ms 205516 KB Output is correct
8 Correct 490 ms 205520 KB Output is correct
9 Correct 482 ms 205552 KB Output is correct
10 Correct 506 ms 205504 KB Output is correct
11 Correct 480 ms 205488 KB Output is correct
12 Correct 504 ms 205560 KB Output is correct
13 Correct 474 ms 205516 KB Output is correct
14 Correct 475 ms 205564 KB Output is correct
15 Correct 486 ms 205488 KB Output is correct
16 Correct 465 ms 205496 KB Output is correct
17 Correct 488 ms 205496 KB Output is correct
18 Correct 490 ms 205616 KB Output is correct
19 Correct 483 ms 205560 KB Output is correct
20 Correct 481 ms 205516 KB Output is correct
21 Correct 491 ms 205516 KB Output is correct
22 Correct 483 ms 208460 KB Output is correct
23 Correct 492 ms 208552 KB Output is correct
24 Correct 497 ms 208480 KB Output is correct
25 Correct 504 ms 208416 KB Output is correct
26 Correct 487 ms 208444 KB Output is correct
27 Correct 523 ms 208588 KB Output is correct
28 Correct 494 ms 208500 KB Output is correct
29 Correct 496 ms 208520 KB Output is correct
30 Correct 503 ms 208552 KB Output is correct
31 Correct 91 ms 205460 KB Output is correct
32 Correct 95 ms 205452 KB Output is correct
33 Correct 91 ms 205400 KB Output is correct
34 Correct 91 ms 205476 KB Output is correct
35 Correct 96 ms 205492 KB Output is correct
36 Correct 96 ms 205496 KB Output is correct
37 Correct 91 ms 205384 KB Output is correct
38 Correct 89 ms 205452 KB Output is correct
39 Correct 92 ms 205452 KB Output is correct
40 Correct 96 ms 205388 KB Output is correct
41 Correct 91 ms 205384 KB Output is correct
42 Correct 94 ms 205396 KB Output is correct
43 Correct 99 ms 205392 KB Output is correct
44 Correct 92 ms 205368 KB Output is correct
45 Correct 92 ms 205484 KB Output is correct
46 Correct 92 ms 205496 KB Output is correct
47 Correct 91 ms 205476 KB Output is correct
48 Correct 100 ms 205436 KB Output is correct
49 Correct 91 ms 205436 KB Output is correct
50 Correct 91 ms 205412 KB Output is correct
51 Correct 93 ms 205420 KB Output is correct
52 Correct 94 ms 205500 KB Output is correct
53 Correct 94 ms 205512 KB Output is correct
54 Correct 92 ms 205424 KB Output is correct
55 Correct 94 ms 205488 KB Output is correct
56 Correct 93 ms 205632 KB Output is correct
57 Correct 94 ms 205408 KB Output is correct
58 Correct 97 ms 205516 KB Output is correct
59 Correct 99 ms 205420 KB Output is correct
60 Correct 93 ms 205512 KB Output is correct
61 Correct 709 ms 210864 KB Output is correct
62 Correct 815 ms 210980 KB Output is correct
63 Correct 520 ms 210896 KB Output is correct
64 Correct 699 ms 210892 KB Output is correct
65 Correct 618 ms 210764 KB Output is correct
66 Correct 611 ms 210760 KB Output is correct
67 Correct 605 ms 210848 KB Output is correct
68 Correct 490 ms 210508 KB Output is correct
69 Correct 484 ms 209740 KB Output is correct
70 Correct 485 ms 208552 KB Output is correct