Cod sursa(job #2231196)
Utilizator | Tamio Vesa Nakajima tamionv | Data | 13 august 2018 14:11:00 |
---|---|---|---|
Problema | Stramosi | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 0.6 kb |
#include <fstream>
using namespace std;
constexpr int maxn = 250000 + 10;
constexpr int maxq = 300000 + 10;
int tt[18][maxn];
int main(){
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int n, m;
f >> n >> m;
for(int i = 1; i <= n; ++i)
f >> tt[0][i];
for(int i = 1; i < 18; ++i)
for(int j = 1; j <= n; ++j)
tt[i][j] = tt[i-1][tt[i-1][j]];
for(int i = 0, q, p; i < m; ++i){
f >> q >> p;
while(p){
const int niv = __builtin_ctz(p);
q = tt[niv][q];
p ^= (1<<niv); }
g << q << '\n'; } }