Submission #440251

# Submission time Handle Problem Language Result Execution time Memory
440251 2021-07-01T20:15:33 Z BaraaArmoush Distributing Candies (IOI21_candies) C++17
29 / 100
936 ms 21736 KB
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define left (node << 1)
#define right (node << 1 | 1)

using namespace std;

typedef long long ll;

const int N = 200200;
const int M = 4 * N;

int n;
int A[N];
int C[N];
ll P[N];
ll tree[M];
int lazy1[M];
ll lazy2[M];

void Merge(int node) {
    tree[node] = tree[left] + tree[right];
}

void push_lazy(int node, int l, int r) {
    if (lazy1[node]) {
        tree[node] = (lazy1[node] == 1 ? P[r + 1] - P[l] : 0);
        if (l < r) {
            lazy1[left] = lazy1[right] = lazy1[node];
            lazy2[left] = lazy2[right] = 0;
        }
        lazy1[node] = 0;
    }

    if (lazy2[node]) {
        tree[node] += (r - l + 1) * lazy2[node];
        if (l < r) {
            lazy2[left] += lazy2[node];
            lazy2[right] += lazy2[node];
        }
        lazy2[node] = 0;
    }
}

void update1(int i, int j, int x, int node = 1, int l = 0, int r = n - 1) {
    push_lazy(node, l, r);

    if (i > j || l > r) {
        return;
    }

    if (j < l || r < i) {
        return;
    }

    if (i <= l && r <= j) {
        lazy1[node] = x;
        lazy2[node] = 0;
        return push_lazy(node, l, r);
    }

    update1(i, j, x, left, l, mid);
    update1(i, j, x, right, mid + 1, r);

    Merge(node);
}

void update2(int i, int j, ll x, int node = 1, int l = 0, int r = n - 1) {
    push_lazy(node, l, r);

    if (i > j || l > r) {
        return;
    }

    if (j < l || r < i) {
        return;
    }

    if (i <= l && r <= j) {
        lazy2[node] = x;
        return push_lazy(node, l, r);
    }

    update2(i, j, x, left, l, mid);
    update2(i, j, x, right, mid + 1, r);

    Merge(node);
}

int query(int i, int node = 1, int l = 0, int r = n - 1) {
    push_lazy(node, l, r);

    if (i < l || r < i) {
        return -1;
    }

    if (l == r) {
        return tree[node];
    }

    return i <= mid ? query(i, left, l, mid) : query(i, right, mid + 1, r);
}

void build(int node = 1, int l = 0, int r = n - 1) {
    push_lazy(node, l, r);

    if (l == r) {
        return void(A[l] = tree[node]);
    }

    build(left, l, mid);
    build(right, mid + 1, r);
}

#undef mid
#undef left
#undef right

int sign(int x) {
    return x > 0 ? +1 : x < 0 ? -1 : 0;
}

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

    vector<int> b(n);
    iota(b.begin(), b.end(), 0);
    sort(b.begin(), b.end(), [&c](int i, int j) {
        return c[i] < c[j];
    });

    for (int i = 0; i < n; i++) {
        C[i] = c[i];
    }

    sort(C, C + n);

    for (int i = 0; i < n; i++) {
        P[i + 1] = P[i] + C[i];
    }

    for (int j = 0; j < q; j++) {
        int can = 0;
        int low = 1, high = n;

        while (low <= high) {
            int mid = (low + high) / 2;
            int x = query(mid - 1);

            if (v[j] > 0) {
                if (x + v[j] < C[mid - 1]) {
                    high = mid - 1;
                } else {
                    can = mid;
                    low = mid + 1;
                }
            } else {
                if (x + v[j] > 0) {
                    high = mid - 1;
                } else {
                    can = mid;
                    low = mid + 1;
                }
            }
        }

        update1(0, can - 1, sign(v[j]));
        update2(can, n - 1, v[j]);
    }

    vector<int> a(n);

    build();

    for (int i = 0; i < n; i++) {
        a[b[i]] = A[i];
    }

    return a;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 607 ms 21424 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 361 ms 5160 KB Output is correct
4 Correct 105 ms 16836 KB Output is correct
5 Correct 932 ms 21496 KB Output is correct
6 Correct 919 ms 21736 KB Output is correct
7 Correct 936 ms 21464 KB Output is correct
8 Correct 932 ms 21572 KB Output is correct
9 Correct 519 ms 21520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -