Submission #467573

# Submission time Handle Problem Language Result Execution time Memory
467573 2021-08-23T17:56:36 Z oliversommer Distributing Candies (IOI21_candies) C++17
0 / 100
200 ms 34428 KB
#include "candies.h"

#include <algorithm>
#include <climits>
#include <vector>

using namespace std;

struct Node {
	int p;
	int lazy;
	int max = INT_MAX;
};

vector<Node> tree;

void lazy_update(int i, int l, int r) {
	if (tree[i].lazy != 0) {
		if (tree[i].lazy > 0) {
			tree[i].p = min(tree[i].max, tree[i].p + tree[i].lazy);
		} else {
			tree[i].p = max(0, tree[i].p + tree[i].lazy);
		}

		if (l != r) {  // has children
			tree[2 * i].lazy += tree[i].lazy;
			tree[2 * i + 1].lazy += tree[i].lazy;
		}

		tree[i].lazy = 0;
	}
}

void set_max(int i, int l, int r, int sl, int sr, int max) {
	if (l > sr || r < sl) {
		return;
	}
	if (l >= sl && r <= sr) {
		tree[i].max = max;
		return;
	}

	int m = (l + r) / 2;
	set_max(2 * i, l, m, sl, sr, max);
	set_max(2 * i + 1, m + 1, r, sl, sr, max);
	tree[i].max = min(tree[2 * i].max, tree[2 * i + 1].max);
}

void update(int i, int l, int r, int sl, int sr, int v) {
	lazy_update(i, l, r);

	if (l > sr || r < sl) {
		return;
	}
	if (l == r && v > tree[i].max) {
        tree[i].p = tree[i].max;
		return;
    }
	if (l >= sl && r <= sr && v < tree[i].max) {
		tree[i].lazy = v;
		lazy_update(i, l, r);
		return;
	}

	int m = (l + r) / 2;
	update(2 * i, l, m, sl, sr, v);
	update(2 * i + 1, m + 1, r, sl, sr, v);
	tree[i].p = tree[2 * i].p + tree[2 * i + 1].p;
}

int query(int i, int l, int r, int sl, int sr) {
	lazy_update(i, l, r);

	if (l > sr || r < sl) {
		return 0;
	}
	if (l >= sl && r <= sr) {
		return tree[i].p;
	}

	int m = (l + r) / 2;
	return query(2 * i, l, m, sl, sr) + query(2 * i + 1, m + 1, r, sl, sr);
}

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> s(n);

	tree = vector<Node>(4 * n + 5);

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

	for (int j = 0; j < q; ++j) {
		update(1, 1, n, l[j] + 1, r[j] + 1, v[j]);
	}

	for (int i = 0; i < n; ++i) {
		s[i] = query(1, 1, n, i + 1, i + 1);
	}

	return s;
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 276 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 200 ms 34428 KB Execution killed with signal 11
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 Incorrect 1 ms 280 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 276 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -