Submission #1309905

#TimeUsernameProblemLanguageResultExecution timeMemory
1309905buzdiSails (IOI07_sails)C++17
55 / 100
71 ms9120 KiB
#include <bits/stdc++.h>
#define ll long long
#define int long long

using namespace std;

const int NMAX = 1e5 + 10;

ll answer;
int n;
int h[NMAX + 1], k[NMAX + 1];
set<pair<int, int>> s;
int mars[NMAX + 5];

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> h[i] >> k[i];
    }

    for(int i = 1; i <= n; i++) {
        s.insert(make_pair(h[i], i));
    }

    while(!s.empty()) {
        auto it = prev(s.end());
        auto [curent_h, pos] = *it;

        int next_h = max(curent_h - k[pos] + 1, 1LL);
        mars[next_h]++;
        mars[curent_h + 1]--;

        k[pos] -= curent_h - next_h + 1;
        curent_h = next_h - 1;

        if(!k[pos]) {
            s.erase(it);
        }

        while(!s.empty() && curent_h > 0) {
            auto it = s.lower_bound(make_pair(curent_h, 0LL));
            if(it == s.end()) {
                break;
            }
            int pos = it->second;

            int next_h = max(curent_h - k[pos] + 1, 1LL);
            mars[next_h]++;
            mars[curent_h + 1]--;

            k[pos] -= curent_h - next_h + 1;
            curent_h = next_h - 1;

            if(!k[pos]) {
                s.erase(it);
            }
        }
    }

    for(int i = 1; i <= NMAX; i++) {
        mars[i] += mars[i - 1];
    }
    for(int i = 1; i <= NMAX; i++) {
        answer += (ll) mars[i] * (mars[i] - 1) / 2;
    }
    cout << answer << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...