我本地测第一个点没问题,为什么luogu上RE了?
查看原帖
我本地测第一个点没问题,为什么luogu上RE了?
736787
hhhqx楼主2024/12/24 17:21

不开 O2 返回 Received signal 4: Illegal Instruction.

开 O2 返回 Received signal 11: Segmentation fault with invalid memory reference.

差分做法

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using PII = pair<int, int>;

const int MAXN = 3e5 + 3;

int n, m, w[MAXN], ans[MAXN];
vector<int> eg[MAXN];
vector<int> ins[MAXN], era[MAXN], _ins[MAXN], _era[MAXN];

int sz[MAXN], son[MAXN], dep[MAXN], top[MAXN], fa[MAXN];

int m_dfs1(int x, int dad){
	sz[x] = 1, son[x] = 0, dep[x] = dep[dad] + 1, fa[x] = dad;
	for(int nxt : eg[x]){
		if(nxt == dad) continue;
		m_dfs1(nxt, x), sz[x] += sz[nxt];
		if(!son[x] || sz[son[x]] < sz[nxt]) son[x] = nxt;
	}
}
void m_dfs2(int x, int ttoopp){
	top[x] = ttoopp;
	if(son[x]) m_dfs2(son[x], ttoopp);
	for(int nxt : eg[x]){
		if(nxt == fa[x] || nxt == son[x]) continue;
		m_dfs2(nxt, nxt);
	}
}
int LCA(int x, int y){
	while(top[x] != top[y]){
		if(dep[top[x]] > dep[top[y]]) swap(x, y);
		y = fa[top[y]];
	}
	return (dep[x] > dep[y] ? y : x);
}

int mp[MAXN * 6], _mp[MAXN * 6], P = MAXN * 3;

void Insert(int x){
	for(int y : ins[x]) mp[y + P]++;
	for(int y : era[x]) mp[y + P]--;
	for(int y : _ins[x]) _mp[y + P]++;
	for(int y : _era[x]) _mp[y + P]--;
}

void Solve(int x){
  LL tmp = mp[w[x] + dep[x] + P] + _mp[w[x] - dep[x] + P];
	for(int nxt : eg[x]){
		if(nxt == fa[x]) continue;
		Solve(nxt);
	}
	Insert(x); 
  
	ans[x] = mp[w[x] + dep[x] + P] + _mp[w[x] - dep[x] + P] - tmp;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0);
	//freopen("P1600_1.in", "r", stdin);
/*

dep[S] - dep[p] = w[p]
dep[S] = w[p] + dep[p]

dep[p] + dep[S] - dep[LCA] * 2 = w[p]
dep[S] - dep[LCA] * 2 = w[p] - dep[p]

*/
	cin >> n >> m;
	for(int i = 1, U, V; i < n; i++){
		cin >> U >> V, eg[U].push_back(V), eg[V].push_back(U);
	}
	m_dfs1(1, 0), m_dfs2(1, 1);
	for(int i = 1; i <= n; i++) cin >> w[i];
	for(int i = 1, x, y, lca; i <= m; i++){
		cin >> x >> y, lca = LCA(x, y);
		ins[x].push_back(dep[x]);
		_ins[y].push_back(dep[x] - dep[lca] * 2);
		era[lca].push_back(dep[x]);
		_era[fa[lca]].push_back(dep[x] - dep[lca] * 2);
	}
	Solve(1);
	for(int i = 1; i <= n; i++) cout << ans[i] << " ";
	return 0;
}
2024/12/24 17:21
加载中...