Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #define forn(i,n) for(int i=0; i<n; i++)
- #define MAXN 17
- using namespace std;
- int ref[100000 + 100];
- int tree[(1<<MAXN) + 100];
- int n, q;
- int solve(int);
- int query(int, int);
- int main()
- {
- freopen("torneo.in", "r", stdin);
- freopen("torneo.out", "w", stdout);
- int x, y;
- cin >> n >> q;
- forn(i,(1<<n)){
- cin >> x;
- tree[(1<<(MAXN-1))+i] = x;
- ref[x] = i+1;
- }
- solve(1);
- cout << ref[ tree[1] ] << endl;
- forn(i,q){
- cin >> x >> y;
- cout << query(x,y) << endl;
- }
- return 0;
- }
- int query(int x, int y){
- int round = 1;
- int ind = (1<<(MAXN-1)) + x-1;
- ind /= 2; //para que no tome la base de la piramide and boom
- if( x == y ) return 0;
- while( ind ){
- if( (ref[tree[ind*2]]==y) || (ref[tree[ind*2+1]]==y) ) return round;
- round++;
- ind /= 2;
- }
- return 0;
- }
- int solve(int ind){
- int a, b;
- if( ind >= (1<<(MAXN-1)) ) return tree[ind];
- a = (tree[ind*2]) ? tree[ind*2] : solve(ind*2);
- b = (tree[ind*2+1]) ? tree[ind*2+1] : solve(ind*2+1);
- return tree[ind] = max(a,b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement