답안 #529710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529710 2022-02-23T13:59:03 Z Alex_tz307 금 캐기 (IZhO14_divide) C++17
100 / 100
93 ms 8220 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);
    if (l) {
      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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 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 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 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 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 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 2 ms 336 KB Output is correct
23 Correct 3 ms 588 KB Output is correct
24 Correct 6 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 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 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 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 2 ms 336 KB Output is correct
23 Correct 3 ms 588 KB Output is correct
24 Correct 6 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 8 ms 972 KB Output is correct
28 Correct 42 ms 3264 KB Output is correct
29 Correct 39 ms 3148 KB Output is correct
30 Correct 89 ms 5948 KB Output is correct
31 Correct 86 ms 7720 KB Output is correct
32 Correct 87 ms 7824 KB Output is correct
33 Correct 93 ms 7680 KB Output is correct
34 Correct 67 ms 7564 KB Output is correct
35 Correct 80 ms 8124 KB Output is correct
36 Correct 67 ms 8220 KB Output is correct