답안 #373357

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
373357 2021-03-04T09:16:49 Z sam571128 Specijacija (COCI20_specijacija) C++14
0 / 110
72 ms 28288 KB
#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 2e5+5;
vector<int> adj[N];
vector<int> lst, now;
int dp[N][20], dep[N];
int n, q, t, M;

void add_edge(int u, int v){
	adj[u].push_back(v);
	adj[v].push_back(u);
}

void dfs(int u, int p){
	for(auto v : adj[u]){
		if(v==p) continue;
		dep[v] = dep[u]+1;
		dfs(v,u);
		dp[v][0] = u;
	}
}

void init(){
	for(int i = 1;i < 20;i++){
		for(int j = 1;j <= M;j++){
			dp[j][i] = dp[dp[j][i-1]][i-1];
		}
	}
}

int lca(int u, int v){
	if(dep[u] < dep[v]) swap(u,v);
	for(int i = 19;~i;i--){
		if((dep[u]-dep[v])&(1<<i)) u = dp[u][i];
	}
	if(u==v) return u;
	for(int i = 19;~i;i--){
		if(dp[u][i]!=dp[v][i]) u = dp[u][i], v = dp[v][i];
	}
	return dp[u][0];
}

signed main(){
	fastio
	cin >> n >> q >> t;
	M = (n+1)*(n+2)/2;
	int arr[n+1];
	for(int i = 1;i <= n;i++) cin >> arr[i];
	lst.push_back(1);
	for(int i = 1;i <= n+1;i++){
		now.clear();
		int cnt = i*(i-1)/2+1;
		for(int j = (i-1)*(i-2)/2+1;j <= i*(i-1)/2;j++){
			if(arr[i-1]==j){
				add_edge(j,cnt++);
				add_edge(j,cnt++);
			}else{
				add_edge(j,cnt++);
			}
		}
		lst = now;
	}
	dfs(1,-1);
	init();
	int z = 0;
	while(q--){
		int u,v;
		cin >> u >> v;
		u = ((u-1+t*z)%M)+1;
		v = ((v-1+t*z)%M)+1;
		cout << (z = lca(u,v)) << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 69 ms 28268 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 69 ms 28268 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 69 ms 28268 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 72 ms 28288 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -