Submission #557835

# Submission time Handle Problem Language Result Execution time Memory
557835 2022-05-06T06:23:38 Z Jomnoi Divide and conquer (IZhO14_divide) C++17
0 / 100
0 ms 340 KB
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_N = 1e5 + 10;
 
long long x[MAX_N], g[MAX_N], d[MAX_N];
int tree[4 * MAX_N];
 
void build(int idx, int l, int r) {
    if(l == r) {
        return void(tree[idx] = l);
    }
 
    int mid = (l + r) / 2;
    build(idx * 2, l, mid);
    build(idx * 2 + 1, mid + 1, r);
 
    int L = tree[idx * 2], R = tree[idx * 2 + 1];
    if(x[L] - d[L - 1] >= x[R] - x[R - 1]) {
        tree[idx] = L;
    }
    else {
        tree[idx] = R;
    }
}
 
int query(int idx, int l, int r, int ql, int qr) {
    if(r < ql or qr < l) {
        return -1;
    }
    if(ql <= l and r <= qr) {
        return tree[idx];
    }
 
    int mid = (l + r) / 2;
    int L = query(idx * 2, l, mid, ql, qr);
    int R = query(idx * 2 + 1, mid + 1, r, ql, qr);
 
    if(L == -1) {
        return R;
    }
    if(R == -1) {
        return L;
    }
    if(x[L] - d[L - 1] >= x[R] - d[R - 1]) {
        return L;
    }
    return R;
}
 
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int N;
    cin >> N;
    for(int i = 1; i <= N; i++) {
        cin >> x[i] >> g[i] >> d[i];
        g[i] += g[i - 1];
        d[i] += d[i - 1];
    }
 
    build(1, 1, N);
 
    long long ans = 0;
    for(int i = 1; i <= N; i++) {
        int l = 1, r = i;
        while(l < r) {
            int mid = (l + r) / 2;
 
            int j = query(1, 1, N, 1, mid);
            if(x[i] - x[j] <= d[i] - d[j - 1]) {
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
 
        ans = max(ans, g[i] - g[i - 1]);
        if(x[i] - x[l] <= d[i] - d[l - 1]) {
            ans = max(ans, g[i] - g[l - 1]);
        }
    }
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -