Submission #467530

# Submission time Handle Problem Language Result Execution time Memory
467530 2021-08-23T12:38:13 Z oliversommer Distributing Candies (IOI21_candies) C++17
0 / 100
494 ms 16732 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
			if (tree[2 * i].lazy > 0) {
				tree[2 * i].p = min(tree[2 * i].max, tree[2 * i].p + tree[2 * i].lazy);
			} else {
				tree[2 * i].p = max(0, tree[2 * i].p + tree[2 * i].lazy);
			}
            tree[2 * i].lazy = tree[i].lazy;
			
            if (tree[2 * i + 1].lazy > 0) {
				tree[2 * i + 1].p = min(tree[2 * i + 1].max, tree[2 * i + 1].p + tree[2 * i + 1].lazy);
			} else {
				tree[2 * i + 1].p = max(0, tree[2 * i + 1].p + tree[2 * i + 1].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);
}

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 >= sl && r <= sr) {
		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);
}

void final(int i, int l, int r, vector<int>& s) {
	lazy_update(i, l, r);

	if (l == r) {  // is leaf
		s[l] = tree[i].p;
		return;
	}

	int m = l + (r - l) / 2;
	final(2 * i, l, m, s);
	final(2 * i + 1, m + 1, r, s);
}

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 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 494 ms 16732 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 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -