Submission #714522

# Submission time Handle Problem Language Result Execution time Memory
714522 2023-03-24T21:33:22 Z four_specks Sails (IOI07_sails) C++17
40 / 100
21 ms 3960 KB
#include <bits/stdc++.h>

using namespace std;

namespace
{
    template <typename T>
    struct Fenwick
    {
        Fenwick(int _n) : n(_n), tree(n + 1, 0) {}

        void add(int p, T x)
        {
            for (++p; p <= n; p += p & -p)
                tree[p] += x;
        }

        T sum(int p)
        {
            T ret = 0;
            for (; p; p -= p & -p)
                ret += tree[p];
            return ret;
        }

    private:
        int n;
        vector<T> tree;
    };

} // namespace

void solve()
{
    const int X = 100'000;

    int n;
    cin >> n;

    vector<long> a(n), b(n);
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];

    Fenwick<long> fenw(X);
    for (int i = 0; i < n; i++)
    {
        fenw.add(a[i] - b[i], 1);
        fenw.add(a[i], -1);
    }

    vector<long> v(X);
    for (int x = 0; x < X; x++)
        v[x] = fenw.sum(x + 1);

    stack<pair<long, int>> st;
    for (int i = 0; i < n; i++)
    {
        long cur = v[i];
        int cnt = 1;
        while (!st.empty() && st.top().first <= v[i])
        {
            auto [x, y] = st.top();
            st.pop();

            cur += x * y;
            cnt += y;
        }

        long q = cur / cnt, d = cur % cnt;

        if (d > 0)
            st.emplace(q + 1, d);
        st.emplace(q, cnt - d);
    }

    int m = 0;

    long res = 0;
    while (!st.empty())
    {
        auto [x, y] = st.top();
        st.pop();

        m += y;

        res += y * x * (x - 1) / 2;
    }

    assert(m == n);

    cout << res << '\n';
}

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

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1876 KB Output is correct
2 Correct 2 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1876 KB Output is correct
2 Correct 2 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1876 KB Output is correct
2 Correct 3 ms 1840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1876 KB Output is correct
2 Correct 2 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 1880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 1964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 2748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 3060 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 3960 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 3696 KB Output isn't correct
2 Halted 0 ms 0 KB -