Submission #442286

# Submission time Handle Problem Language Result Execution time Memory
442286 2021-07-07T11:43:23 Z blue Distributing Candies (IOI21_candies) C++17
3 / 100
5000 ms 48952 KB
#include "candies.h"
#include <vector>
#include <iostream>
using namespace std;

const long long INF = 3'000'000'000;

struct segtree //min, max, range add
{
    int l;
    int r;

    long long mn = 0;
    long long mx = 0;
    long long lp = 0;

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

    segtree()
    {
        ;
    }

    segtree(int L, int R)
    {
        l = L;
        r = R;
        if(l == r) return;
        int m = (l+r+10)/2 - 5;
        left = new segtree(l, m);
        right = new segtree(m+1, r);
    }

    long long rangemin(int L, int R)
    {
        if(R < l || r < L) return INF;
        else if(L <= l && r <= R) return mn;
        else return lp + min(left->rangemin(L, R), right->rangemin(L, R));
    }

    long long rangemax(int L, int R)
    {
        if(R < l || r < L) return -INF;
        else if(L <= l && r <= R) return mx;
        else return lp + max(left->rangemax(L, R), right->rangemax(L, R));
    }

    void add(int L, int R, long long V)
    {
        if(R < l || r < L) return;

        // cerr << "adding to " << l << ' ' << r << ": " << L << ' ' << R << ' ' << V << '\n';

        if(L <= l && r <= R)
        {
            mn += V;
            mx += V;
            lp += V;
        }
        else
        {
            left->add(L, R, V);
            right->add(L, R, V);
            mn = lp + min(left->mn, right->mn);
            mx = lp + max(left->mx, right->mx);
        }
    }
};

vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v)
{
    int N = c.size();
    int Q = l.size();

    segtree S(-3, Q-1);

    S.add(-3, -3, -INF);
    S.add(-2, -2, +INF);

    vector<int> insertions[N], removals[N];
    for(int i = 0; i < Q; i++)
    {
        insertions[ l[i] ].push_back(i);
        removals[ r[i] ].push_back(i);
    }

    vector<int> res(N, 0);
    long long curr_upd_sum = 0;

    // for(int j = -1; j <= Q-1; j++) cerr << S.rangemax(j, j) << ' ';
    // cerr << '\n';

    for(int i = 0; i < N; i++)
    {
        // cerr << "i = " << i << '\n';
        for(int o: insertions[i])
        {
            // cerr << "inserting " << o << ": " << l[o] << ' ' << r[o] << ' ' << v[o] << '\n';
            S.add(o, Q-1, +v[o]);
            curr_upd_sum += v[o];

            // cerr << "segment tree: ";
            // for(int j = -1; j <= Q-1; j++) cerr << S.rangemax(j, j) << ' ';
            // cerr << '\n';
        }

        long long lower_bound = 0;

        // cerr << S.rangemax(-1, Q-1) << ' ' << S.rangemin(-1, Q-1) << '\n';


        int last_good;
        for(last_good = Q-1; last_good >= 0; last_good--)
        {
            if(S.rangemax(last_good-1, Q-1) - S.rangemin(last_good-1, Q-1) > (long long)c[i])
                break;
        }
        // cerr << "last_good = " << last_good << '\n';

        if(S.rangemax(last_good-1, last_good-1) < S.rangemax(last_good, last_good))
            lower_bound = S.rangemax(last_good, Q-1) - (long long)c[i];
        else
            lower_bound = S.rangemin(last_good, Q-1);

        // cerr << "curr_upd_sum = " << curr_upd_sum << ", lower_bound = " << lower_bound << '\n';

        res[i] = (curr_upd_sum - lower_bound);

        for(int o: removals[i])
        {
            // cerr << "removing " << o << '\n';
            S.add(o, Q-1, -v[o]);
            curr_upd_sum -= v[o];
        }

        // cerr << "\n\n";
    }

    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 10 ms 644 KB Output is correct
5 Correct 76 ms 788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5091 ms 48952 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Execution timed out 5085 ms 35928 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Execution timed out 5090 ms 34460 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 10 ms 644 KB Output is correct
5 Correct 76 ms 788 KB Output is correct
6 Execution timed out 5091 ms 48952 KB Time limit exceeded
7 Halted 0 ms 0 KB -