Submission #714514

# Submission time Handle Problem Language Result Execution time Memory
714514 2023-03-24T21:19:16 Z four_specks Sails (IOI07_sails) C++17
40 / 100
28 ms 4628 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<int> 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);
    }

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

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

    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 1492 KB Output is correct
2 Correct 2 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1492 KB Output is correct
2 Correct 2 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1492 KB Output is correct
2 Correct 2 ms 1472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1492 KB Output is correct
2 Correct 3 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 1488 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1608 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 2836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 2892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 3544 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 4628 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 4440 KB Output isn't correct
2 Halted 0 ms 0 KB -