#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";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
241 ms |
114796 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
241 ms |
114796 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
241 ms |
114796 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
227 ms |
114700 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |