답안 #939511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939511 2024-03-06T12:39:02 Z haxorman 사탕 분배 (IOI21_candies) C++17
3 / 100
5000 ms 44732 KB
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define ll long long

const int mxN = 2e5 + 7;
const int SZ = exp2(ceil(log2(mxN)));

int n, q;
ll lazy[4*mxN];
pair<ll,int> seg[4*mxN][2];
vector<pair<int,int>> que[mxN][2];

void push(int ind, int l, int r, int cnt = 1) {
    seg[ind][0].f += lazy[ind];
    seg[ind][1].f += lazy[ind];

    if (l != r) {
        lazy[2*ind] += lazy[ind];
        lazy[2*ind+1] += lazy[ind];

        if (cnt) {
            int mid = (l+r)/2;
            push(2*ind,l,mid,cnt-1);
            push(2*ind+1,mid+1,r,cnt-1);
        }
    }
    lazy[ind] = 0;
}

void update(int lo, int hi, ll inc, int ind = 1, int l = 0, int r = SZ - 1) {
    push(ind,l,r);
    if (lo > r || l > hi) {
        return;
    }

    if (lo <= l && r <= hi) {
        lazy[ind] += inc;
        push(ind,l,r);
        return;
    }
    
    int mid = (l+r)/2;
    update(lo,hi,inc,2*ind,l,mid);
    update(lo,hi,inc,2*ind+1,mid+1,r);
    
    if (seg[2*ind][0].f < seg[2*ind+1][0].f) seg[ind][0] = seg[2*ind][0];
    else seg[ind][0] = seg[2*ind+1][0];

    if (seg[2*ind][1].f > seg[2*ind+1][1].f) seg[ind][1] = seg[2*ind][1];
    else seg[ind][1] = seg[2*ind+1][1];
}

// {min, max}
pair<pair<ll,int>,pair<ll,int>>
query(int lo, int hi, int ind = 1, int l = 0, int r = SZ - 1) {
    push(ind,l,r);
    if (lo > r || l > hi) {
        return {{LLONG_MAX,0}, {LLONG_MIN,0}};
    }

    if (lo <= l && r <= hi) {
        return {seg[ind][0], seg[ind][1]};
    }

    int mid = (l+r)/2;
    auto a = query(lo,hi,2*ind,l,mid), b = query(lo,hi,2*ind+1,mid+1,r);
    auto res = make_pair(min(a.f,b.f), max(a.s,b.s));
    return res;
}

vector<int> distribute_candies(vector<int> c, vector<int> l,
                                    vector<int> r, vector<int> v) {
    n = c.size(), q = v.size()+1;

    seg[1+SZ][0] = seg[1+SZ][1] = {0, 1};
    for (int i = 2; i <= q; ++i) {
        que[l[i-2]][0].push_back({v[i-2], i});
        que[r[i-2]][1].push_back({v[i-2], i});

        seg[i+SZ][0] = seg[i+SZ][1] = {0, i};
    }
    
    for (int ind = SZ-1; ind >= 0; --ind) {
        if (seg[2*ind][0].f < seg[2*ind+1][0].f) seg[ind][0] = seg[2*ind][0];
        else seg[ind][0] = seg[2*ind+1][0];

        if (seg[2*ind][1].f > seg[2*ind+1][1].f) seg[ind][1] = seg[2*ind][1];
        else seg[ind][1] = seg[2*ind+1][1];
    }
    
    vector<int> ans(n);
    for (int i = 0; i < n; ++i) {
        for (auto [val, t] : que[i][0]) {
            update(t, q, val);
        }
        update(1,q,-c[i]);
        
        /*
        for (int j = 0; j <= q; ++j) {
            cout << query(j,j).f.f << ' ';
        }
        cout << "\n";
        */
    
        int lb = 0, rb = q;
        ll res = 0;
        while (lb <= rb) {
            int mb = (lb+rb)/2;
            auto cur = query(mb,q);

            if (cur.s.f - cur.f.f >= (ll) c[i]) {
                lb = mb + 1;

                if (cur.s.s > cur.f.s) {
                    res = c[i] + query(q,q).f.f - query(cur.s.s,cur.s.s).f.f;
                }
                else {
                    res = query(q,q).f.f - query(cur.f.s,cur.f.s).f.f;
                }
            }
            else {
                rb = mb - 1;
            }
        }
        ans[i] = res;
        
        update(1,q,c[i]);
        for (auto [val, t] : que[i][1]) {
            update(t, q, -val);
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 9 ms 21172 KB Output is correct
4 Correct 10 ms 21084 KB Output is correct
5 Correct 45 ms 21340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5048 ms 44732 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 21084 KB Output is correct
2 Correct 675 ms 40380 KB Output is correct
3 Correct 2885 ms 24724 KB Output is correct
4 Execution timed out 5047 ms 44632 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21080 KB Output is correct
2 Correct 11 ms 21084 KB Output is correct
3 Correct 574 ms 40644 KB Output is correct
4 Correct 3408 ms 24852 KB Output is correct
5 Execution timed out 5059 ms 43788 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 9 ms 21172 KB Output is correct
4 Correct 10 ms 21084 KB Output is correct
5 Correct 45 ms 21340 KB Output is correct
6 Execution timed out 5048 ms 44732 KB Time limit exceeded
7 Halted 0 ms 0 KB -