Submission #440248

# Submission time Handle Problem Language Result Execution time Memory
440248 2021-07-01T20:12:48 Z BaraaArmoush Distributing Candies (IOI21_candies) C++17
0 / 100
613 ms 15080 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;

#define sim template <class c
#define ris return *this
#define dor > debug& operator<<
#define eni(x) sim > typename enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) {
    return rge<c>{i, j};
}
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
    ~debug() {
        cerr << endl;
    }
    eni(!=) cerr << boolalpha << i;
    ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
}
#else
    sim dor(const c&) {
        ris;
    }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

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

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

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) {
    // debug() << imie(c);
    // debug() << imie(l);
    // debug() << imie(r);
    // debug() << imie(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];
    }

    // debug() << imie(c);
    // debug() << imie(b);
    // debug() << imie(range(C, C + n));

    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;
                }
            }
        }

        // debug() << imie(can);

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

        // for (int i = 0; i < n; i++) {
        //     debug() << imie(i) imie(query(i));
        // }
    }

    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 613 ms 15080 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 366 ms 5216 KB Output is correct
4 Incorrect 105 ms 10400 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 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 -