답안 #922339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
922339 2024-02-05T12:24:26 Z pcc Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
234 ms 11356 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>

const int mxn = 5e4+10;
int N,Q;
vector<pii> tree[mxn];
ll segtree[mxn*4];
int idx[mxn],par[mxn],sz[mxn],bs[mxn],link_top[mxn],dep[mxn],val[mxn],arr[mxn],cnt;
vector<pll> st;

void modify(int now,int l,int r,int s,int e){
	if(!segtree[now])return;
	if(l>=s&&e>=r){
		st.push_back(make_pair(now,segtree[now]));
		segtree[now] = 0;
		return;
	}
	int mid = (l+r)>>1;
	if(mid>=s)modify(now*2+1,l,mid,s,e);
	if(mid<e)modify(now*2+2,mid+1,r,s,e);
	st.push_back(make_pair(now,segtree[now]));
	segtree[now] = segtree[now*2+1]+segtree[now*2+2];
}
ll getval(int now,int l,int r,int s,int e){
	if(l>=s&&e>=r)return segtree[now];
	else if(!segtree[now])return 0ll;
	int mid = (l+r)>>1;
	if(mid>=e)return getval(now*2+1,l,mid,s,e);
	else if(mid<s)return getval(now*2+2,mid+1,r,s,e);
	else return getval(now*2+1,l,mid,s,e)+getval(now*2+2,mid+1,r,s,e);
}
void build(int now,int l,int r){
	if(l==r){
		segtree[now] = val[l];
		return;
	}
	int mid = (l+r)>>1;
	build(now*2+1,l,mid);
	build(now*2+2,mid+1,r);
	segtree[now] = segtree[now*2+1]+segtree[now*2+2];
	return;
}

void dfs1(int now){
	sz[now] = 1;
	for(auto nxt:tree[now]){
		if(nxt.fs == par[now])continue;
		par[nxt.fs] = now;
		arr[nxt.fs] = nxt.sc;
		dep[nxt.fs] = dep[now]+1;
		dfs1(nxt.fs);
		sz[now] += sz[nxt.fs];
		if(!bs[now]||sz[bs[now]]<sz[nxt.fs])bs[now] = nxt.fs;
	}
	return;
}
void dfs2(int now,int top){
	idx[now] = ++cnt;
	val[cnt] = arr[now];
	link_top[now] = top;
	if(bs[now])dfs2(bs[now],top);
	for(auto nxt:tree[now]){
		if(nxt.fs == par[now]||nxt.fs == bs[now])continue;
		dfs2(nxt.fs,nxt.fs);
	}
	return;
}

ll lca(int a,int b){
	int ta = link_top[a],tb = link_top[b];
	ll re = 0;
	while(ta != tb){
		if(dep[ta]<dep[tb]){
			swap(a,b);
			swap(ta,tb);
		}
		re += getval(0,1,N,idx[ta],idx[a]);
		modify(0,1,N,idx[ta],idx[a]);
		a = par[ta];
		ta = link_top[a];
	}
	if(dep[a]>dep[b])swap(a,b);
	if(a != b){
		re += getval(0,1,N,idx[a]+1,idx[b]);
		modify(0,1,N,idx[a]+1,idx[b]);
	}
	return re;
}

void segdfs(int now,int l,int r){
	cout<<l<<' '<<r<<":"<<segtree[now]<<endl;
	if(l == r)return;
	int mid = (l+r)>>1;
	segdfs(now*2+1,l,mid);
	segdfs(now*2+2,mid+1,r);
	return;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N;
	for(int i = 1;i<N;i++){
		int a,b,c;
		cin>>a>>b>>c;
		a++,b++;
		tree[a].push_back({b,c});
		tree[b].push_back({a,c});
	}
	dep[1] = 1;
	dfs1(1);
	dfs2(1,1);
	build(0,1,N);
	/*
	for(int i = 1;i<=N;i++)cout<<idx[i]<<' ';cout<<endl;
	for(int i = 1;i<=N;i++)cout<<val[i]<<' ';cout<<endl;
	segdfs(0,1,N);

   */
	cin>>Q;
	while(Q--){
		int req[5];
		for(auto &i:req)cin>>i,i++;
		ll ans = 0;
		for(int i = 0;i<5;i++){
			for(int j = i+1;j<5;j++){
				ans += lca(req[i],req[j]);
			}
		}
		cout<<ans<<'\n';
		while(!st.empty()){
			segtree[st.back().fs] = st.back().sc;
			st.pop_back();
		}
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 9480 KB Output is correct
2 Correct 63 ms 11348 KB Output is correct
3 Correct 61 ms 11356 KB Output is correct
4 Correct 69 ms 11288 KB Output is correct
5 Correct 62 ms 11268 KB Output is correct
6 Correct 61 ms 11348 KB Output is correct
7 Correct 62 ms 11348 KB Output is correct
8 Correct 61 ms 11240 KB Output is correct
9 Correct 61 ms 11248 KB Output is correct
10 Correct 61 ms 11356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 7260 KB Output is correct
2 Correct 19 ms 10176 KB Output is correct
3 Correct 19 ms 10332 KB Output is correct
4 Correct 19 ms 10332 KB Output is correct
5 Correct 19 ms 10332 KB Output is correct
6 Correct 21 ms 10424 KB Output is correct
7 Correct 19 ms 10264 KB Output is correct
8 Correct 19 ms 10324 KB Output is correct
9 Correct 19 ms 10364 KB Output is correct
10 Correct 19 ms 10272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 71 ms 9480 KB Output is correct
3 Correct 63 ms 11348 KB Output is correct
4 Correct 61 ms 11356 KB Output is correct
5 Correct 69 ms 11288 KB Output is correct
6 Correct 62 ms 11268 KB Output is correct
7 Correct 61 ms 11348 KB Output is correct
8 Correct 62 ms 11348 KB Output is correct
9 Correct 61 ms 11240 KB Output is correct
10 Correct 61 ms 11248 KB Output is correct
11 Correct 61 ms 11356 KB Output is correct
12 Correct 18 ms 7260 KB Output is correct
13 Correct 19 ms 10176 KB Output is correct
14 Correct 19 ms 10332 KB Output is correct
15 Correct 19 ms 10332 KB Output is correct
16 Correct 19 ms 10332 KB Output is correct
17 Correct 21 ms 10424 KB Output is correct
18 Correct 19 ms 10264 KB Output is correct
19 Correct 19 ms 10324 KB Output is correct
20 Correct 19 ms 10364 KB Output is correct
21 Correct 19 ms 10272 KB Output is correct
22 Correct 74 ms 9512 KB Output is correct
23 Correct 234 ms 7784 KB Output is correct
24 Correct 78 ms 10740 KB Output is correct
25 Correct 74 ms 10740 KB Output is correct
26 Correct 75 ms 10736 KB Output is correct
27 Correct 72 ms 10588 KB Output is correct
28 Correct 71 ms 10756 KB Output is correct
29 Correct 81 ms 10884 KB Output is correct
30 Correct 72 ms 10588 KB Output is correct
31 Correct 72 ms 10588 KB Output is correct