Submission #456441

# Submission time Handle Problem Language Result Execution time Memory
456441 2021-08-06T17:41:30 Z benedict0724 Distributing Candies (IOI21_candies) C++17
0 / 100
670 ms 41896 KB
#include "candies.h"

#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 1e16;

vector<pair<ll, ll>> tree;
vector<ll> lazy;
vector<int> query[200002];
vector<int> ans;

void propagate(int i, int l, int r)
{
    tree[i].first += lazy[i];
    tree[i].second += lazy[i];
    if(l != r)
    {
        lazy[i*2] += lazy[i];
        lazy[i*2+1] += lazy[i];
    }
    lazy[i] = 0;
}

void update(int i, int l, int r, int s, int e, ll v)
{
    propagate(i, l, r);
    if(e < l || r < s) return;
    if(s <= l && r <= e)
    {
        lazy[i] += v;
        propagate(i, l, r);
        return;
    }
    int m = (l + r)/2;
    update(i*2, l, m, s, e, v);
    update(i*2+1, m+1, r, s, e, v);

    tree[i].first = max(tree[i*2].first, tree[i*2+1].first);
    tree[i].second = min(tree[i*2].second, tree[i*2+1].second);
}

ll sum = 0;

void solve(int i, int l, int r, ll C, int k, ll Ma, ll mi)
{
    propagate(i, l, r);
    if(l == r)
    {
        if(tree[i].first > Ma) ans[k] = sum - mi;
        else ans[k] = C - Ma + sum;
        return;
    }
    int m = (l + r)/2;
    propagate(i*2, l, m);
    propagate(i*2+1, m+1, r);
    if(max(tree[i*2+1].first, Ma) - min(tree[i*2+1].second, mi) <= C)
    {
        Ma = max(tree[i*2+1].first, Ma), mi = min(tree[i*2+1].second, mi);
        solve(i*2, l, m, C, k, Ma, mi);
    }
    else solve(i*2+1, m+1, r, C, k, Ma, mi);
}

std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
                                    std::vector<int> r, std::vector<int> v) {
    int n = c.size();
    int q = l.size();
    tree.resize(4*q+4);
    lazy.resize(4*q+4);
    ans.resize(n);
    for(int i=1;i<=q;i++)
    {
        query[l[i-1]].push_back(i);
        query[r[i-1]+1].push_back(-i);
    }

    for(int i=0;i<n;i++)
    {
        for(int j : query[i])
        {
            if(j > 0) { update(1, 0, q, j, q, v[j-1]); sum += v[j-1]; }
            else { update(1, 0, q, -j, q, -v[-j-1]); sum -= v[-j-1]; }
        }
        solve(1, 0, q, c[i], i, -INF, INF);
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Incorrect 3 ms 4968 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 670 ms 41896 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Incorrect 3 ms 4968 KB Output isn't correct
3 Halted 0 ms 0 KB -