Submission #444390

# Submission time Handle Problem Language Result Execution time Memory
444390 2021-07-14T00:21:04 Z JvThunder Distributing Candies (IOI21_candies) C++17
0 / 100
1204 ms 212576 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

const int MAXN = 1000005;
const int MAXQ = 1000005;
const ll INF = (ll)(1e18);

struct SegmentTree 
{
    int n;
    ll vmin[(MAXN + MAXQ) * 4] = {0};
    ll vmax[(MAXN + MAXQ) * 4] = {0};
    ll lazy[(MAXN + MAXQ) * 4] = {0};

    void init(int _n) { n = _n; }

    void lazy_update(int node, int l, int r)
    {
        vmin[node] += lazy[node]; vmax[node] += lazy[node];
        if(l==r) return;
        lazy[2*node] += lazy[node];
        lazy[2*node+1] += lazy[node];
        lazy[node] = 0;
    }

    void update(int node, int l, int r, int L, int R, int add)
    {
        lazy_update(node, l, r);
        if (l > R || r < L) return;
        if (L <= l && r <= R) {
            lazy[node] += add;
            lazy_update(node, l, r);
            return;
        }
        int mid = (l+r)/2;
        update(2*node, l, mid, L, R, add);
        update(2*node+1, mid+1, r, L, R, add);
        vmin[node] = min(vmin[2*node], vmin[2*node+1]);
        vmax[node] = max(vmax[2*node], vmax[2*node+1]);
    }

    ll get(int node, int l, int r, int lb) 
    {
        lazy_update(node, l, r);
        if (l==r) return vmin[node];
        int mid = (l+r)/2;
        if (vmax[2*node+1]+lazy[2*node+1] >= lb)
            return get(2*node+1, mid + 1, r, lb);
        return min(vmin[2*node+1]+lazy[2*node+1], get(2*node, l, mid, lb));
    }

    void add_range(int L, int R, int add) { update(1, 0, n-1, L, R, add); }

    ll min_suffix(int lb) 
    {
        if (vmax[1]<lb) return -INF;
        return min(0LL, get(1, 0, n-1, lb));
    }
} T;

vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V) 
{
    int n = C.size(); int q = L.size();
    vector<int> A(n);
    
    vector<vector<int>> begin_at(n,vector<int>()), end_at(n,vector<int>());
    for(int i=0;i<q;i++) 
    {
        begin_at[L[i]].push_back(i);
        end_at[R[i]].push_back(i);
    }

    T.init(q+1);
    vector<int> final_A(n);
    for(int i=0;i<n;i++)
    {
        if (i>0) 
        {
            T.add_range(0,0,-A[i-1]);
            for(int j:end_at[i-1]) T.add_range(0,1+j,-V[j]);
        }
        T.add_range(0,0,A[i]);
        for(int j:begin_at[i]) T.add_range(0,1+j,V[j]);

        int l = 1, r = C[i]+1;
        while (l<=r) 
        {
            int mid = (l+r)/2;
            ll smin = T.min_suffix(mid);
            if (-smin+mid>C[i]) r = mid-1;
            else l = mid+1;
        }
        final_A[i] = r;
    }

    return final_A;
}
# Verdict Execution time Memory Grader output
1 Correct 83 ms 188140 KB Output is correct
2 Incorrect 83 ms 188100 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1204 ms 212576 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 82 ms 188128 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 85 ms 188028 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 83 ms 188140 KB Output is correct
2 Incorrect 83 ms 188100 KB Output isn't correct
3 Halted 0 ms 0 KB -