Submission #529712

# Submission time Handle Problem Language Result Execution time Memory
529712 2022-02-23T14:01:08 Z Alex_tz307 Divide and conquer (IZhO14_divide) C++17
100 / 100
90 ms 5996 KB
#include <bits/stdc++.h>

using namespace std;

const int kN = 1e5;
int x[1 + kN], e[1 + kN];
int64_t sum[1 + kN];

void maxSelf(int64_t &x, int64_t y) {
  if (x < y) {
    x = y;
  }
}

struct ST {
  int n;
  vector<int64_t> t, lazy;

  ST(int N) : n(N) {
    int dim = 1;
    while (dim < n) {
      dim *= 2;
    }
    dim *= 2;
    t.resize(dim);
    lazy.resize(dim);
  }

  void updateNode(int x, int64_t v) {
    t[x] += v;
    lazy[x] += v;
  }

  void push(int x) {
    if (lazy[x] == 0) {
      return;
    }
    for (int i = 0; i < 2; ++i) {
      updateNode(x * 2 + i, lazy[x]);
    }
    lazy[x] = 0;
  }

  void update(int x, int lx, int rx, int st, int dr, int v) {
    if (st <= lx && rx <= dr) {
      updateNode(x, v);
      return;
    }
    push(x);
    int mid = (lx + rx) / 2;
    if (st <= mid) {
      update(x * 2, lx, mid, st, dr, v);
    }
    if (mid < dr) {
      update(x * 2 + 1, mid + 1, rx, st, dr, v);
    }
    t[x] = max(t[x * 2], t[x * 2 + 1]);
  }

  void update(int st, int dr, int64_t v) {
    if (dr < st) {
      return;
    }
    update(1, 1, n, st, dr, v);
  }

  int query(int x, int lx, int rx, int st, int dr) {
    if (lx == rx) {
      if (t[x] < 0) {
        return 0;
      }
      return lx;
    }
    push(x);
    int mid = (lx + rx) / 2, ans = 0;
    if (st <= mid && t[x * 2] >= 0) {
      ans = query(x * 2, lx, mid, st, dr);
    }
    if (ans == 0 && mid < dr && t[x * 2 + 1] >= 0) {
      ans = query(x * 2 + 1, mid + 1, rx, st, dr);
    }
    return ans;
  }

  int query(int st, int dr) {
    return query(1, 1, n, st, dr);
  }
};

void testCase() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int g;
    cin >> x[i] >> g >> e[i];
    sum[i] = sum[i - 1] + g;
  }
  ST t(n);
  int64_t ans = 0;
  for (int r = 1; r <= n; ++r) {
    t.update(1, r - 1, e[r] - (x[r] - x[r - 1]));
    t.update(r, r, e[r]);
    int l = t.query(1, r);
    maxSelf(ans, sum[r] - sum[l - 1]);
  }
  cout << ans << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 216 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 216 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 3 ms 588 KB Output is correct
24 Correct 3 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 216 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 3 ms 588 KB Output is correct
24 Correct 3 ms 588 KB Output is correct
25 Correct 3 ms 588 KB Output is correct
26 Correct 7 ms 972 KB Output is correct
27 Correct 9 ms 972 KB Output is correct
28 Correct 39 ms 3060 KB Output is correct
29 Correct 42 ms 3112 KB Output is correct
30 Correct 88 ms 5964 KB Output is correct
31 Correct 90 ms 5984 KB Output is correct
32 Correct 85 ms 5952 KB Output is correct
33 Correct 88 ms 5996 KB Output is correct
34 Correct 63 ms 5920 KB Output is correct
35 Correct 68 ms 5984 KB Output is correct
36 Correct 68 ms 5956 KB Output is correct