萌新求助
查看原帖
萌新求助
361308
Stinger楼主2021/3/19 20:57

这份代码本机#2 RE,交上去莫名其妙A了。

本机Win10。

#include <cstdio>
#include <cassert>
#define int long long
//#pragma comment(linker, "/STACK:102400000,102400000") 


int In[1000005], Out[1000005], Dep[1000005], c[1000005], c2[1000005];
int head[1000005], val[1000005], tot, cnt, n, m;
struct Edge {
	int to, nxt;
} e[2000005];
inline void AddEdge(const int u, const int v) {
	e[++ tot].to = v, e[tot].nxt = head[u], head[u] = tot;
}
inline void update(const int l, const int r, const int d) {
	for (int i(l); i <= n; i += (i & ~i + 1)) c[i] += d;
	for (int i(r + 1); i <= n; i += (i & ~i + 1)) c[i] -= d;
}
inline int query(const int x) {
	int sum(0);
	for (int i(x); i; i -= (i & ~i + 1)) sum += c[i];
	return sum;
}
inline void update2(const int l, const int r, const int d) {
	for (int i(l); i <= n; i += (i & ~i + 1)) c2[i] += d;
	for (int i(r + 1); i <= n; i += (i & ~i + 1)) c2[i] -= d;
}
inline int query2(const int x) {
	int sum(0);
	for (int i(x); i; i -= (i & ~i + 1)) sum += c2[i];
	return sum;
}
void dfs(const int u, const int fa) {
//	printf("%lld %lld\n", u, fa);
	Dep[u] = Dep[fa] + 1;
	In[u] = ++ cnt;
	for (int i(head[u]); i; i = e[i].nxt)
		if (e[i].to != fa) dfs(e[i].to, u);
	update(In[u], Out[u] = cnt, val[u]);
}

signed main() {
//	freopen("P3178_2.in", "r", stdin);
//	freopen("output.out", "w", stdout);
	scanf("%lld%lld", &n, &m);
	for (int i(1); i <= n; ++ i) scanf("%lld", val + i);
	for (int i(1); i < n; ++ i) {
		int u, v;
		scanf("%lld%lld", &u, &v);
		AddEdge(u, v), AddEdge(v, u);
	}
	dfs(1, -1);
//	puts("233");
	while (m --) {
		int op, x;
		scanf("%lld%lld", &op, &x);
		if (op == 1) {
			int d;
			scanf("%lld", &d);
			update(In[x], Out[x], d);
		} else if (op == 2) {
			int d;
			scanf("%lld", &d);
			update(In[x], Out[x], (1 - Dep[x]) * d);
			update2(In[x], Out[x], d);
		} else printf("%lld\n", query(In[x]) + Dep[x] * query2(In[x]));
	}
}
2021/3/19 20:57
加载中...