Pagini recente » Cod sursa (job #2649158) | Cod sursa (job #1891676) | Cod sursa (job #1853963) | Cod sursa (job #1491644) | Cod sursa (job #2828360)
#include <bits/stdc++.h>
#define dim 250005
#define logg 18
using namespace std;
class prs{
private:
FILE *fin;
char *buff;
int sp;
char read_ch(){
++sp;
if(sp==4096){
sp=0;
fread(buff,1,4096,fin);
}
return buff[sp];
}
public:
prs(const char *name){
fin=fopen(name,"r");
buff=new char [4096]();
sp=4095;
}
prs& operator >> (int &n){
char c;
while(!isdigit(c=read_ch()));
n=c-'0';
while(isdigit(c=read_ch()))
n=n*10+c-'0';
return *this;
}
};
class OutParser {
private:
FILE *fout;
char *buff;
int sp;
void write_ch(char ch) {
if (sp == 50000) {
fwrite(buff, 1, 50000, fout);
sp = 0;
buff[sp++] = ch;
} else {
buff[sp++] = ch;
}
}
public:
OutParser(const char* name) {
fout = fopen(name, "w");
buff = new char[50000]();
sp = 0;
}
~OutParser() {
fwrite(buff, 1, sp, fout);
fclose(fout);
}
OutParser& operator << (int vu32) {
if (vu32 <= 9) {
write_ch(vu32 + '0');
} else {
(*this) << (vu32 / 10);
write_ch(vu32 % 10 + '0');
}
return *this;
}
OutParser& operator << (long long vu64) {
if (vu64 <= 9) {
write_ch(vu64 + '0');
} else {
(*this) << (vu64 / 10);
write_ch(vu64 % 10 + '0');
}
return *this;
}
OutParser& operator << (char ch) {
write_ch(ch);
return *this;
}
OutParser& operator << (const char *ch) {
while (*ch) {
write_ch(*ch);
++ch;
}
return *this;
}
};
int n,m;
int dp[logg][dim];
int putere[dim];
int main()
{
prs fin("stramosi.in");
OutParser fout("stramosi.out");
fin>>n>>m;
putere[1]=0;
for(int i=2;i<=n;++i)
putere[i]=putere[(i>>1)]+1;
for(int i=1;i<=n;++i)
fin>>dp[0][i];
for(int j=1;(1<<j)<=n;++j)
for(int i=1;i<=n;++i)
dp[j][i]=dp[j-1][dp[j-1][i]];
int q,p;
for(int i=1;i<=m;++i){
fin>>q>>p;
while(p){
q=dp[putere[p]][q];
p-=(1<<putere[p]);
}
fout<<q<<"\n";
}
}