QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#133333#3998. The Profiteernhuang685RE 817ms97020kbC++204.4kb2023-08-02 00:17:442023-08-02 00:17:47

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-02 00:17:47]
  • 评测
  • 测评结果:RE
  • 用时:817ms
  • 内存:97020kb
  • [2023-08-02 00:17:44]
  • 提交

answer

/**
 * @file qoj3998F1.cpp
 * @author n685
 * @brief
 * @date 2023-07-19
 *
 *
 */
#include <bits/stdc++.h>

#ifdef LOCAL
std::ifstream cin;
std::ofstream cout;
using std::cerr;
#else
using std::cin;
using std::cout;
#define cerr                                                                   \
  if (false)                                                                   \
  std::cerr
#endif

namespace K {
int64_t mx;

int64_t sz = 1;
std::vector<std::vector<int64_t>> val;
void rollback(int a) { sz -= a; }
void add(int64_t v, int64_t c) {
  sz++;
  auto &pre = val[sz - 2];
  auto &cur = val[sz - 1];
  int64_t ans = 0;
  for (int64_t i = 0; i <= mx; ++i) {
    cur[i] = pre[i];
    if (i >= c)
      cur[i] = std::max(cur[i], pre[i - c] + v);
    ans += cur[i];
  }
  cur.back() = ans;
}
int64_t sum() { return val[sz - 1].back(); }
}; // namespace K

int main() {
#ifdef LOCAL
  cin.open("input.txt");
  cout.rdbuf()->pubsetbuf(0, 0);
  cout.open("output.txt");
#else
  cin.tie(nullptr)->sync_with_stdio(false);
#endif

  int n;
  int64_t k, E;

  cin >> n >> k >> E;
  K::mx = k;
  // K::val.push_back(std::vector<int64_t>(k + 2));
  K::val.resize(n + 1);
  for (int i = 0; i <= n; ++i)
    K::val[i].resize(k + 2);
  K::sz = 1;
  E *= k;

  std::vector<int64_t> v(n), a(n), b(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> a[i] >> b[i];

  int64_t ans = 0;
  auto dnq = [&](auto &self, int l, int r, int dl, int dr) {
    if (dl == dr) {
      if (dl == n - 1) {
        int dm = dl;
        for (int i = std::max(r + 1, dl); i <= dr; ++i) {
          if (i > dm)
            K::add(v[i], a[i]);
          else
            K::add(v[i], b[i]);
        }

        int ll = l, rr = std::min(r, dm);
        for (int i = dm + 1; i <= r; ++i)
          K::add(v[i], a[i]);
        while (ll < rr) {
          int mid = (ll + rr + 1) / 2;
          // add from mid to rr
          for (int i = ll; i < mid; ++i)
            K::add(v[i], a[i]);
          for (int i = mid; i <= rr; ++i)
            K::add(v[i], b[i]);
          if (K::sum() <= E) {
            K::rollback(rr - mid + 1);
            ll = mid;
          } else {
            K::rollback(rr - ll + 1);
            for (int i = mid; i <= rr; ++i)
              K::add(v[i], b[i]);
            rr = mid - 1;
          }
        }

        int m = ll;
        K::add(v[m], b[m]);
        bool g = (K::sum() <= E);
        // added all but m
        K::rollback(std::min(r, dm) - l + dr - std::max(r + 1, dl) +
                    std::max(0, r - dm) + 2);
        if (g)
          ans += (int64_t)(ll - l + 1) * (n - dl);
      } else
        ans += (int64_t)(r - l + 1) * (n - dl);
      return;
    }
    int dm = (dl + dr) / 2;
    for (int i = std::max(r + 1, dl); i <= dr; ++i) {
      if (i > dm)
        K::add(v[i], a[i]);
      else
        K::add(v[i], b[i]);
    }

    int ll = l, rr = std::min(r, dm);
    for (int i = dm + 1; i <= r; ++i)
      K::add(v[i], a[i]);
    while (ll < rr) {
      int mid = (ll + rr + 1) / 2;
      // add from mid to rr
      for (int i = ll; i < mid; ++i)
        K::add(v[i], a[i]);
      for (int i = mid; i <= rr; ++i)
        K::add(v[i], b[i]);
      if (K::sum() <= E) {
        K::rollback(rr - mid + 1);
        ll = mid;
      } else {
        K::rollback(rr - ll + 1);
        for (int i = mid; i <= rr; ++i)
          K::add(v[i], b[i]);
        rr = mid - 1;
      }
    }

    int m = ll;
    K::add(v[m], b[m]);
    bool g = (K::sum() <= E);
    // added all but m
    K::rollback(std::min(r, dm) - l + dr - std::max(r + 1, dl) +
                std::max(0, r - dm) + 2);
    if (!g)
      m = l - 1;

    if (g) {
      for (int i = m + 1; i < std::min(r + 1, dl); ++i)
        K::add(v[i], b[i]);
      for (int i = std::max(m + 1, dm + 1); i <= dr; ++i)
        K::add(v[i], a[i]);
      self(self, l, m, dl, dm);
      K::rollback(std::max(0, std::min(r + 1, dl) - m - 1) + dr -
                  std::max(m, dm));
    }

    if (m + 1 <= r) {
      for (int i = l; i <= std::min(dm, m); ++i)
        K::add(v[i], a[i]);
      for (int i = std::max(r + 1, dl); i <= dm; ++i)
        K::add(v[i], b[i]);
      self(self, m + 1, r, dm + 1, dr);
      K::rollback(dm - std::max(r + 1, dl) +
                  std::max(0, std::min(dm, m) - l + 1) + 1);
    }
  };
  dnq(dnq, 0, n - 1, 0, n - 1);

  cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3476kb

input:

4 5 3
3 2 4
1 2 3
2 1 2
3 1 3

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3532kb

input:

4 5 4
3 2 4
1 2 3
2 1 2
3 1 3

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3524kb

input:

4 5 5
3 2 4
1 2 3
2 1 2
3 1 3

output:

7

result:

ok single line: '7'

Test #4:

score: 0
Accepted
time: 753ms
memory: 96948kb

input:

200000 50 23333
2620 5 21
8192 17 34
6713 38 46
6687 13 42
390 9 13
4192 7 37
7898 17 21
1471 16 45
3579 22 40
9628 8 23
7164 28 45
3669 14 31
5549 29 35
4670 30 39
5811 15 20
4162 18 29
7590 29 41
7786 23 35
383 9 40
5576 39 46
5586 4 9
1110 14 33
8568 4 6
8548 39 42
9133 10 42
6679 22 39
8353 33 3...

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 817ms
memory: 97020kb

input:

200000 50 233332
8019 18 20
3111 27 40
2015 16 47
6491 17 18
1224 30 38
428 23 34
7521 4 41
7252 6 33
4121 32 45
4230 18 35
1605 21 42
9489 17 25
3704 35 45
6202 8 22
6493 1 5
3041 14 46
4509 23 43
9646 11 48
5294 19 27
3562 19 40
9408 30 47
1751 21 29
4053 4 27
5596 32 49
8609 13 29
1686 4 31
3588 ...

output:

17523021

result:

ok single line: '17523021'

Test #6:

score: -100
Runtime Error

input:

200000 50 2333331
7420 30 44
8652 22 28
5418 8 21
9825 3 8
4257 21 40
9962 6 16
3370 18 41
2192 37 41
231 8 18
7764 3 41
3455 9 18
1159 8 46
9775 9 42
4400 21 43
4593 10 22
712 7 22
2067 21 27
9618 9 35
8008 13 38
114 4 31
4503 39 49
4661 14 41
4837 15 35
1371 12 16
9568 21 48
8934 16 34
870 4 35
83...

output:


result: