Submission #1366910

#TimeUsernameProblemLanguageResultExecution timeMemory
1366910gelastropodCollecting Stamps 5 (JOI26_stamps)C++20
18 / 100
1857 ms81064 KiB
#pragma GCC optimize("O3,inline")
#include <bits/stdc++.h>
using namespace std;
#define int long long

int D;
vector<int> T;
vector<vector<int>> adjlist;

struct node {
	int s, e, m, v, lazy;
	node* l, * r;

	node(int _s, int _e) : s(_s), e(_e), m((_s + _e) / 2), v(0), lazy(0) {
		if (s != e)
			l = new node(s, m),
			r = new node(m + 1, e);
	}

	void prop() {
		if (s == e || lazy == 0) return;
		l->v += lazy;
		r->v += lazy;
		l->lazy += lazy;
		r->lazy += lazy;
		lazy = 0;
	}

	void upd(int a, int b, int x) {
		if (b < s || a > e) return;
		if (a <= s && b >= e) {
			v += x;
			lazy += x;
			return;
		}
		prop();
		l->upd(a, b, x);
		r->upd(a, b, x);
		v = max(l->v, r->v);
	}

	int qry(int a, int b) {
		if (b < s || a > e) return -1;
		if (a <= s && b >= e) return v;
		prop();
		return max(l->qry(a, b), r->qry(a, b));
	}
} *root;

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int N, a, b; cin >> N >> D;
	adjlist.resize(N, vector<int>());
	for (int i = 0; i < N; i++) {
		cin >> a;
		T.push_back(a);
	}
	for (int i = 0; i < N - 1; i++) {
		cin >> a >> b; a--, b--;
		adjlist[a].push_back(b);
		adjlist[b].push_back(a);
	}
	root = new node(0, N - 1);
	for (int i = 0; i < N; i++) root->upd(i, i, i - T[i]);
	for (int i = 0; i < N; i++) {
		int res = 0;
		if (T[i] == 0) {
			res = -1;
		}
		int low = 0, high = i, ans = -1;
		while (low <= high) {
			int x = (low + high) / 2;
			if (root->qry(x, i) >= 0) ans = x, low = x + 1;
			else high = x - 1;
		}
		if (!(ans == -1 || ans < i - D)) res += ans - max(0LL, i - D) + 1;
		low = i, high = N - 1, ans = -1;
		while (low <= high) {
			int x = (low + high) / 2;
			if (root->qry(i, x) >= 0) ans = x, high = x - 1;
			else low = x + 1;
		}
		if (!(ans == -1 || ans > i + D)) res += min(N - 1, i + D) - ans + 1;
		cout << res << '\n';
		root->upd(0, i, 1);
		root->upd(i + 1, N - 1, -1);
	}
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...