답안 #529703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529703 2022-02-23T13:44:11 Z Alex_tz307 금 캐기 (IZhO14_divide) C++17
48 / 100
1000 ms 9268 KB
#include <bits/stdc++.h>

using namespace std;

const int kN = 1e5;
int x[1 + kN], g[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, int64_t 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);
  }

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

  int64_t query(int st, int dr) {
    if (dr < st) {
      return -1;
    }
    return query(1, 1, n, st, dr);
  }
};

bool check(int n, int64_t k) {
  ST t(n);
  int l = 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]);
    while (l + 1 <= r && sum[r] - sum[l] >= k) {
      l += 1;
    }
    if (t.query(1, l) >= 0) {
      return true;
    }
  }
  return false;
}

void testCase() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x[i] >> g[i] >> e[i];
    sum[i] = sum[i - 1] + g[i];
  }
  int64_t l = 0, r = 1e14L;
  while (l <= r) {
    int64_t mid = (l + r) / 2;
    if (check(n, mid)) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  cout << l - 1 << '\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 332 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 5 ms 332 KB Output is correct
16 Correct 8 ms 396 KB Output is correct
17 Correct 8 ms 332 KB Output is correct
18 Correct 13 ms 456 KB Output is correct
19 Correct 10 ms 332 KB Output is correct
20 Correct 12 ms 408 KB Output is correct
21 Correct 15 ms 332 KB Output is correct
22 Correct 22 ms 460 KB Output is correct
23 Correct 52 ms 716 KB Output is correct
24 Correct 48 ms 780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 5 ms 332 KB Output is correct
16 Correct 8 ms 396 KB Output is correct
17 Correct 8 ms 332 KB Output is correct
18 Correct 13 ms 456 KB Output is correct
19 Correct 10 ms 332 KB Output is correct
20 Correct 12 ms 408 KB Output is correct
21 Correct 15 ms 332 KB Output is correct
22 Correct 22 ms 460 KB Output is correct
23 Correct 52 ms 716 KB Output is correct
24 Correct 48 ms 780 KB Output is correct
25 Correct 61 ms 728 KB Output is correct
26 Correct 129 ms 1140 KB Output is correct
27 Correct 125 ms 1208 KB Output is correct
28 Correct 738 ms 4364 KB Output is correct
29 Correct 727 ms 4676 KB Output is correct
30 Execution timed out 1016 ms 9268 KB Time limit exceeded
31 Halted 0 ms 0 KB -