比赛 期末考试1 评测结果 AAAAAAAAAA
题目名称 Output Only 最终得分 100
用户昵称 xuyuqing 运行时间 1.245 s
代码语言 C++ 内存使用 15.75 MiB
提交时间 2026-02-08 09:24:17
显示代码纯文本

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

class fastIn {
	private:
	
	char buf [1 << 20];
	char *p1 = buf;
	char *p2 = buf;
	
	inline char getch ();
	
	public:
	
	template <typename NumT>
	void read (NumT &num);
	
	template <typename NumT>
	fastIn& operator >> (NumT &num);
};

inline char fastIn::getch () {
	if (p1 == p2) {
		p1 = buf;
		p2 = buf + fread (buf, 1, 1 << 20, stdin);
		if (p1 == p2) {
			return EOF;
		}
	}
	return *p1++;
}

template <typename NumT>
void fastIn::read (NumT &num) {
	char ch = getch ();
	NumT op = 1;
	num = 0;
	
	while (ch < '0' || '9' < ch) {
		op = ((ch == '-') ? -1 : 1);
		ch = getch ();
	}
	while ('0' <= ch && ch <= '9') {
		num *= 10;
		num += (ch - '0');
		ch = getch ();
	}
	
	num *= op;
}

template <typename NumT>
fastIn& fastIn::operator >> (NumT &num) {
	read (num);
	return *this;
}

fastIn fin;

const int N = 233233;

int n;
int k;
int q;
int fa[N];
int c[N];
int add[N];
bool flag[N];
vector<int> graph[N];
int res;

void dfs (int now, int dad) {
	fa[now] = dad;
	add[now] = add[dad];
	int num = c[now] + add[now];
	num %= k;
	num = k - num;
	num %= k;
	add[now] += num;

	if (num) {
		flag[now] = true;
		res++;
	}

	for (auto son : graph[now]) {
		if (son == dad) {
			continue;
		}
		dfs (son, now);
	}
}


void dfs2 (int now, int dad) {
	if (flag[now] == true) {
		res--;
		flag[now] = false;
	}

	add[now] = add[dad];
	int num = c[now] + add[now];
	num %= k;
	num = k - num;
	num %= k;
	add[now] += num;

	if (num) {
		flag[now] = true;
		res++;
	}

	for (auto son : graph[now]) {
		if (son == dad) {
			continue;
		}
		dfs2 (son, now);
	}
}

int main () {

	freopen ("tioj_outplay.in", "r", stdin);
	freopen ("tioj_outplay.out", "w", stdout);

	fin >> n >> k >> q;
	for (int i = 1; i <= n; i++) {
		fin >> c[i];
	}
	for (int i = 1; i < n; i++) {
		int u, v;
		fin >> u >> v;
		graph[u].push_back(v);
		graph[v].push_back(u);
	}

	dfs (1, 0);

	for (int i = 1; i <= q; i++) {
		int w, x;
		fin >> w >> x;

		c[w] = x;
		dfs2 (w, fa[w]);
		printf ("%d\n", res);
	}
	
	return 0;
}