Submission #1274829

#TimeUsernameProblemLanguageResultExecution timeMemory
1274829gustavo_dSails (IOI07_sails)C++20
0 / 100
1096 ms3612 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 1e5;
const int MAXH = 1e5;

ll H[MAXN], qty[MAXN];
ll h[MAXH+1];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int n; cin >> n;
    for (int i=0; i<n; i++) {
        cin >> H[i] >> qty[i];
        for (int j=H[i]-qty[i]+1; j<=H[i]; j++) {
            h[j]++;
        }
    }

    vector<tuple<ll, ll, ll>> freq; freq.emplace_back(1, 1, h[1]);
    for (int i=2; i<=MAXH; i++) {
        auto [l, r, f] = freq.back();
        f *= (r-l+1);
        freq.pop_back();
        f += h[i]; r++;
        while (!freq.empty() and f/(r-l+1) > get<2> (freq.back())) {
            auto [l2, r2, f2] = freq.back();
            freq.pop_back();
            l = l2; f += f2 * (r2-l2+1);
        }
        ll more = f % (r-l+1);
        if (more != 0) freq.emplace_back(l, l+more-1, f / (r-l+1) + 1);
        freq.emplace_back(l+more, r, f / (r-l+1));
    }

    ll ans = 0;
    for (auto [l, r, f] : freq) {
        if (f == 0) continue;
        // cout << l << " até " << r << " com " << f << endl;
        ans += f*(f-1)/2 * (r-l+1);
    }
    cout << ans << '\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...