#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];
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
freopen("input.in","r",stdin);
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";
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:50:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
50 | freopen("input.in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
23916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
23916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
23916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
23916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |