답안 #764863

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
764863 2023-06-24T05:59:37 Z vjudge1 Roadside Advertisements (NOI17_roadsideadverts) C++17
7 / 100
66 ms 16368 KB
#include <bits/stdc++.h>
 
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
 
using namespace std;
using ll = long long;
 
const int N = 2e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 ,   mod = 1e9+7 ;

int P[N] , D[N] , H[N] , B[N] , pos[N];
vector<int> g[N] ,vec;
int a[N] , b[N] , d[N] , c[N] , NW , t[N*4] , t1[N*4] , u[N*4];
int up[N][20] , n , sz[N];
void upd(int v, int tl, int tr , int pos , int val){
	u[v] = 1;
	if(tl == tr){
		t[v] = val;
		t1[v] = val;
		return;
	}
	int tm = (tl+tr) >> 1;
	if(tm >= pos) upd(v*2,tl,tm,pos,val);
	else upd(v*2+1,tm+1,tr,pos,val);
	t[v] = t[v*2]+t[v*2+1];
}
int lca(int a , int b){
	if(D[a] > D[b]) swap(a,b);
	for(int i = 19; i >= 0; i--){
		if(D[a] <= D[up[b][i]]){
			b = up[b][i];
		}
	}
	if(a == b) return a;
	for(int i = 19; i >= 0; i--){
		if(up[a][i] != up[b][i]){
			b = up[b][i];
			a = up[a][i];
		}
	}
	return up[a][0];
}

void dfs(int v , int pr){
	P[v] = pr;
	D[v] = D[pr]+1;
	int mx = 0;
	sz[v] = 1;
	up[v][0] = pr;
	for(int i = 1; i < 20; i++) up[v][i] = up[up[v][i-1]][i-1];
	for(int i : g[v]){
		int to = (v^a[i]^b[i]);
		if(to == pr) continue;
		dfs(to,v);
		d[to] = c[i];
		sz[v] += sz[to];
		if(sz[to] > mx){
			mx = sz[to];
			H[v] = to;
		}
	}
}	
void decompose(int v ,int h){
	B[v] = h;
	pos[v] = ++NW;
	if(H[v]) decompose(H[v],h);
	for(int i : g[v]){
		int to = (v^a[i]^b[i]);
		if(to != P[v] && to != H[v]) decompose(to,to);
	}
}
int getsum(int v ,int tl , int tr , int l , int r , int mx){
	int resmx = min(mx,u[v]);
	if(l <= tl && tr <= r){
		u[v] = 0;
		int num = t[v];
		t[v] = 0;
		vec.push_back(v);
		return num*resmx;
	}
	if(tl > r || tr < l) return 0;
	int tm = (tl+tr) >> 1;
	int res = getsum(v*2,tl,tm,l,r,resmx) + getsum(v*2+1,tm+1,tr,l,r,resmx);
	vec.push_back(v);
	t[v] = t[v*2]+t[v*2+1];
	return res;
}

int get(int a , int b){
	int res = 0;
	for(; B[a] != B[b]; a = P[B[a]]) res += getsum(1,0,n,pos[B[a]],pos[a],1);
	res += getsum(1,0,n,pos[b]+1,pos[a],1);
	return res;
}
void solve(){
	cin >> n;
	for(int i = 1; i < n;i++){
		cin >> a[i] >> b[i] >> c[i];
		a[i]++;	
		b[i]++;	
		g[a[i]].push_back(i);		
		g[b[i]].push_back(i);	
	}
	dfs(1,0);
	decompose(1,1);
	for(int i = 1; i <= n; i++) upd(1,0,n,pos[i],d[i]);
	int q;
	cin >> q;
	for(int i = 1; i <= q; i++){
		int a , b , c , d, e;
		cin >> a >> b >> c >> d >> e;
		a++;
		b++;
		c++;
		d++;
		e++;
		int LCA = lca(a,b);
		LCA = lca(LCA,c);
		LCA = lca(LCA,d);
		LCA = lca(LCA,e);
		ll ans = get(a,LCA);
		ans += get(b,LCA);
		ans += get(c,LCA);
		ans += get(d,LCA);
		ans += get(e,LCA);
		cout << ans << "\n";
		while(vec.size()){
			u[vec.back()] = 1;
			t[vec.back()] = t1[vec.back()];
			vec.pop_back();
		}
	}
}
/*

*/
signed main(){
	ios;
	solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 66 ms 16368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 36 ms 14004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Incorrect 66 ms 16368 KB Output isn't correct
3 Halted 0 ms 0 KB -