Submission #694137

# Submission time Handle Problem Language Result Execution time Memory
694137 2023-02-03T22:47:33 Z erray Ants and Sugar (JOI22_sugar) C++17
100 / 100
1265 ms 71964 KB
// author: erray
#undef DEBUG 
#include <bits/stdc++.h>

#ifdef DEBUG
  #include "debug.h"
#else
  #define debug(...) void(37)
#endif

using namespace std;

  struct node {
    //0 merge
    //1 max
    array<array<long long, 2>, 2> x{};
    array<long long, 2> tag{};
    void modify(int type, long long d) {
      if (type == 0) {
        //start max
        x[1][0] += d, x[1][1] += d;
        tag[type] += d;
      } else if (type == 1) {
        //end max
        x[0][1] += d, x[1][1] += d;
        tag[type] += d;      
      } else if (type == 2) {
        //start merge
        x[0][0] += d, x[0][1] += d; 
      } else if (type == 3) {
        //end merge
        x[0][0] += d, x[1][0] += d;      
      } else {
        //whole 
        x[0][0] += d, x[0][1] += d, x[1][0] += d, x[1][1] += d;
      }
    } 
  };
  node operator+(node l, node r) {
    l.x[1][1] = max(0LL, l.x[1][1]);
    r.x[1][1] = max(0LL, r.x[1][1]);
    node res;
    res.x[0][0] = max(l.x[0][1] + r.x[1][0], l.x[0][0] + r.x[0][0]);
    res.x[1][1] = max(l.x[1][1] + r.x[1][1], l.x[1][0] + r.x[0][1]);
    res.x[0][1] = max(l.x[0][1] + r.x[1][1], l.x[0][0] + r.x[0][1]);
    res.x[1][0] = max(r.x[1][0] + l.x[1][1], l.x[1][0] + r.x[0][0]);
    return res;
  }

struct SegTree {
  int n;
  vector<node> tree;
  #define def int mid = (l + r) >> 1, rv = v + ((mid - l + 1) << 1)
  void push(int v, int l, int r) {
    def;
    for (int i = 0; i < 2; ++i) {
      if (tree[v].tag[i] != 0) {
        tree[v + 1].modify(i, tree[v].tag[i]);
        tree[rv].modify(i, tree[v].tag[i]);        
        tree[v].tag[i] = 0;
      }
    }
  }
  void modify(int v, int l, int r, int ll, int rr, int type, int x) {
    if (l >= ll && rr >= r) {
      tree[v].modify(type, x);
      debug(v, l, tree[v].x);
      return;
    }        
    def;
    push(v, l, r);
    //debug(v, v + 1, rv);
    if (ll <= mid) {
      modify(v + 1, l, mid, ll, rr, type, x);
    }                 	
    if (mid < rr) {
      modify(rv, mid + 1, r, ll, rr, type, x);
    }
    tree[v] = tree[v + 1] + tree[rv];
    debug(v, l, r, tree[v].x);  
  }
  SegTree(int _n) : n(_n) {
    tree.resize((n << 1) - 1);
  }
  long long get() {
    return max(0LL, tree[0].x[1][1]);
  }
  void modify(int l, int r, int type, int x) {
     assert(0 <= l && l <= r && r < n && 0 <= type && type < 4);
     debug(l, r, type, x);
     modify(0, 0, n - 1, l, r, type, x);
  }
  void modify(int p, int x) {
    assert(0 <= p && p < n);
    debug(p, x);
    modify(0, 0, n - 1, p, p, 4, x);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int Q, L;
  cin >> Q >> L;
  vector<int> T(Q), X(Q), A(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> T[i] >> X[i] >> A[i];
  }
  debug(X);
  vector<int> cords;
  for (int i = 0; i < Q; ++i) {
    if (T[i] == 1) {
      cords.push_back(X[i]);
    }
  }
  sort(cords.begin(), cords.end());
  int s = int(unique(cords.begin(), cords.end()) - cords.begin());
  cords.resize(s);
  if (s == 0) {
    for (int i = 0; i < Q; ++i) {
      cout << 0 << '\n';
    }
    return 0;
  }
  if (s == 1) {
    long long V = 0;
    long long U = 0;
    for (int i = 0; i < Q; ++i) {
      if (T[i] == 1) {
        V += A[i];
      } else if (abs(cords[0] - X[i]) <= L) {
        U += A[i];
      }
      cout << min(V, U) << '\n';
    }
    return 0;
  }
  auto LB = [&](int x) {
    return int(lower_bound(cords.begin(), cords.end(), x) - cords.begin());
  };
  debug(cords);

  SegTree st(s);
  long long total = 0;
  for (int q = 0; q < Q; ++q) {
    debug(q, total);
    if (T[q] == 1) {
      total += A[q]; 
      st.modify(LB(X[q]), A[q]);
    } else {
      int id = LB(X[q]);
      debug(id, A[q]);
      if (id < s && cords[id] == X[q]) {
        st.modify(LB(X[q]), -A[q]);
      } else if (id == s || (id > 0 && X[q] <= cords[id - 1] + L))  {
        st.modify(id - 1, id - 1, 3, -A[q]);
      } else {
        st.modify(id, id, 2, -A[q]);        
      }
      {
        int l = LB(X[q] - L);
        int r = LB(X[q]) - 1;
        debug(l, r);
        if (l <= r) {
          st.modify(l, r, 1, -A[q]);   
        }
      }
      {
         int r = LB(X[q] + L + 1) - 1;
         int l = LB(X[q] + 1);
         debug(l, r);
         if (l <= r) {
           st.modify(l, r, 0, -A[q]);
         }
      }
    }
    debug(total, st.get());
    cout << total - st.get() << '\n';
  }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 4 ms 596 KB Output is correct
12 Correct 4 ms 596 KB Output is correct
13 Correct 4 ms 548 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 336 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 724 KB Output is correct
19 Correct 3 ms 596 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 592 KB Output is correct
22 Correct 3 ms 724 KB Output is correct
23 Correct 4 ms 596 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
25 Correct 4 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 573 ms 24804 KB Output is correct
5 Correct 316 ms 21808 KB Output is correct
6 Correct 694 ms 28176 KB Output is correct
7 Correct 323 ms 21940 KB Output is correct
8 Correct 772 ms 32712 KB Output is correct
9 Correct 666 ms 41864 KB Output is correct
10 Correct 875 ms 32624 KB Output is correct
11 Correct 668 ms 41740 KB Output is correct
12 Correct 278 ms 19032 KB Output is correct
13 Correct 405 ms 24912 KB Output is correct
14 Correct 645 ms 38400 KB Output is correct
15 Correct 649 ms 38320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 288 ms 19012 KB Output is correct
3 Correct 400 ms 24892 KB Output is correct
4 Correct 694 ms 38280 KB Output is correct
5 Correct 667 ms 38140 KB Output is correct
6 Correct 564 ms 27860 KB Output is correct
7 Correct 35 ms 3068 KB Output is correct
8 Correct 275 ms 16336 KB Output is correct
9 Correct 497 ms 22240 KB Output is correct
10 Correct 802 ms 37932 KB Output is correct
11 Correct 868 ms 38096 KB Output is correct
12 Correct 845 ms 37828 KB Output is correct
13 Correct 785 ms 37648 KB Output is correct
14 Correct 800 ms 37580 KB Output is correct
15 Correct 690 ms 37192 KB Output is correct
16 Correct 780 ms 37116 KB Output is correct
17 Correct 1008 ms 37244 KB Output is correct
18 Correct 1152 ms 37060 KB Output is correct
19 Correct 1116 ms 36960 KB Output is correct
20 Correct 1210 ms 36796 KB Output is correct
21 Correct 1195 ms 36696 KB Output is correct
22 Correct 1265 ms 36608 KB Output is correct
23 Correct 1183 ms 36452 KB Output is correct
24 Correct 1050 ms 36476 KB Output is correct
25 Correct 1045 ms 36320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 4 ms 596 KB Output is correct
12 Correct 4 ms 596 KB Output is correct
13 Correct 4 ms 548 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 336 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 724 KB Output is correct
19 Correct 3 ms 596 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 592 KB Output is correct
22 Correct 3 ms 724 KB Output is correct
23 Correct 4 ms 596 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
25 Correct 4 ms 592 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 573 ms 24804 KB Output is correct
30 Correct 316 ms 21808 KB Output is correct
31 Correct 694 ms 28176 KB Output is correct
32 Correct 323 ms 21940 KB Output is correct
33 Correct 772 ms 32712 KB Output is correct
34 Correct 666 ms 41864 KB Output is correct
35 Correct 875 ms 32624 KB Output is correct
36 Correct 668 ms 41740 KB Output is correct
37 Correct 278 ms 19032 KB Output is correct
38 Correct 405 ms 24912 KB Output is correct
39 Correct 645 ms 38400 KB Output is correct
40 Correct 649 ms 38320 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 288 ms 19012 KB Output is correct
43 Correct 400 ms 24892 KB Output is correct
44 Correct 694 ms 38280 KB Output is correct
45 Correct 667 ms 38140 KB Output is correct
46 Correct 564 ms 27860 KB Output is correct
47 Correct 35 ms 3068 KB Output is correct
48 Correct 275 ms 16336 KB Output is correct
49 Correct 497 ms 22240 KB Output is correct
50 Correct 802 ms 37932 KB Output is correct
51 Correct 868 ms 38096 KB Output is correct
52 Correct 845 ms 37828 KB Output is correct
53 Correct 785 ms 37648 KB Output is correct
54 Correct 800 ms 37580 KB Output is correct
55 Correct 690 ms 37192 KB Output is correct
56 Correct 780 ms 37116 KB Output is correct
57 Correct 1008 ms 37244 KB Output is correct
58 Correct 1152 ms 37060 KB Output is correct
59 Correct 1116 ms 36960 KB Output is correct
60 Correct 1210 ms 36796 KB Output is correct
61 Correct 1195 ms 36696 KB Output is correct
62 Correct 1265 ms 36608 KB Output is correct
63 Correct 1183 ms 36452 KB Output is correct
64 Correct 1050 ms 36476 KB Output is correct
65 Correct 1045 ms 36320 KB Output is correct
66 Correct 430 ms 25548 KB Output is correct
67 Correct 561 ms 30052 KB Output is correct
68 Correct 675 ms 35080 KB Output is correct
69 Correct 670 ms 32152 KB Output is correct
70 Correct 738 ms 48456 KB Output is correct
71 Correct 793 ms 48640 KB Output is correct
72 Correct 834 ms 48696 KB Output is correct
73 Correct 920 ms 48872 KB Output is correct
74 Correct 148 ms 17740 KB Output is correct
75 Correct 447 ms 23984 KB Output is correct
76 Correct 735 ms 71964 KB Output is correct
77 Correct 670 ms 67112 KB Output is correct
78 Correct 735 ms 66816 KB Output is correct
79 Correct 843 ms 48876 KB Output is correct
80 Correct 731 ms 66960 KB Output is correct
81 Correct 922 ms 48828 KB Output is correct
82 Correct 722 ms 66812 KB Output is correct
83 Correct 1138 ms 48964 KB Output is correct
84 Correct 704 ms 66824 KB Output is correct
85 Correct 1199 ms 49112 KB Output is correct
86 Correct 712 ms 66960 KB Output is correct
87 Correct 1234 ms 48972 KB Output is correct
88 Correct 672 ms 66968 KB Output is correct
89 Correct 907 ms 60912 KB Output is correct