Pagini recente »
Monitorul de evaluare
|
Atașamentele paginii 2024-05-17-clasa-5-tema-42
|
Statistici Ciocirlan Ioana (ioanaaaaa)
|
Diferențe pentru runda/2021-09-12-clasa-10-testare-grupa1 între reviziile 1 și 2
|
Cod sursă (job #786684)
Cod sursă (job
#786684)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("burlane.in");
ofstream fout("burlane.out");
struct burlan{
int p, next, jumps;
}v[100001];
int n, q, blockSize;
void build(){
v[1].next = 0, v[1].jumps = 1;
for(int i = n; i >= 2; i--){
int block = (i - 1) / blockSize, j = i, k = i;
v[i].jumps = 0;
while(j != 0 && block == (j - 1) / blockSize){
j = v[k].p, v[i].jumps++;
k = j;
}
v[i].next = j;
}
}
void update(int x, int y){
v[x].p = y;
int block = (x - 1) / blockSize, bucketFront = block * blockSize + 1, bucketEnd = min(n, (block + 1) * blockSize);
for(int i = bucketFront; i <= bucketEnd; i++)
if(v[i].next < bucketFront)
v[i].next = v[i].p, v[i].jumps = 1;
else
v[i].next = v[v[i].p].next, v[i].jumps = v[v[i].p].jumps + 1;
}
void query(int x){
int rez = 0;
while(x != 0)
rez += v[x].jumps, x = v[x].next;
fout << rez << "\n";
}
void readHoles(){
fin >> n >> q;
for(int i = 1; i <= n; i++)
fin >> v[i].p;
}
void solveQueries(){
int t, x, y;
for(int i = 1; i <= q; i++){
fin >> t >> x;
if(t == 1)
query(x);
else
fin >> y, update(x, y);
}
}
int main(){
readHoles();
blockSize = int(sqrt(n));
build();
solveQueries();
return 0;
}