Submission #483083

# Submission time Handle Problem Language Result Execution time Memory
483083 2021-10-27T16:19:57 Z blue Sails (IOI07_sails) C++17
40 / 100
1000 ms 13636 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const long long INF = 1'000'000'000'000'000'000LL;

long long ans = 0;

struct segtree
{
    int l;
    int r;
    pair<long long, int> v;
    long long lp = 0;

    segtree* left = NULL;
    segtree* right = NULL;

    segtree()
    {
        ;
    }

    segtree(int L, int R)
    {
        l = L;
        r = R;
        v = make_pair(0LL, l);

        if(l == r) return;

        int m = (l+r)/2;
        left = new segtree(l, m);
        right = new segtree(m+1, r);
    }

    void add(int L, int R, long long V)
    {
        if(R < l || r < L) return;
        else if(L <= l && r <= R)
        {
            v.first += V;
            lp += V;
        }
        else
        {
            left->add(L, R, V);
            right->add(L, R, V);
            v = min(left->v, right->v);
            v.first += lp;
        }
    }

    pair<long long, int> rangemin(int L, int R)
    {
        if(R < l || r < L) return make_pair(INF, -1);
        else if(L <= l && r <= R) return v;
        else
        {
            pair<long long, int> ans = min(left->rangemin(L, R), right->rangemin(L, R));
            ans.first += lp;
            return ans;
        }
    }

    void dfs(long long k)
    {
        k += lp;
        if(l == r) ans += k*(k-1)/2;
        else
        {
            left->dfs(k);
            right->dfs(k);
        }
    }
};

const int mx = 100'000;

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

    int N;
    cin >> N;

    vector< pair<int, int> > S(N);
    for(int i = 0; i < N; i++) cin >> S[i].first >> S[i].second;
    sort(S.begin(), S.end());

    segtree ST(1, mx);
    for(int i = 0; i < N; i++)
    {
        int occ_lo = 0;
        int occ_hi = S[i].first + 1;
        // cerr << "\n\n\n";
        // cerr << "i = " << i << '\n';
        // cerr << S[i].first << ' ' << S[i].second << '\n';
        while(S[i].second > 0)
        {
            pair<long long, int> mn = ST.rangemin(occ_lo+1, occ_hi-1);

            int loc = mn.second;
            int upd = min(S[i].second, occ_hi - loc);
            S[i].second -= upd;
            ST.add(loc, loc + upd - 1, +1);

            if(loc == occ_lo+1)
                occ_lo += upd;
            if(loc+upd-1 == occ_hi - 1)
                occ_hi -= upd;

            // cerr << "adding +1 to " << loc << ' ' << loc + upd - 1 << '\n';
        }
    }
    // cerr << "updating done\n";

    ST.dfs(0);

    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12748 KB Output is correct
2 Correct 10 ms 12728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12748 KB Output is correct
2 Correct 10 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12748 KB Output is correct
2 Correct 10 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12748 KB Output is correct
2 Correct 17 ms 12712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 12840 KB Output is correct
2 Correct 16 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1087 ms 12876 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1088 ms 13132 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 1088 ms 13136 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 1081 ms 13508 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1089 ms 13444 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1090 ms 13636 KB Time limit exceeded
2 Halted 0 ms 0 KB -