답안 #623236

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
623236 2022-08-05T11:03:49 Z cheissmart 사탕 분배 (IOI21_candies) C++17
3 / 100
5000 ms 21412 KB
#include "candies.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 2e5 + 7;
const ll oo = 1e18;

int c[N], n, q;
V<pi> ev[N];

ll a[N];
void add(int l, int r, int x) {
    for(int i = l; i < r; i++) {
        a[i] += x;
    }
}
ll qmax(int l, int r) {
    ll mx = -oo;
    for(int i = l; i < r; i++) {
        mx = max(mx, a[i]);
    }
    return mx;
}
ll qmin(int l, int r) {
    ll mn = oo;
    for(int i = l; i < r; i++) {
        mn = min(mn, a[i]);
    }
    return mn;
}

vi distribute_candies(vi _c, vi _l, vi _r, vi _v) {
    n = SZ(_c), q = SZ(_l);
    for(int i = 0; i < n; i++)
        c[i] = _c[i];

    vi ans(n);

    for(int i = 1; i <= q; i++) {
        int l = _l[i - 1], r = _r[i - 1], v = _v[i - 1];
        ev[l].EB(i, v);
        ev[r + 1].EB(i, -v);
    }
    for(int i = 0; i < n; i++) {
        for(auto [pos, val]:ev[i])
            add(pos, q + 1, val);
        ll final = qmax(q, q + 1);
        ll mx = qmax(0, q + 1);
        ll mn = qmin(0, q + 1);
        if(mx - mn <= c[i]) {
            ans[i] = final - mn;
        } else {
            int lb = 0, rb = q;
            while(lb <= rb) {
                int mb = (lb + rb) / 2;
                ll mx = qmax(mb, q + 1);
                ll mn = qmin(mb, q + 1);
                if(mx - mn <= c[i])
                    rb = mb - 1;
                else
                    lb = mb + 1;
            }
            ll mx = qmax(rb, q + 1);
            ll mn = qmin(rb, q + 1);
            assert(mx - mn > c[i]);
            ll val = qmax(rb, rb + 1);
            if(val == mx) {
                ans[i] = final - mn;
            } else {
                ans[i] = final - (mx - c[i]);
            }
        }
    }

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5008 KB Output is correct
3 Correct 5 ms 5076 KB Output is correct
4 Correct 6 ms 5076 KB Output is correct
5 Correct 29 ms 5160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5024 ms 21412 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Execution timed out 5031 ms 15680 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Execution timed out 5012 ms 14560 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5008 KB Output is correct
3 Correct 5 ms 5076 KB Output is correct
4 Correct 6 ms 5076 KB Output is correct
5 Correct 29 ms 5160 KB Output is correct
6 Execution timed out 5024 ms 21412 KB Time limit exceeded
7 Halted 0 ms 0 KB -