Pagini recente » Cod sursa (job #1362985) | Cod sursa (job #2498243) | Cod sursa (job #2085675) | Cod sursa (job #276172) | Cod sursa (job #3235283)
#include <bits/stdc++.h>
#include <fstream>
#include <queue>
using namespace std;
const int NMAX = 1e5;
int a[NMAX + 1];
int rmq[NMAX + 1][18], LOG[NMAX + 1];
int rmq_max[NMAX + 1][18];
int gcd(int a, int b) {
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
//query in O(1), functioneaza doar pentru functii idempotente
int query1(int x, int y) {
int k = LOG[y - x + 1];
return min(rmq[x][k], rmq[y - (1 << k) + 1][k]);
}
int query1_max(int x, int y) {
int k = LOG[y - x + 1];
return max(rmq_max[x][k], rmq_max[y - (1 << k) + 1][k]);
}
int querry(int x, int y, int a[]) {
if (a[x] == y)
return y;
else if(a[y] == x)
return x;
return a[x] == a[y] ? a[x] : querry(a[x], a[y], a);
}
//query in O(log(n)), merge si pe functii neidempotente
int query2(int x, int y) {
int diff = y - x + 1;
int st = x;
int sol = 1e9;
for(int i = 0; (1 << i) <= diff; i++) {
if((1 << i) & diff) {
sol = gcd(sol, rmq[st][i]);
st += (1 << i);
}
}
return sol;
}
int main() {
//pb1
// ifstream in("rmq.in");
// ofstream out("rmq.out");
// int N, M, x, y;
// in >> N >> M;
// for(int i = 1; i <= N; i++){
// in >> a[i];
// }
// for(int i = 2; i <= N; i++) {
// LOG[i] = LOG[i / 2] + 1;
// }
// for(int i = 1; i <= N; i++) {
// rmq[i][0] = a[i];
// }
// for(int j = 1; (1 << j) <= N; j++) {
// for(int i = 1; i + (1 << j) - 1 <= N; i++) {
// rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
// }
// }
// for(int i = 1; i <= M; i++){
// in >> x >> y;
// out << query1(x, y) << '\n';
// }
//pb 2
// ifstream in("divquery.in");
// ofstream out("divquery.out");
// int N, M, x, y;
// in >> N >> M;
// for(int i = 1; i <= N; i++){
// in >> a[i];
// }
// for(int i = 2; i <= N; i++) {
// LOG[i] = LOG[i / 2] + 1;
// }
// for(int i = 1; i <= N; i++) {
// rmq[i][0] = a[i];
// }
// for(int j = 1; (1 << j) <= N; j++) {
// for(int i = 1; i + (1 << j) - 1 <= N; i++) {
// rmq[i][j] = gcd(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
// }
// }
// for(int i = 1; i <= M; i++){
// in >> x >> y;
// out << query1(x, y) << '\n';
// }
//pb 3
// ifstream in("divquery.in");
// ofstream out("divquery.out");
// int N, M, x, y;
// cin >> N;
// for(int i = 1; i <= N; i++){
// cin >> a[i];
// }
// cin >> M;
// for(int i = 2; i <= N; i++) {
// LOG[i] = LOG[i / 2] + 1;
// }
// for(int i = 1; i <= N; i++) {
// rmq[i][0] = a[i];
// rmq_max[i][0] = a[i];
// }
// for(int j = 1; (1 << j) <= N; j++) {
// for(int i = 1; i + (1 << j) - 1 <= N; i++) {
// rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
// rmq_max[i][j] = max(rmq_max[i][j - 1], rmq_max[i + (1 << (j - 1))][j - 1]);
// }
// }
// queue<int> q;
// int p[NMAX + 1];
// p[1] = 1;
// q.push(1);
// int fr[NMAX + 1]={0};
// fr[a[1]] = 1;
// for (int i = 2; i <= N; i++){
// q.push(i);
// fr[a[i]]++;
// while(fr[a[i]] > 1){
// fr[a[q.front()]]--;
// q.pop();
// }
// p[i] = q.front();
// }
// cout << endl;
// for(int i = 1; i <= N; i++)
// cout << p[i] << " ";
// cout << endl;
// for(int i = 1; i <= M; i++){
// cin >> x >> y;
// if(p[y] <= x && query1_max(x, y) - query1(x, y) == y - x)
// cout << 1;
// else
// cout << 0;
// }
ifstream in("lca.in");
ofstream out("lca.out");
int N, M, x, y;
in >> N >> M;
for(int i = 2; i <= N; i++){
in >> a[i];
}
for(int i = 2; i <= N; i++) {
LOG[i] = LOG[i / 2] + 1;
}
for(int i = 1; i <= M; i++){
in >> x >> y;
out << querry(x, y, a) << '\n';
}
return 0;
}
// https://infoarena.ro/problema/rmq
// https://www.pbinfo.ro/probleme/1735/divquery
// https://www.pbinfo.ro/probleme/3860/consecutive1
// https://www.infoarena.ro/problema/stramosi
// https://www.infoarena.ro/problema/lca
// https://www.youtube.com/watch?v=0jWeUdxrGm4
// https://www.youtube.com/watch?v=oib-XsjFa-M