Submission #218919

# Submission time Handle Problem Language Result Execution time Memory
218919 2020-04-03T07:08:58 Z rama_pang Two Dishes (JOI19_dishes) C++14
74 / 100
7103 ms 266200 KB
#include <bits/stdc++.h>
using namespace std;

using lint = long long;
const lint INF = 1e18;

class SegmentTree {
 private:
  // We keep dp[n] = tree[n] + transition[n], to be able to do updates quickly
  int sz;
  vector<lint> tree;
  vector<lint> lazy_sum;
  vector<lint> lazy_max;
  vector<lint> transition;

  void Push(int n) {
    for (int c = 0; c < 2; c++) {
      tree[n * 2 + c] += lazy_sum[n];
      lazy_sum[n * 2 + c] += lazy_sum[n];
      lazy_max[n * 2 + c] += lazy_sum[n];

      tree[n * 2 + c] = max(tree[n * 2 + c], lazy_max[n]);
      lazy_max[n * 2 + c] = max(lazy_max[n * 2 + c], lazy_max[n]);
    }
    lazy_max[n] = -INF;
    lazy_sum[n] = 0;
  }

  void RangeSumUpdate(int n, int tl, int tr, int l, int r, lint x, bool update_tree) {
    if (r < tl || tr < l || tl > tr || l > r) return;
    if (l <= tl && tr <= r) {
      if (update_tree) {
        tree[n] += x;
        lazy_sum[n] += x;
        lazy_max[n] += x;
      } else {
        transition[n] += x;
      }
      return;
    }
    Push(n);
    int mid = (tl + tr) / 2;
    RangeSumUpdate(n * 2, tl, mid, l, r, x, update_tree);
    RangeSumUpdate(n * 2 + 1, mid + 1, tr, l, r, x, update_tree);
  }

  void RangeMaximumUpdate(int n, int tl, int tr, int l, int r, lint x) {
    if (r < tl || tr < l || tl > tr || l > r) return;
    if (l <= tl && tr <= r) {
      tree[n] = max(tree[n], x);
      lazy_max[n] = max(lazy_max[n], x);
      return;
    }
    Push(n);
    int mid = (tl + tr) / 2;
    RangeMaximumUpdate(n * 2, tl, mid, l, r, x);
    RangeMaximumUpdate(n * 2 + 1, mid + 1, tr, l, r, x); 
  }

  lint QueryTree(int n, int tl, int tr, int pos) {
    if (tl == tr) return tree[n]; // dp[] = tree[] + transition[]
    Push(n);
    int mid = (tl + tr) / 2;
    if (pos <= mid) {
      return QueryTree(n * 2, tl, mid, pos);
    } else {
      return QueryTree(n * 2 + 1, mid + 1, tr, pos);
    }
  }

  lint Query(int n, int tl, int tr, int pos) {
    if (tl == tr) return tree[n] + transition[n]; // dp[] = tree[] + transition[]
    Push(n);
    int mid = (tl + tr) / 2;
    if (pos <= mid) {
      return transition[n] + Query(n * 2, tl, mid, pos);
    } else {
      return transition[n] + Query(n * 2 + 1, mid + 1, tr, pos);
    }
  }
  

 public:
  SegmentTree(int n) : sz(n) {
    tree.assign(4 * sz, 0);
    lazy_sum.assign(4 * sz, 0);
    lazy_max.assign(4 * sz, -INF);
    transition.assign(4 * sz, 0);
  }

  void UpdateSumTree(int l, int r, lint x) { // range sum update on dp
    return RangeSumUpdate(1, 0, sz - 1, l, r, x, true);
  }

  void UpdateTransition(int l, int r, lint x) { // range sum update on transition
    return RangeSumUpdate(1, 0, sz - 1, l, r, x, false);
  }

  void UpdateMaximumTree(int l, int r, lint x) { // dp[k] = max(dp[k], transition[k] + x), x = dp[0], for all nodes
    return RangeMaximumUpdate(1, 0, sz - 1, l, r, x);
  }

  lint QueryTree(int pos) {
    return QueryTree(1, 0, sz - 1, pos);
  }

  lint Query(int pos) {
    return Query(1, 0, sz - 1, pos);
  }
};

int N, M;
vector<int> X; // X[i] = maximum index j, such that A[1] + A[2] + ... + A[i] + B[1] + B[2] + ... + B[j] <= S[i]
vector<int> Y; // Y[j] = maximum index i, such that A[1] + A[2] + ... + A[i] + B[1] + B[2] + ... + B[j] <= T[j]
vector<int> P;
vector<int> Q;

lint NaiveDP(bool dbg = false) {
  vector<vector<lint>> dp(N + 1, vector<lint>(M + 1, -INF));
  dp[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      if (j < M) dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + (i <= Y[j + 1] ? Q[j + 1] : 0));
      if (i < N) dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (j <= X[i + 1] ? P[i + 1] : 0));
    }
  }

  if (dbg) {
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= M; j++) {
        cout << dp[i][j] << " \n"[j == M];
      }
    }
  }

  return dp[N][M];
}

lint SegmentTreeDP() {
  SegmentTree seg(M + 1);
  vector<vector<int>> deactivate(N + 2); // deactivate Q[j] at row i

  for (int j = 1; j <= M; j++) {
    if (0 <= Y[j]) {
      seg.UpdateTransition(j, M, Q[j]);
      deactivate[Y[j]].emplace_back(j);
    }
  }

  for (int i = 0; i <= N; i++) {
    seg.UpdateSumTree(0, X[i], P[i]);
    seg.UpdateMaximumTree(X[i] + 1, M, seg.QueryTree(X[i])); // maintain monotonicity

    sort(begin(deactivate[i]), end(deactivate[i]));
    for (auto &j : deactivate[i]) {
      seg.UpdateTransition(j, M, -Q[j]);
      seg.UpdateSumTree(j, M, Q[j]);

      // make tree[] monotone again, by range maximum update on tree. 
      // This simulates the transition dp[i][j + 1] from dp[i][j]
      // As only the values [j + 1, M] changes, we only need to compare
      // it with the value at [j].
      seg.UpdateMaximumTree(j, M, seg.QueryTree(j - 1));
    }
  }

  return seg.Query(M);
}

void Read() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> N >> M;

  vector<lint> A(N + 1), B(M + 1);
  vector<lint> S(N + 1), T(M + 1);

  P.resize(N + 1), Q.resize(M + 1);
  X.resize(N + 1), Y.resize(M + 1);

  for (int i = 1; i <= N; i++) {
    cin >> A[i] >> S[i] >> P[i];
    A[i] += A[i - 1];
  }

  for (int i = 1; i <= M; i++) {
    cin >> B[i] >> T[i] >> Q[i];
    B[i] += B[i - 1];
  }

  for (int i = 0; i <= N; i++) {
    X[i] = upper_bound(begin(B), end(B), S[i] - A[i]) - begin(B) - 1;
  }

  for (int i = 0; i <= M; i++) {
    Y[i] = upper_bound(begin(A), end(A), T[i] - B[i]) - begin(A) - 1;
  }
}

int main() {
  Read();
  cout << SegmentTreeDP() << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 889 ms 41596 KB Output is correct
2 Correct 972 ms 50660 KB Output is correct
3 Correct 917 ms 48456 KB Output is correct
4 Correct 779 ms 50508 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 891 ms 48320 KB Output is correct
7 Correct 512 ms 35512 KB Output is correct
8 Correct 109 ms 13632 KB Output is correct
9 Correct 880 ms 49532 KB Output is correct
10 Correct 887 ms 47100 KB Output is correct
11 Correct 830 ms 43004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 8 ms 768 KB Output is correct
18 Correct 9 ms 768 KB Output is correct
19 Correct 9 ms 768 KB Output is correct
20 Correct 8 ms 768 KB Output is correct
21 Correct 11 ms 768 KB Output is correct
22 Correct 9 ms 768 KB Output is correct
23 Correct 9 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 8 ms 768 KB Output is correct
18 Correct 9 ms 768 KB Output is correct
19 Correct 9 ms 768 KB Output is correct
20 Correct 8 ms 768 KB Output is correct
21 Correct 11 ms 768 KB Output is correct
22 Correct 9 ms 768 KB Output is correct
23 Correct 9 ms 768 KB Output is correct
24 Correct 1075 ms 44772 KB Output is correct
25 Correct 863 ms 45564 KB Output is correct
26 Correct 681 ms 44924 KB Output is correct
27 Correct 879 ms 50428 KB Output is correct
28 Correct 902 ms 46172 KB Output is correct
29 Correct 857 ms 46716 KB Output is correct
30 Correct 1140 ms 47868 KB Output is correct
31 Correct 560 ms 33592 KB Output is correct
32 Correct 96 ms 11840 KB Output is correct
33 Correct 779 ms 45892 KB Output is correct
34 Correct 1010 ms 45444 KB Output is correct
35 Correct 1061 ms 41596 KB Output is correct
36 Correct 1061 ms 41572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 8 ms 768 KB Output is correct
18 Correct 9 ms 768 KB Output is correct
19 Correct 9 ms 768 KB Output is correct
20 Correct 8 ms 768 KB Output is correct
21 Correct 11 ms 768 KB Output is correct
22 Correct 9 ms 768 KB Output is correct
23 Correct 9 ms 768 KB Output is correct
24 Correct 1075 ms 44772 KB Output is correct
25 Correct 863 ms 45564 KB Output is correct
26 Correct 681 ms 44924 KB Output is correct
27 Correct 879 ms 50428 KB Output is correct
28 Correct 902 ms 46172 KB Output is correct
29 Correct 857 ms 46716 KB Output is correct
30 Correct 1140 ms 47868 KB Output is correct
31 Correct 560 ms 33592 KB Output is correct
32 Correct 96 ms 11840 KB Output is correct
33 Correct 779 ms 45892 KB Output is correct
34 Correct 1010 ms 45444 KB Output is correct
35 Correct 1061 ms 41596 KB Output is correct
36 Correct 1061 ms 41572 KB Output is correct
37 Correct 727 ms 47868 KB Output is correct
38 Correct 913 ms 53220 KB Output is correct
39 Correct 926 ms 50556 KB Output is correct
40 Correct 926 ms 50684 KB Output is correct
41 Correct 5 ms 384 KB Output is correct
42 Correct 1150 ms 51044 KB Output is correct
43 Correct 800 ms 48860 KB Output is correct
44 Correct 1055 ms 48412 KB Output is correct
45 Correct 1122 ms 44632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 8 ms 768 KB Output is correct
18 Correct 9 ms 768 KB Output is correct
19 Correct 9 ms 768 KB Output is correct
20 Correct 8 ms 768 KB Output is correct
21 Correct 11 ms 768 KB Output is correct
22 Correct 9 ms 768 KB Output is correct
23 Correct 9 ms 768 KB Output is correct
24 Correct 1075 ms 44772 KB Output is correct
25 Correct 863 ms 45564 KB Output is correct
26 Correct 681 ms 44924 KB Output is correct
27 Correct 879 ms 50428 KB Output is correct
28 Correct 902 ms 46172 KB Output is correct
29 Correct 857 ms 46716 KB Output is correct
30 Correct 1140 ms 47868 KB Output is correct
31 Correct 560 ms 33592 KB Output is correct
32 Correct 96 ms 11840 KB Output is correct
33 Correct 779 ms 45892 KB Output is correct
34 Correct 1010 ms 45444 KB Output is correct
35 Correct 1061 ms 41596 KB Output is correct
36 Correct 1061 ms 41572 KB Output is correct
37 Correct 727 ms 47868 KB Output is correct
38 Correct 913 ms 53220 KB Output is correct
39 Correct 926 ms 50556 KB Output is correct
40 Correct 926 ms 50684 KB Output is correct
41 Correct 5 ms 384 KB Output is correct
42 Correct 1150 ms 51044 KB Output is correct
43 Correct 800 ms 48860 KB Output is correct
44 Correct 1055 ms 48412 KB Output is correct
45 Correct 1122 ms 44632 KB Output is correct
46 Correct 3858 ms 238940 KB Output is correct
47 Correct 5072 ms 266200 KB Output is correct
48 Correct 5047 ms 252728 KB Output is correct
49 Correct 5036 ms 252556 KB Output is correct
50 Correct 7103 ms 254620 KB Output is correct
51 Correct 4812 ms 241508 KB Output is correct
52 Correct 5986 ms 228040 KB Output is correct
53 Correct 6765 ms 222928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 889 ms 41596 KB Output is correct
2 Correct 972 ms 50660 KB Output is correct
3 Correct 917 ms 48456 KB Output is correct
4 Correct 779 ms 50508 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 891 ms 48320 KB Output is correct
7 Correct 512 ms 35512 KB Output is correct
8 Correct 109 ms 13632 KB Output is correct
9 Correct 880 ms 49532 KB Output is correct
10 Correct 887 ms 47100 KB Output is correct
11 Correct 830 ms 43004 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 8 ms 768 KB Output is correct
29 Correct 9 ms 768 KB Output is correct
30 Correct 9 ms 768 KB Output is correct
31 Correct 8 ms 768 KB Output is correct
32 Correct 11 ms 768 KB Output is correct
33 Correct 9 ms 768 KB Output is correct
34 Correct 9 ms 768 KB Output is correct
35 Correct 1075 ms 44772 KB Output is correct
36 Correct 863 ms 45564 KB Output is correct
37 Correct 681 ms 44924 KB Output is correct
38 Correct 879 ms 50428 KB Output is correct
39 Correct 902 ms 46172 KB Output is correct
40 Correct 857 ms 46716 KB Output is correct
41 Correct 1140 ms 47868 KB Output is correct
42 Correct 560 ms 33592 KB Output is correct
43 Correct 96 ms 11840 KB Output is correct
44 Correct 779 ms 45892 KB Output is correct
45 Correct 1010 ms 45444 KB Output is correct
46 Correct 1061 ms 41596 KB Output is correct
47 Correct 1061 ms 41572 KB Output is correct
48 Correct 727 ms 47868 KB Output is correct
49 Correct 913 ms 53220 KB Output is correct
50 Correct 926 ms 50556 KB Output is correct
51 Correct 926 ms 50684 KB Output is correct
52 Correct 5 ms 384 KB Output is correct
53 Correct 1150 ms 51044 KB Output is correct
54 Correct 800 ms 48860 KB Output is correct
55 Correct 1055 ms 48412 KB Output is correct
56 Correct 1122 ms 44632 KB Output is correct
57 Incorrect 727 ms 48252 KB Output isn't correct
58 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 889 ms 41596 KB Output is correct
2 Correct 972 ms 50660 KB Output is correct
3 Correct 917 ms 48456 KB Output is correct
4 Correct 779 ms 50508 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 891 ms 48320 KB Output is correct
7 Correct 512 ms 35512 KB Output is correct
8 Correct 109 ms 13632 KB Output is correct
9 Correct 880 ms 49532 KB Output is correct
10 Correct 887 ms 47100 KB Output is correct
11 Correct 830 ms 43004 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 8 ms 768 KB Output is correct
29 Correct 9 ms 768 KB Output is correct
30 Correct 9 ms 768 KB Output is correct
31 Correct 8 ms 768 KB Output is correct
32 Correct 11 ms 768 KB Output is correct
33 Correct 9 ms 768 KB Output is correct
34 Correct 9 ms 768 KB Output is correct
35 Correct 1075 ms 44772 KB Output is correct
36 Correct 863 ms 45564 KB Output is correct
37 Correct 681 ms 44924 KB Output is correct
38 Correct 879 ms 50428 KB Output is correct
39 Correct 902 ms 46172 KB Output is correct
40 Correct 857 ms 46716 KB Output is correct
41 Correct 1140 ms 47868 KB Output is correct
42 Correct 560 ms 33592 KB Output is correct
43 Correct 96 ms 11840 KB Output is correct
44 Correct 779 ms 45892 KB Output is correct
45 Correct 1010 ms 45444 KB Output is correct
46 Correct 1061 ms 41596 KB Output is correct
47 Correct 1061 ms 41572 KB Output is correct
48 Correct 727 ms 47868 KB Output is correct
49 Correct 913 ms 53220 KB Output is correct
50 Correct 926 ms 50556 KB Output is correct
51 Correct 926 ms 50684 KB Output is correct
52 Correct 5 ms 384 KB Output is correct
53 Correct 1150 ms 51044 KB Output is correct
54 Correct 800 ms 48860 KB Output is correct
55 Correct 1055 ms 48412 KB Output is correct
56 Correct 1122 ms 44632 KB Output is correct
57 Correct 3858 ms 238940 KB Output is correct
58 Correct 5072 ms 266200 KB Output is correct
59 Correct 5047 ms 252728 KB Output is correct
60 Correct 5036 ms 252556 KB Output is correct
61 Correct 7103 ms 254620 KB Output is correct
62 Correct 4812 ms 241508 KB Output is correct
63 Correct 5986 ms 228040 KB Output is correct
64 Correct 6765 ms 222928 KB Output is correct
65 Incorrect 727 ms 48252 KB Output isn't correct
66 Halted 0 ms 0 KB -