답안 #1034979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1034979 2024-07-26T02:01:05 Z pcc Prize (CEOI22_prize) C++17
0 / 100
3035 ms 425232 KB
#include <bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define fs first
#define sc second
#define ll long long
#define al4 array<ll,4>

const int mxn = 1e6+10;
const int B = 20;

struct Tree{
	vector<vector<int>> par;
	vector<int> dfn;
	vector<pii> eul;
	vector<int> dep;
	vector<vector<int>> edges;
	int pt = 0;
	void init(int n){
		eul = vector<pii>(n);
		dep = vector<int>(n);
		edges = vector<vector<int>>(n);
		par = vector<vector<int>>(n,vector<int>(B,0));
	}
	void add_edge(int a,int b){
		edges[a].push_back(b);
	}
	void dfs(int now = 1,int fa = 1){
		eul[now].fs = ++pt;
		dfn.push_back(now);
		par[now][0] = fa;
		for(int i = 1;i<B;i++)par[now][i] = par[par[now][i-1]][i-1];
		for(auto nxt:edges[now]){
			if(nxt == par[now][0])continue;
			par[nxt][0] = now;
			dep[nxt] = dep[now]+1;
			dfs(nxt,now);
		}
		eul[now].sc = pt;
	}
	int LCA(int a,int b){
		if(dep[a]<dep[b])swap(a,b);
		int d = dep[a]-dep[b];
		for(int i = B-1;i>=0;i--){
			if(d&(1<<i))a = par[a][i];
		}
		for(int i = B-1;i>=0;i--){
			if(par[a][i] != par[b][i])a = par[a][i],b = par[b][i];
		}
		return a==b?a:par[a][0];
	}
};

Tree t1,t2;
int rt1,rt2;
int N,K,Q,T;
ll val1[mxn],val2[mxn];
vector<int> tar;

al4 ask(int a,int b){
	cout<<"? "<<a<<' '<<b<<endl;
	al4 re;
	for(auto &i:re)cin>>i;
	return re;
}

int main(){
	//ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N>>K>>Q>>T;
	t1.init(N+1);t2.init(N+1);
	for(int i = 1;i<=N;i++){
		int p;
		cin>>p;
		if(p == -1)rt1 = i;
		else t1.add_edge(p,i);
	}
	for(int i = 1;i<=N;i++){
		int p;
		cin>>p;
		if(p == -1)rt2 = i;
		else t2.add_edge(p,i);
	}
	t1.dfs(rt1,rt1);t2.dfs(rt2,rt2);
	for(int i = 0;i<K;i++)tar.push_back(t1.dfn[i]);
	for(auto &i:tar)cout<<i<<' ';cout<<endl;
	for(int i = 1;i<K;i++){
		auto tmp = ask(tar[0],tar[i]);
		val1[tar[i]] = tmp[1];
	}
	cout<<"!"<<endl;
	while(T--){
		int a,b;
		cin>>a>>b;
		ll ans = val1[a]+val1[b]-val1[t1.LCA(a,b)]*2;
		cout<<ans<<' '<<ans<<endl;
	}
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:86:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   86 |  for(auto &i:tar)cout<<i<<' ';cout<<endl;
      |  ^~~
Main.cpp:86:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   86 |  for(auto &i:tar)cout<<i<<' ';cout<<endl;
      |                               ^~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1395 ms 212672 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1321 ms 212736 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1394 ms 212756 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3035 ms 425232 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3035 ms 425232 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -