This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 = 1e6+5;
vector<int> adj[N];
int dp[N][22], 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 < 22;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 = 21;~i;i--){
if((dep[u]-dep[v])&(1<<i)) u = dp[u][i];
}
if(u==v) return u;
for(int i = 21;~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];
for(int i = 1;i <= n+1;i++){
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++);
}
}
}
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";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |