Submission #200182

# Submission time Handle Problem Language Result Execution time Memory
200182 2020-02-05T15:45:07 Z EntityIT Two Dishes (JOI19_dishes) C++14
100 / 100
5858 ms 142344 KB
#include<bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;

template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }

mt19937 rng( (uint32_t)chrono::steady_clock::now().time_since_epoch().count() );

int n, m;
vector<int> p, q;
vector<LL> s, t, pSA, pSB;

struct It {
  vector<LL> sum;
  vector<bool> lz;
  It(int nNode) { sum.assign(nNode, 0); lz.assign(nNode, false); }

  void trueVal(int i, int Left, int Right) {
    if (!lz[i]) return ;
    sum[i] = 0;
    if (Left != Right) lz[i << 1] = lz[i << 1 | 1] = lz[i];
    lz[i] = false;
  }

  void upd(int l, int r, int i = 1, int Left = 0, int Right = m) {
    trueVal(i, Left, Right);
    if (r < Left || Right < l) return ;
    if (l <= Left && Right <= r) {
      lz[i] = true; trueVal(i, Left, Right);
      return ;
    }
    int Mid = (Left + Right) >> 1;
    upd(l, r, i << 1, Left, Mid);
    upd(l, r, i << 1 | 1, Mid + 1, Right);
    sum[i] = sum[i << 1] + sum[i << 1 | 1];
  }
  void add(int pos, LL val, int i = 1, int Left = 0, int Right = m) {
    trueVal(i, Left, Right);
    if (pos < Left || Right < pos) return ;
    if (Left == Right) {
      sum[i] += val;
      return ;
    }
    int Mid = (Left + Right) >> 1;
    add(pos, val, i << 1, Left, Mid);
    add(pos, val, i << 1 | 1, Mid + 1, Right);

    sum[i] = sum[i << 1] + sum[i << 1 | 1];
  }

  int findPos(int l, LL &cur, int i = 1, int Left = 0, int Right = m) {
    trueVal(i, Left, Right);
    if (l <= Left && sum[i] < cur) {
      cur -= sum[i];
      return m + 1;
    }

    if (Left == Right) return Left;

    int Mid = (Left + Right) >> 1;
    int tmp;
    if (l <= Mid && (tmp = findPos(l, cur, i << 1, Left, Mid) ) <= m) return tmp;
    else return findPos(l, cur, i << 1 | 1, Mid + 1, Right);
  }
  LL get(int l, int r, int i = 1, int Left = 0, int Right = m) {
    trueVal(i, Left, Right);
    if (l <= Left && Right <= r) return sum[i];
    int Mid = (Left + Right) >> 1;
    if (Mid < l) return get(l, r, i << 1 | 1, Mid + 1, Right);
    else if (r <= Mid) return get(l, r, i << 1, Left, Mid);
    else return get(l, r, i << 1, Left, Mid) + get(l, r, i << 1 | 1, Mid + 1, Right);
  }
};

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);

  #ifdef FourLeafClover
  freopen("input", "r", stdin);
  #endif // FourLeafCLover

  cin >> n >> m;
  pSA.assign(n + 1, 0);
  pSB.assign(m + 1, 0);
  s.assign(n + 1, 0);
  t.assign(m + 1, 0);
  p.assign(n + 1, 0);
  q.assign(m + 1, 0);

  for (int i = 1; i <= n; ++i) {
    cin >> pSA[i] >> s[i] >> p[i];
    pSA[i] += pSA[i - 1];
  }

  for (int i = 1; i <= m; ++i) {
    cin >> pSB[i] >> t[i] >> q[i];
    pSB[i] += pSB[i - 1];
  }

  vector<int> mxI(m + 1);
  vector<vector<int> > changeMxI(n + 1);
  for (int j = 0; j <= m; ++j) {
    mxI[j] = (int)(upper_bound(all(pSA), t[j] - pSB[j]) - pSA.begin() ) - 1;
    if (mxI[j] + 1 <= n) changeMxI[ mxI[j] + 1 ].emplace_back(j);
  }

  It it( (m + 5) << 2);

  auto upd = [&](int pos) {
    LL tmp = it.get(pos, pos);

    if (tmp >= 0) return ;

    tmp = -tmp;
    int r = (pos < m ? it.findPos(pos + 1, tmp) : m + 1);
    it.add(r, it.get(pos, r - 1) );
    it.upd(pos, r - 1);
  };

  for (int i = 1; i <= n; ++i) {
    int mxJ = (int)(upper_bound(all(pSB), s[i] - pSA[i]) - pSB.begin() ) - 1;

    vector<int> tmp;
    if (mxJ >= 0) {
      it.add(0, p[i]); tmp.emplace_back(0);
      if (mxJ < m) {
        it.add(mxJ + 1, -p[i]);
        tmp.emplace_back(mxJ + 1);
      }
    }

    for (auto j : changeMxI[i]) {
      it.add(j, q[j]);
      tmp.emplace_back(j);
    }

    sort(all(tmp) ); reverse(all(tmp) );
    for (int j : tmp) if (j) upd(j);
  }

  LL ans = it.get(0, m);
  for (int j = 0; j <= m; ++j) ans += mxI[j] < n ? 0 : q[j];

  cout << ans << '\n';

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 398 ms 23928 KB Output is correct
2 Correct 508 ms 23548 KB Output is correct
3 Correct 260 ms 19960 KB Output is correct
4 Correct 329 ms 23796 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 432 ms 22136 KB Output is correct
7 Correct 102 ms 11516 KB Output is correct
8 Correct 125 ms 8952 KB Output is correct
9 Correct 268 ms 19960 KB Output is correct
10 Correct 429 ms 26360 KB Output is correct
11 Correct 205 ms 21116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 8 ms 632 KB Output is correct
18 Correct 7 ms 632 KB Output is correct
19 Correct 10 ms 636 KB Output is correct
20 Correct 13 ms 760 KB Output is correct
21 Correct 9 ms 632 KB Output is correct
22 Correct 10 ms 524 KB Output is correct
23 Correct 9 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 8 ms 632 KB Output is correct
18 Correct 7 ms 632 KB Output is correct
19 Correct 10 ms 636 KB Output is correct
20 Correct 13 ms 760 KB Output is correct
21 Correct 9 ms 632 KB Output is correct
22 Correct 10 ms 524 KB Output is correct
23 Correct 9 ms 632 KB Output is correct
24 Correct 398 ms 23668 KB Output is correct
25 Correct 265 ms 25580 KB Output is correct
26 Correct 417 ms 23796 KB Output is correct
27 Correct 293 ms 29560 KB Output is correct
28 Correct 421 ms 24800 KB Output is correct
29 Correct 230 ms 23288 KB Output is correct
30 Correct 862 ms 27256 KB Output is correct
31 Correct 123 ms 15860 KB Output is correct
32 Correct 112 ms 12152 KB Output is correct
33 Correct 505 ms 25968 KB Output is correct
34 Correct 679 ms 25464 KB Output is correct
35 Correct 947 ms 27256 KB Output is correct
36 Correct 838 ms 27256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 8 ms 632 KB Output is correct
18 Correct 7 ms 632 KB Output is correct
19 Correct 10 ms 636 KB Output is correct
20 Correct 13 ms 760 KB Output is correct
21 Correct 9 ms 632 KB Output is correct
22 Correct 10 ms 524 KB Output is correct
23 Correct 9 ms 632 KB Output is correct
24 Correct 398 ms 23668 KB Output is correct
25 Correct 265 ms 25580 KB Output is correct
26 Correct 417 ms 23796 KB Output is correct
27 Correct 293 ms 29560 KB Output is correct
28 Correct 421 ms 24800 KB Output is correct
29 Correct 230 ms 23288 KB Output is correct
30 Correct 862 ms 27256 KB Output is correct
31 Correct 123 ms 15860 KB Output is correct
32 Correct 112 ms 12152 KB Output is correct
33 Correct 505 ms 25968 KB Output is correct
34 Correct 679 ms 25464 KB Output is correct
35 Correct 947 ms 27256 KB Output is correct
36 Correct 838 ms 27256 KB Output is correct
37 Correct 437 ms 23848 KB Output is correct
38 Correct 324 ms 29680 KB Output is correct
39 Correct 376 ms 29564 KB Output is correct
40 Correct 615 ms 29688 KB Output is correct
41 Correct 5 ms 376 KB Output is correct
42 Correct 1020 ms 27260 KB Output is correct
43 Correct 561 ms 25840 KB Output is correct
44 Correct 673 ms 25336 KB Output is correct
45 Correct 952 ms 27256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 8 ms 632 KB Output is correct
18 Correct 7 ms 632 KB Output is correct
19 Correct 10 ms 636 KB Output is correct
20 Correct 13 ms 760 KB Output is correct
21 Correct 9 ms 632 KB Output is correct
22 Correct 10 ms 524 KB Output is correct
23 Correct 9 ms 632 KB Output is correct
24 Correct 398 ms 23668 KB Output is correct
25 Correct 265 ms 25580 KB Output is correct
26 Correct 417 ms 23796 KB Output is correct
27 Correct 293 ms 29560 KB Output is correct
28 Correct 421 ms 24800 KB Output is correct
29 Correct 230 ms 23288 KB Output is correct
30 Correct 862 ms 27256 KB Output is correct
31 Correct 123 ms 15860 KB Output is correct
32 Correct 112 ms 12152 KB Output is correct
33 Correct 505 ms 25968 KB Output is correct
34 Correct 679 ms 25464 KB Output is correct
35 Correct 947 ms 27256 KB Output is correct
36 Correct 838 ms 27256 KB Output is correct
37 Correct 437 ms 23848 KB Output is correct
38 Correct 324 ms 29680 KB Output is correct
39 Correct 376 ms 29564 KB Output is correct
40 Correct 615 ms 29688 KB Output is correct
41 Correct 5 ms 376 KB Output is correct
42 Correct 1020 ms 27260 KB Output is correct
43 Correct 561 ms 25840 KB Output is correct
44 Correct 673 ms 25336 KB Output is correct
45 Correct 952 ms 27256 KB Output is correct
46 Correct 2282 ms 101284 KB Output is correct
47 Correct 1643 ms 130560 KB Output is correct
48 Correct 1958 ms 130616 KB Output is correct
49 Correct 3097 ms 130552 KB Output is correct
50 Correct 5817 ms 118904 KB Output is correct
51 Correct 3338 ms 109620 KB Output is correct
52 Correct 3732 ms 104952 KB Output is correct
53 Correct 5339 ms 118264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 398 ms 23928 KB Output is correct
2 Correct 508 ms 23548 KB Output is correct
3 Correct 260 ms 19960 KB Output is correct
4 Correct 329 ms 23796 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 432 ms 22136 KB Output is correct
7 Correct 102 ms 11516 KB Output is correct
8 Correct 125 ms 8952 KB Output is correct
9 Correct 268 ms 19960 KB Output is correct
10 Correct 429 ms 26360 KB Output is correct
11 Correct 205 ms 21116 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 6 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 6 ms 376 KB Output is correct
23 Correct 6 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 8 ms 632 KB Output is correct
29 Correct 7 ms 632 KB Output is correct
30 Correct 10 ms 636 KB Output is correct
31 Correct 13 ms 760 KB Output is correct
32 Correct 9 ms 632 KB Output is correct
33 Correct 10 ms 524 KB Output is correct
34 Correct 9 ms 632 KB Output is correct
35 Correct 398 ms 23668 KB Output is correct
36 Correct 265 ms 25580 KB Output is correct
37 Correct 417 ms 23796 KB Output is correct
38 Correct 293 ms 29560 KB Output is correct
39 Correct 421 ms 24800 KB Output is correct
40 Correct 230 ms 23288 KB Output is correct
41 Correct 862 ms 27256 KB Output is correct
42 Correct 123 ms 15860 KB Output is correct
43 Correct 112 ms 12152 KB Output is correct
44 Correct 505 ms 25968 KB Output is correct
45 Correct 679 ms 25464 KB Output is correct
46 Correct 947 ms 27256 KB Output is correct
47 Correct 838 ms 27256 KB Output is correct
48 Correct 437 ms 23848 KB Output is correct
49 Correct 324 ms 29680 KB Output is correct
50 Correct 376 ms 29564 KB Output is correct
51 Correct 615 ms 29688 KB Output is correct
52 Correct 5 ms 376 KB Output is correct
53 Correct 1020 ms 27260 KB Output is correct
54 Correct 561 ms 25840 KB Output is correct
55 Correct 673 ms 25336 KB Output is correct
56 Correct 952 ms 27256 KB Output is correct
57 Correct 352 ms 22516 KB Output is correct
58 Correct 422 ms 28280 KB Output is correct
59 Correct 596 ms 28280 KB Output is correct
60 Correct 401 ms 28408 KB Output is correct
61 Correct 588 ms 25976 KB Output is correct
62 Correct 5 ms 376 KB Output is correct
63 Correct 905 ms 25848 KB Output is correct
64 Correct 573 ms 24304 KB Output is correct
65 Correct 714 ms 23800 KB Output is correct
66 Correct 867 ms 25592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 398 ms 23928 KB Output is correct
2 Correct 508 ms 23548 KB Output is correct
3 Correct 260 ms 19960 KB Output is correct
4 Correct 329 ms 23796 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 432 ms 22136 KB Output is correct
7 Correct 102 ms 11516 KB Output is correct
8 Correct 125 ms 8952 KB Output is correct
9 Correct 268 ms 19960 KB Output is correct
10 Correct 429 ms 26360 KB Output is correct
11 Correct 205 ms 21116 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 6 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 6 ms 376 KB Output is correct
23 Correct 6 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 8 ms 632 KB Output is correct
29 Correct 7 ms 632 KB Output is correct
30 Correct 10 ms 636 KB Output is correct
31 Correct 13 ms 760 KB Output is correct
32 Correct 9 ms 632 KB Output is correct
33 Correct 10 ms 524 KB Output is correct
34 Correct 9 ms 632 KB Output is correct
35 Correct 398 ms 23668 KB Output is correct
36 Correct 265 ms 25580 KB Output is correct
37 Correct 417 ms 23796 KB Output is correct
38 Correct 293 ms 29560 KB Output is correct
39 Correct 421 ms 24800 KB Output is correct
40 Correct 230 ms 23288 KB Output is correct
41 Correct 862 ms 27256 KB Output is correct
42 Correct 123 ms 15860 KB Output is correct
43 Correct 112 ms 12152 KB Output is correct
44 Correct 505 ms 25968 KB Output is correct
45 Correct 679 ms 25464 KB Output is correct
46 Correct 947 ms 27256 KB Output is correct
47 Correct 838 ms 27256 KB Output is correct
48 Correct 437 ms 23848 KB Output is correct
49 Correct 324 ms 29680 KB Output is correct
50 Correct 376 ms 29564 KB Output is correct
51 Correct 615 ms 29688 KB Output is correct
52 Correct 5 ms 376 KB Output is correct
53 Correct 1020 ms 27260 KB Output is correct
54 Correct 561 ms 25840 KB Output is correct
55 Correct 673 ms 25336 KB Output is correct
56 Correct 952 ms 27256 KB Output is correct
57 Correct 2282 ms 101284 KB Output is correct
58 Correct 1643 ms 130560 KB Output is correct
59 Correct 1958 ms 130616 KB Output is correct
60 Correct 3097 ms 130552 KB Output is correct
61 Correct 5817 ms 118904 KB Output is correct
62 Correct 3338 ms 109620 KB Output is correct
63 Correct 3732 ms 104952 KB Output is correct
64 Correct 5339 ms 118264 KB Output is correct
65 Correct 352 ms 22516 KB Output is correct
66 Correct 422 ms 28280 KB Output is correct
67 Correct 596 ms 28280 KB Output is correct
68 Correct 401 ms 28408 KB Output is correct
69 Correct 588 ms 25976 KB Output is correct
70 Correct 5 ms 376 KB Output is correct
71 Correct 905 ms 25848 KB Output is correct
72 Correct 573 ms 24304 KB Output is correct
73 Correct 714 ms 23800 KB Output is correct
74 Correct 867 ms 25592 KB Output is correct
75 Correct 1726 ms 112872 KB Output is correct
76 Correct 2285 ms 142344 KB Output is correct
77 Correct 3183 ms 138144 KB Output is correct
78 Correct 1954 ms 137472 KB Output is correct
79 Correct 5858 ms 126712 KB Output is correct
80 Correct 3331 ms 115176 KB Output is correct
81 Correct 4078 ms 109560 KB Output is correct
82 Correct 5538 ms 120588 KB Output is correct
83 Correct 5770 ms 120372 KB Output is correct