Submission #529704

# Submission time Handle Problem Language Result Execution time Memory
529704 2022-02-23T13:48:27 Z Alex_tz307 Divide and conquer (IZhO14_divide) C++17
48 / 100
1000 ms 6276 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);
  }

  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;
  int64_t l = 0, r = 0;
  for (int i = 1; i <= n; ++i) {
    int g;
    cin >> x[i] >> g >> e[i];
    sum[i] = sum[i - 1] + g;
    maxSelf(l, g);
    r += g;
  }
  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;
}
# Verdict Execution time Memory 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 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory 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 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 3 ms 332 KB Output is correct
17 Correct 6 ms 332 KB Output is correct
18 Correct 10 ms 332 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 4 ms 332 KB Output is correct
21 Correct 7 ms 332 KB Output is correct
22 Correct 13 ms 420 KB Output is correct
23 Correct 64 ms 588 KB Output is correct
24 Correct 47 ms 588 KB Output is correct
# Verdict Execution time Memory 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 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 3 ms 332 KB Output is correct
17 Correct 6 ms 332 KB Output is correct
18 Correct 10 ms 332 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 4 ms 332 KB Output is correct
21 Correct 7 ms 332 KB Output is correct
22 Correct 13 ms 420 KB Output is correct
23 Correct 64 ms 588 KB Output is correct
24 Correct 47 ms 588 KB Output is correct
25 Correct 20 ms 588 KB Output is correct
26 Correct 41 ms 972 KB Output is correct
27 Correct 44 ms 972 KB Output is correct
28 Correct 370 ms 3148 KB Output is correct
29 Correct 678 ms 3148 KB Output is correct
30 Execution timed out 1089 ms 6276 KB Time limit exceeded
31 Halted 0 ms 0 KB -