Pagini recente » Cod sursa (job #310533) | Cod sursa (job #2361068) | Cod sursa (job #62004) | Cod sursa (job #1251440) | Cod sursa (job #1423975)
#include <vector>
#include <fstream>
#include <cmath>
using namespace std;
void fa_tabel_stramosi(vector<vector<int> >& tabel_stramosi){
for(int i = 1, intermediar; i < tabel_stramosi[0].size(); ++i){
for(int j = 0; j < tabel_stramosi.size(); ++j){
tabel_stramosi[j][i] = tabel_stramosi[tabel_stramosi[j][i-1]][i-1]; } } }
int get_ans(const vector<vector<int> >& tabel_stramosi, int num, int h){
for(int i = 0; h; ++i){
if(h & (1<<i)){
num = tabel_stramosi[num][i];
h ^= (1<<i); } }
return num; }
int main(){
ifstream f("stramosi.in");
int n = 0, q = 0;
f >> n >> q;
const int nr_max_stramosi = log2(n+1);
vector<vector<int> > tabel_stramosi(n+1, vector<int>(nr_max_stramosi+1, 0));
for(int i = 1; i <= n; ++i){
f >> tabel_stramosi[i][0]; }
fa_tabel_stramosi(tabel_stramosi);
ofstream g("stramosi.out");
for(int i = 0, num, h; i < q; ++i){
f >> num >> h;
g << get_ans(tabel_stramosi, num, h) << '\n'; }
return 0; }