제출 #375382

#제출 시각아이디문제언어결과실행 시간메모리
375382Alex_tz307Art Exhibition (JOI18_art)C++17
100 / 100
361 ms94572 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int NMAX = 5e5 + 5;
int N, pref_sum[NMAX], lg2[NMAX], rmq_max[NMAX][20], ans;
pair<int,int> a[NMAX];

void compute_lg2() {
    lg2[1] = 0;
    for(int i = 2; i <= N; ++i)
        lg2[i] = lg2[i >> 1] + 1;
}

void compute_rmq() {
    for(int i = 1; i <= N; ++i)
        rmq_max[i][0] = pref_sum[i] - a[i].first;
    for(int j = 1; (1 << j) <= N; ++j)
        for(int i = 1; i + (1 << j) - 1 <= N; ++i)
            rmq_max[i][j] = max(rmq_max[i][j - 1], rmq_max[i + (1 << (j - 1))][j - 1]);
}

int query_max(int l, int r) {
    int k = lg2[r - l + 1];
    int dif = (r - l + 1) - (1 << k);
    return max(rmq_max[l][k], rmq_max[l + dif][k]);
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> N;
    for(int i = 1; i <= N; ++i)
        cin >> a[i].first >> a[i].second;
    sort(a + 1, a + N + 1);
    for(int i = 1; i <= N; ++i)
        pref_sum[i] = pref_sum[i - 1] + a[i].second;
    compute_lg2();
    compute_rmq();
    for(int i = 1; i <= N; ++i) {
        int best = a[i].first + query_max(i, N) - pref_sum[i - 1];
        ans = max(ans, best);
    }
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...