Arkimond9620 commited on
Commit
e97ede4
·
verified ·
1 Parent(s): 0a84324

Upload 9015 files

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. ancestors/dp_log/538af9a6-139a-11eb-8399-309c234925a0.cpp +92 -0
  2. ancestors/dp_log/539c1098-139a-11eb-b4df-309c234925a0.cpp +103 -0
  3. ancestors/dp_log/53aab706-139a-11eb-8714-309c234925a0.cpp +31 -0
  4. ancestors/dp_log/53babe00-139a-11eb-8a6d-309c234925a0.cpp +88 -0
  5. ancestors/dp_log/53cc2326-139a-11eb-8257-309c234925a0.cpp +75 -0
  6. ancestors/dp_log/540f8312-139a-11eb-a770-309c234925a0.cpp +107 -0
  7. ancestors/dp_log/541e03f0-139a-11eb-94e2-309c234925a0.cpp +46 -0
  8. ancestors/dp_log/542c9c14-139a-11eb-a355-309c234925a0.cpp +88 -0
  9. ancestors/dp_log/543c50be-139a-11eb-b5f1-309c234925a0.cpp +98 -0
  10. ancestors/dp_log/547f08e4-139a-11eb-a943-309c234925a0.cpp +90 -0
  11. ancestors/dp_log/54926f3a-139a-11eb-bda3-309c234925a0.cpp +76 -0
  12. ancestors/dp_log/54a1ff1a-139a-11eb-8043-309c234925a0.cpp +104 -0
  13. ancestors/dp_log/54b0577e-139a-11eb-b333-309c234925a0.cpp +57 -0
  14. ancestors/dp_log/54beeb38-139a-11eb-8978-309c234925a0.cpp +80 -0
  15. ancestors/dp_log/54d2f880-139a-11eb-a792-309c234925a0.cpp +86 -0
  16. ancestors/dp_log/54f11f2e-139a-11eb-84f1-309c234925a0.cpp +68 -0
  17. ancestors/dp_log/5500616e-139a-11eb-835a-309c234925a0.cpp +99 -0
  18. ancestors/dp_log/5510b52e-139a-11eb-85a6-309c234925a0.cpp +66 -0
  19. ancestors/dp_log/551ee666-139a-11eb-bfb9-309c234925a0.cpp +66 -0
  20. ancestors/dp_log/555a6188-139a-11eb-88ba-309c234925a0.cpp +66 -0
  21. ancestors/dp_log/5569ccc8-139a-11eb-928b-309c234925a0.cpp +62 -0
  22. ancestors/dp_log/55795aec-139a-11eb-a6e0-309c234925a0.cpp +60 -0
  23. ancestors/dp_log/55896192-139a-11eb-8f27-309c234925a0.cpp +58 -0
  24. ancestors/dp_log/5598cae4-139a-11eb-af18-309c234925a0.cpp +60 -0
  25. ancestors/dp_log/55ab4176-139a-11eb-a30f-309c234925a0.cpp +52 -0
  26. ancestors/dp_log/55e78718-139a-11eb-84d2-309c234925a0.cpp +58 -0
  27. ancestors/dp_log/560a78e8-139a-11eb-9736-309c234925a0.cpp +58 -0
  28. ancestors/dp_log/56188240-139a-11eb-bfed-309c234925a0.cpp +79 -0
  29. ancestors/dp_log/562c0a2c-139a-11eb-a615-309c234925a0.cpp +61 -0
  30. ancestors/dp_log/563eceda-139a-11eb-948b-309c234925a0.cpp +79 -0
  31. ancestors/dp_log/564da036-139a-11eb-89a6-309c234925a0.cpp +69 -0
  32. ancestors/dp_log/565d7eba-139a-11eb-927a-309c234925a0.cpp +61 -0
  33. ancestors/dp_log/56961ad8-139a-11eb-8038-309c234925a0.cpp +73 -0
  34. ancestors/dp_log/56b8e524-139a-11eb-8016-309c234925a0.cpp +78 -0
  35. ancestors/dp_log/56c84e86-139a-11eb-8425-309c234925a0.cpp +69 -0
  36. ancestors/dp_log/56d9176c-139a-11eb-988a-309c234925a0.cpp +63 -0
  37. ancestors/dp_log/56e80b76-139a-11eb-b7b3-309c234925a0.cpp +63 -0
  38. ancestors/dp_log/56f63c48-139a-11eb-a94f-309c234925a0.cpp +74 -0
  39. ancestors/dp_log/5705ccb4-139a-11eb-9fe8-309c234925a0.cpp +60 -0
  40. ancestors/dp_log/571758e2-139a-11eb-83aa-309c234925a0.cpp +54 -0
  41. ancestors/dp_log/57264cf4-139a-11eb-84cc-309c234925a0.cpp +85 -0
  42. ancestors/dp_log/5735b646-139a-11eb-847e-309c234925a0.cpp +36 -0
  43. ancestors/dp_log/57482cd8-139a-11eb-8c39-309c234925a0.cpp +81 -0
  44. ancestors/dp_log/575747f8-139a-11eb-bd5d-309c234925a0.cpp +83 -0
  45. ancestors/dp_log/57ab1924-139a-11eb-b836-309c234925a0.cpp +68 -0
  46. ancestors/dp_log/57bacb8c-139a-11eb-b5a6-309c234925a0.cpp +31 -0
  47. ancestors/dp_log/57cab226-139a-11eb-8e00-309c234925a0.cpp +316 -0
  48. ancestors/dp_log/57d97f28-139a-11eb-b768-309c234925a0.cpp +66 -0
  49. ancestors/dp_log/57e90f92-139a-11eb-b35d-309c234925a0.cpp +84 -0
  50. ancestors/dp_log/57fac2f8-139a-11eb-ba35-309c234925a0.cpp +60 -0
ancestors/dp_log/538af9a6-139a-11eb-8399-309c234925a0.cpp ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+ using namespace std;
5
+
6
+ ofstream g ("stramosi.out");
7
+
8
+ int const N = 250001 , SZ = 1e6;
9
+
10
+ int v [N][18] , p;
11
+
12
+ char s [SZ];
13
+
14
+ int getn (){
15
+
16
+ int x = 0;
17
+
18
+ while (! isdigit (s [p])){
19
+
20
+ ++ p;
21
+
22
+ if (p == SZ)
23
+
24
+ fread (s , 1 , SZ , stdin) , p = 0;
25
+
26
+ }
27
+
28
+ while (isdigit (s [p])){
29
+
30
+ x = x * 10 + (s [p] - '0');
31
+
32
+ ++ p;
33
+
34
+ if (p == SZ)
35
+
36
+ fread (s , 1 , SZ , stdin) , p = 0;
37
+
38
+ }
39
+
40
+ return x;
41
+
42
+ }
43
+
44
+ int main()
45
+
46
+ {
47
+
48
+ int n , m , i , j , p , cur , q , x;
49
+
50
+ freopen ("stramosi.in" , "r" , stdin);
51
+
52
+ scanf ("%d%d" , &n , &m);
53
+
54
+ fread (s , 1 , SZ , stdin);
55
+
56
+ for(i = 1 ; i <= n ; ++ i)
57
+
58
+ v [i][0] = getn ();
59
+
60
+ for(i = 1 ; (1 << i) <= n ; ++ i)
61
+
62
+ for(j = (1 << i) + 1 ; j <= n ; ++ j)
63
+
64
+ v [j][i] = v [v [j][i - 1]][i - 1];
65
+
66
+ while (m --){
67
+
68
+ p = getn () , q = getn ();
69
+
70
+ cur = p , x = 0;
71
+
72
+ while ((1 << x) < q)
73
+
74
+ ++ x;
75
+
76
+ for(i = x ; i >= 0 ; -- i)
77
+
78
+ if ((1 << i) & q)
79
+
80
+ cur = v [cur][i];
81
+
82
+ g << cur << '\n';
83
+
84
+ }
85
+
86
+
87
+
88
+
89
+
90
+ return 0;
91
+
92
+ }
ancestors/dp_log/539c1098-139a-11eb-b4df-309c234925a0.cpp ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+
4
+
5
+ using namespace std;
6
+
7
+
8
+
9
+ ifstream fin ("stramosi.in");
10
+
11
+ ofstream fout ("stramosi.out");
12
+
13
+
14
+
15
+ int tt[250001];
16
+
17
+ int d[18][250001];
18
+
19
+
20
+
21
+ int main()
22
+
23
+ {
24
+
25
+ int n, m, i, p, log;
26
+
27
+ fin>>n>>m;
28
+
29
+
30
+
31
+ for(i=1; i<=n; i++)
32
+
33
+ {
34
+
35
+ fin>>d[0][i];
36
+
37
+ }
38
+
39
+
40
+
41
+ log=0;
42
+
43
+ while( (1<<log)<=n )
44
+
45
+ {
46
+
47
+ log++;
48
+
49
+ }
50
+
51
+ log--;
52
+
53
+
54
+
55
+
56
+
57
+ for(p=1; p<=log; p++)
58
+
59
+ {
60
+
61
+ for(i=1; i<=n; i++)
62
+
63
+ {
64
+
65
+ d[p][i]=d[p-1][ d[p-1][i] ];
66
+
67
+ }
68
+
69
+ }
70
+
71
+
72
+
73
+ for(i=1; i<=m; i++)
74
+
75
+ {
76
+
77
+ int nod, x;
78
+
79
+ fin>>nod>>x;
80
+
81
+
82
+
83
+ for(p=0; p<=log; p++)
84
+
85
+ {
86
+
87
+ if( (1<<p) & x )
88
+
89
+ {
90
+
91
+ nod=d[p][nod];
92
+
93
+ }
94
+
95
+ }
96
+
97
+
98
+
99
+ fout<<nod<<"\n";
100
+
101
+ }
102
+
103
+ }
ancestors/dp_log/53aab706-139a-11eb-8714-309c234925a0.cpp ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+ ifstream fin("stramosi.in");
7
+ ofstream fout("stramosi.out");
8
+
9
+ int dp[21][250005];
10
+
11
+ int main() {
12
+ int n, m;
13
+ fin >> n >> m;
14
+ for(int i = 1;i <= n; ++i){
15
+ fin >> dp[0][i];
16
+ }
17
+ for(int j = 1;(1 << j) <= n; ++j)
18
+ for(int i = 1;i <= n; ++i)
19
+ dp[j][i] = dp[j-1][dp[j - 1][i]];
20
+ while(m--){
21
+ int q, p;
22
+ fin >> q >> p;
23
+ int ans = q;
24
+ for(int j = 0;j <= 20; ++j){
25
+ if((p & (1 << j)) != 0)
26
+ ans = dp[j][ans];
27
+ }
28
+ fout << ans << "\n";
29
+ }
30
+ return 0;
31
+ }
ancestors/dp_log/53babe00-139a-11eb-8a6d-309c234925a0.cpp ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ ifstream f("stramosi.in");
9
+
10
+ ofstream g("stramosi.out");
11
+
12
+
13
+
14
+ int n,m,i,stramos,t,query,pas,k,tata[250100],d[20][250100];
15
+
16
+
17
+
18
+ int main()
19
+
20
+ {
21
+
22
+ f>>n>>m;
23
+
24
+ for(i=1;i<=n;i++)
25
+
26
+ {
27
+
28
+ f>>tata[i];
29
+
30
+ d[0][i]=tata[i];
31
+
32
+ }
33
+
34
+
35
+
36
+ for(k=1;(1<<k)<=n;k++)
37
+
38
+ {
39
+
40
+ for(i=1;i<=n;i++)
41
+
42
+ {
43
+
44
+ d[k][i]=d[k-1][d[k-1][i]];
45
+
46
+ }
47
+
48
+ }
49
+
50
+ for(query=1;query<=m;query++)
51
+
52
+ {
53
+
54
+ f>>i>>t;
55
+
56
+
57
+
58
+ stramos=i;
59
+
60
+ pas=0;
61
+
62
+ while(t)
63
+
64
+ {
65
+
66
+ if(t%2==1)
67
+
68
+ {
69
+
70
+ stramos=d[pas][stramos];
71
+
72
+ }
73
+
74
+ pas++;
75
+
76
+ t/=2;
77
+
78
+ }
79
+
80
+
81
+
82
+ g<<stramos<<'\n';
83
+
84
+ }
85
+
86
+ return 0;
87
+
88
+ }
ancestors/dp_log/53cc2326-139a-11eb-8257-309c234925a0.cpp ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <vector>
3
+ #include <deque>
4
+ #include <algorithm>
5
+ #include <math.h>
6
+ #include <fstream>
7
+ using namespace std;
8
+
9
+
10
+
11
+ ifstream fin("stramosi.in");
12
+
13
+ ofstream fout("stramosi.out");
14
+
15
+
16
+
17
+ int n, q, logN;
18
+
19
+
20
+
21
+ int main()
22
+
23
+ {
24
+
25
+ fin >> n >> q;
26
+
27
+
28
+
29
+ logN = log2(n);
30
+
31
+
32
+
33
+ vector<vector<int> > d(logN + 1, vector<int>(250001));
34
+
35
+
36
+
37
+ for (int i = 1; i <= n; ++i)
38
+
39
+ fin >> d[0][i];
40
+
41
+
42
+
43
+ for (int pw = 1; pw <= logN; ++pw)
44
+
45
+ for (int i = 1; i <= n; ++i)
46
+
47
+ d[pw][i] = d[pw - 1][d[pw - 1][i]];
48
+
49
+
50
+
51
+ for (int i = 0; i < q; i++)
52
+
53
+ {
54
+
55
+ int nod, x;
56
+
57
+
58
+
59
+ fin >> nod >> x;
60
+
61
+
62
+
63
+ for (int pw = logN; pw >= 0; --pw)
64
+
65
+ if (((1 << pw) & x))
66
+
67
+ nod = d[pw][nod];
68
+
69
+
70
+
71
+ fout << nod << "\n";
72
+
73
+ }
74
+
75
+ }
ancestors/dp_log/540f8312-139a-11eb-a770-309c234925a0.cpp ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+
4
+
5
+ using namespace std;
6
+
7
+
8
+
9
+ ifstream fin("stramosi.in");
10
+
11
+ ofstream fout("stramosi.out");
12
+
13
+
14
+
15
+ const int N = 250005, LOG = 18;
16
+
17
+
18
+
19
+ int n, q;
20
+
21
+ int t[LOG][N];
22
+
23
+
24
+
25
+ void precompute()
26
+
27
+ {
28
+
29
+ for (int lvl = 1; (1 << lvl) <= n; lvl++)
30
+
31
+ {
32
+
33
+ for (int i = 1; i <= n; i++)
34
+
35
+ {
36
+
37
+ t[lvl][i] = t[lvl - 1][t[lvl - 1][i]];
38
+
39
+ }
40
+
41
+ }
42
+
43
+ }
44
+
45
+
46
+
47
+ int SolveQuery(int membru, int nr)
48
+
49
+ {
50
+
51
+ int ans = membru;
52
+
53
+ for (int bit = 0; bit < 18; bit++)
54
+
55
+ {
56
+
57
+ if (nr & (1 << bit))
58
+
59
+ {
60
+
61
+ ans = t[bit][ans];
62
+
63
+ }
64
+
65
+ }
66
+
67
+ return ans;
68
+
69
+ }
70
+
71
+
72
+
73
+ int main()
74
+
75
+ {
76
+
77
+ fin >> n >> q;
78
+
79
+
80
+
81
+ for (int i = 1; i <= n; i++)
82
+
83
+ {
84
+
85
+ fin >> t[0][i];
86
+
87
+ }
88
+
89
+
90
+
91
+ precompute();
92
+
93
+
94
+
95
+ while (q--)
96
+
97
+ {
98
+
99
+ int persona, nr;
100
+
101
+ fin >> persona >> nr;
102
+
103
+ fout << SolveQuery(persona, nr) << '\n';
104
+
105
+ }
106
+
107
+ }
ancestors/dp_log/541e03f0-139a-11eb-94e2-309c234925a0.cpp ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+ #include <vector>
4
+
5
+ using namespace std;
6
+
7
+ ifstream fin("stramosi.in");
8
+ ofstream fout("stramosi.out");
9
+
10
+ const int N = 250005;
11
+ const int LOG = 18;
12
+
13
+ int n, q;
14
+ int anc[LOG][N];
15
+
16
+ void precompute()
17
+ {
18
+ for(int lvl = 1; (1 << lvl) <= n; lvl++)
19
+ for(int i = 1; i <= n; i++)
20
+ anc[lvl][i] = anc[lvl-1][anc[lvl-1][i]];
21
+ }
22
+
23
+ int solveQuery(int p, int q)
24
+ {
25
+ int ans = q;
26
+ for(int bit = 0; bit < LOG; bit++)
27
+ if(p & (1 << bit))
28
+ ans = anc[bit][ans];
29
+ return ans;
30
+ }
31
+
32
+ int main()
33
+ {
34
+ fin >> n >> q;
35
+ for(int i = 1; i <= n; i++)
36
+ fin >> anc[0][i];
37
+
38
+ precompute();
39
+ while(q--)
40
+ {
41
+ int p, q;
42
+ fin >> q >> p;
43
+ fout << solveQuery(p, q) << '\n';
44
+ }
45
+ return 0;
46
+ }
ancestors/dp_log/542c9c14-139a-11eb-a355-309c234925a0.cpp ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+ #include <vector>
4
+
5
+
6
+ using namespace std;
7
+
8
+
9
+
10
+ ifstream fin("stramosi.in");
11
+
12
+ ofstream fout("stramosi.out");
13
+
14
+
15
+
16
+ const int N = 250005;
17
+
18
+ const int LOG = 18;
19
+
20
+
21
+
22
+ int n, q;
23
+
24
+ int anc[LOG][N];
25
+
26
+
27
+
28
+ void precompute()
29
+
30
+ {
31
+
32
+ for (int lvl = 1; (1 << lvl) <= n; lvl++)
33
+
34
+ for (int i = 1; i <= n; i++)
35
+
36
+ anc[lvl][i] = anc[lvl - 1][anc[lvl - 1][i]];
37
+
38
+ }
39
+
40
+
41
+
42
+ int solveQuery(int p, int q)
43
+
44
+ {
45
+
46
+ int ans = q;
47
+
48
+ for (int bit = 0; bit < LOG; bit++)
49
+
50
+ if (p & (1 << bit))
51
+
52
+ ans = anc[bit][ans];
53
+
54
+ return ans;
55
+
56
+ }
57
+
58
+
59
+
60
+ int main()
61
+
62
+ {
63
+
64
+ fin >> n >> q;
65
+
66
+ for (int i = 1; i <= n; i++)
67
+
68
+ fin >> anc[0][i];
69
+
70
+
71
+
72
+ precompute();
73
+
74
+ while (q--)
75
+
76
+ {
77
+
78
+ int p, q;
79
+
80
+ fin >> q >> p;
81
+
82
+ fout << solveQuery(p, q) << '\n';
83
+
84
+ }
85
+
86
+ return 0;
87
+
88
+ }
ancestors/dp_log/543c50be-139a-11eb-b5f1-309c234925a0.cpp ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+
5
+
6
+ using namespace std;
7
+
8
+
9
+
10
+ ifstream fin ("stramosi.in");
11
+
12
+ ofstream fout ("stramosi.out");
13
+
14
+
15
+
16
+ void usain_bolt()
17
+
18
+ {
19
+
20
+ ios::sync_with_stdio(false);
21
+
22
+ fin.tie(0);
23
+
24
+ }
25
+
26
+
27
+
28
+ const int N = 250005;
29
+
30
+
31
+
32
+ int dad[N][18];
33
+
34
+
35
+
36
+ int main()
37
+
38
+ {
39
+
40
+ usain_bolt();
41
+
42
+
43
+
44
+ int n, q;
45
+
46
+
47
+
48
+ fin >> n >> q;
49
+
50
+ for(int i = 1; i <= n; ++i) {
51
+
52
+ fin >> dad[i][0];
53
+
54
+ }
55
+
56
+ for(int j = 1; j <= 17; ++j) {
57
+
58
+ for(int i = 1; i <= n; ++i) {
59
+
60
+ dad[i][j] = dad[dad[i][j - 1]][j - 1];
61
+
62
+ }
63
+
64
+ }
65
+
66
+ while(q--) {
67
+
68
+ int x, y;
69
+
70
+
71
+
72
+ fin >> x >> y;
73
+
74
+
75
+
76
+ int cnt = 0;
77
+
78
+
79
+
80
+ while(y > 0) {
81
+
82
+ if(y & 1) {
83
+
84
+ x = dad[x][cnt];
85
+
86
+ }
87
+
88
+ y >>= 1, ++cnt;
89
+
90
+ }
91
+
92
+ fout << x << "\n";
93
+
94
+ }
95
+
96
+ return 0;
97
+
98
+ }
ancestors/dp_log/547f08e4-139a-11eb-a943-309c234925a0.cpp ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ const int MAX_N = 250000;
9
+
10
+ const int MAX_K = log2(MAX_N) + 1;
11
+
12
+
13
+
14
+ int T[MAX_K][MAX_N];
15
+
16
+
17
+
18
+ int N, M;
19
+
20
+
21
+
22
+ int main()
23
+
24
+ {
25
+
26
+ ifstream cin("stramosi.in");
27
+
28
+ ofstream cout("stramosi.out");
29
+
30
+
31
+
32
+ cin >> N >> M;
33
+
34
+
35
+
36
+ for(int i = 1; i <= N; ++i)
37
+
38
+ cin >> T[0][i];
39
+
40
+
41
+
42
+ int K = log2(N) + 1;
43
+
44
+
45
+
46
+ for(int k = 1; k < K; ++k)
47
+
48
+ for(int i = 1; i <= N; ++i)
49
+
50
+ T[k][i] = T[k - 1][T[k - 1][i]];
51
+
52
+
53
+
54
+ for(int i = 1; i <= M; ++i)
55
+
56
+ {
57
+
58
+ int Q, P;
59
+
60
+ cin >> Q >> P;
61
+
62
+
63
+
64
+ int k = 0;
65
+
66
+
67
+
68
+ while(P)
69
+
70
+ {
71
+
72
+ if(P & 1) Q = T[k][Q];
73
+
74
+
75
+
76
+ ++k;
77
+
78
+ P = P >> 1;
79
+
80
+ }
81
+
82
+
83
+
84
+ cout << Q << '\n';
85
+
86
+
87
+
88
+ }
89
+
90
+ }
ancestors/dp_log/54926f3a-139a-11eb-bda3-309c234925a0.cpp ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+ using namespace std;
5
+
6
+ ifstream in("stramosi.in");
7
+
8
+ ofstream out("stramosi.out");
9
+
10
+ long long ma[30][250100];
11
+
12
+ int main()
13
+
14
+ {
15
+
16
+ long long n,m,p,q;
17
+
18
+ int i,j;
19
+
20
+ in>>n>>m;
21
+
22
+ for(i=1; i<=n; i++)
23
+
24
+ {
25
+
26
+ in>>ma[0][i];
27
+
28
+ }
29
+
30
+ for(i=1; (1<<i)<=n; i++)
31
+
32
+ {
33
+
34
+ for(j=1; j<=n; j++)
35
+
36
+ {
37
+
38
+ ma[i][j]=ma[i-1][ma[i-1][j]];
39
+
40
+ }
41
+
42
+ }
43
+
44
+ while(m--)
45
+
46
+ {
47
+
48
+ in>>q>>p;
49
+
50
+ int ok=0;
51
+
52
+ while(p)
53
+
54
+ {
55
+
56
+ if(p%2==1)
57
+
58
+ {
59
+
60
+ q=ma[ok][q];
61
+
62
+ }
63
+
64
+ ok++;
65
+
66
+ p/=2;
67
+
68
+ }
69
+
70
+ out<<q<<'\n';
71
+
72
+ }
73
+
74
+ return 0;
75
+
76
+ }
ancestors/dp_log/54a1ff1a-139a-11eb-8043-309c234925a0.cpp ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "bits/stdc++.h"
2
+
3
+
4
+ using namespace std;
5
+
6
+ ifstream fin("stramosi.in");
7
+
8
+ ofstream fout("stramosi.out");
9
+
10
+
11
+
12
+ const int NMAX = 25e4 + 3, NMAXLOG2 = log2(NMAX) + 1;
13
+
14
+ int n, m, daddy[NMAX], log2from[NMAX], pow2[NMAXLOG2], dp[NMAX][NMAXLOG2 + 2];
15
+
16
+
17
+
18
+ void read() {
19
+
20
+ fin >> n >> m;
21
+
22
+ for (int i = 1; i <= n; i++)
23
+
24
+ fin >> daddy[i];
25
+
26
+ }
27
+
28
+
29
+
30
+ void precompute() {
31
+
32
+ log2from[1] = 0;
33
+
34
+ for (int i = 2; i < NMAX; i++)
35
+
36
+ log2from[i] = log2from[i >> 1] + 1;
37
+
38
+ pow2[0] = 1;
39
+
40
+ for (int i = 1; i <= NMAXLOG2; i++)
41
+
42
+ pow2[i] = pow2[i - 1] << 1;
43
+
44
+ }
45
+
46
+
47
+
48
+ void computeDP() {
49
+
50
+ for (int i = 1; i <= n; i++)
51
+
52
+ dp[i][0] = daddy[i];
53
+
54
+ for (int i = 1; i <= n; i++)
55
+
56
+ for (int j = 1; j <= NMAXLOG2; j++)
57
+
58
+ dp[i][j] = dp[dp[i][j - 1]][j - 1];
59
+
60
+ }
61
+
62
+
63
+
64
+ void query() {
65
+
66
+ while (m--) {
67
+
68
+ int node, nrancestor;
69
+
70
+ fin >> node >> nrancestor;
71
+
72
+ while (nrancestor > 0) {
73
+
74
+ int p = log2from[nrancestor];
75
+
76
+ node = dp[node][p];
77
+
78
+ nrancestor -= pow2[p];
79
+
80
+ }
81
+
82
+ fout << node << '\n';
83
+
84
+ }
85
+
86
+ }
87
+
88
+
89
+
90
+ int main() {
91
+
92
+ precompute();
93
+
94
+ read();
95
+
96
+ computeDP();
97
+
98
+ query();
99
+
100
+ fout.close();
101
+
102
+ return 0;
103
+
104
+ }
ancestors/dp_log/54b0577e-139a-11eb-b333-309c234925a0.cpp ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include<fstream>
3
+
4
+
5
+ using namespace std;
6
+
7
+ ifstream in("stramosi.in");
8
+
9
+ ofstream out("stramosi.out");
10
+
11
+ int stramos[20][250001];
12
+
13
+ int main()
14
+
15
+ {
16
+
17
+ int n,m,p,q;
18
+
19
+ in>>n>>m;
20
+
21
+ for(int i=1;i<=n;i++)
22
+
23
+ in>>stramos[0][i];
24
+
25
+ for(int i=1;i<=18;i++)
26
+
27
+ for(int j=1;j<=n;j++){
28
+
29
+ stramos[i][j]=stramos[i-1][stramos[i-1][j]];
30
+
31
+ }
32
+
33
+ for(int i=1;i<=m;i++){
34
+
35
+ in>>q>>p;
36
+
37
+ for(int j=0;p>0;j++){
38
+
39
+ if((1<<j)&p){
40
+
41
+ q=stramos[j][q];
42
+
43
+ p-=(1<<j);
44
+
45
+ }
46
+
47
+ }
48
+
49
+
50
+
51
+ out<<q<<"\n";
52
+
53
+ }
54
+
55
+ return 0;
56
+
57
+ }
ancestors/dp_log/54beeb38-139a-11eb-8978-309c234925a0.cpp ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+
3
+
4
+ using namespace std;
5
+ int father[20 +1][250000 +1];
6
+ void precalc(int n){
7
+
8
+ int i,j;
9
+
10
+ for(j=1;j<20;j++){
11
+
12
+ for(i=1;i<=n;i++){
13
+
14
+ father[j][i]=father[j-1][father[j-1][i]];
15
+
16
+ }
17
+
18
+ }
19
+
20
+ }
21
+
22
+
23
+
24
+ int answer(int nod,int x){
25
+
26
+ int j;
27
+
28
+ for(j=0;(1<<j)<=x;j++){
29
+
30
+ if(((1<<j)&x)!=0)
31
+
32
+ nod=father[j][nod];
33
+
34
+ }
35
+
36
+ return nod;
37
+
38
+ }
39
+
40
+
41
+
42
+ int main()
43
+
44
+ {
45
+
46
+ FILE *fin,*fout;
47
+
48
+ fin=fopen("stramosi.in","r");
49
+
50
+ fout=fopen("stramosi.out","w");
51
+
52
+ int n,m,i,j,x,y;
53
+
54
+ fscanf(fin,"%d%d",&n,&m);
55
+
56
+ for(i=1;i<=n;i++){
57
+
58
+ fscanf(fin,"%d",&father[0][i]);
59
+
60
+ }
61
+
62
+
63
+
64
+ precalc(n);
65
+
66
+ for(i=1;i<=m;i++){
67
+
68
+ fscanf(fin,"%d%d",&x,&y);
69
+
70
+ fprintf(fout,"%d\n",answer(x,y));
71
+
72
+ }
73
+
74
+ fclose(fin);
75
+
76
+ fclose(fout);
77
+
78
+ return 0;
79
+
80
+ }
ancestors/dp_log/54d2f880-139a-11eb-a792-309c234925a0.cpp ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ const int MAX_N = 250000;
9
+
10
+ const int MAX_K = log2(MAX_N) + 1;
11
+
12
+
13
+
14
+ int T[MAX_K][MAX_N];
15
+
16
+
17
+
18
+ int N, M;
19
+
20
+
21
+
22
+ int main()
23
+
24
+ {
25
+
26
+ ifstream cin("stramosi.in");
27
+
28
+ ofstream cout("stramosi.out");
29
+
30
+
31
+
32
+ cin >> N >> M;
33
+
34
+
35
+
36
+ for(int i = 1; i <= N; ++i)
37
+
38
+ cin >> T[0][i];
39
+
40
+
41
+
42
+ for(int k = 1; k < MAX_K; ++k)
43
+
44
+ for(int i = 1; i <= N; ++i)
45
+
46
+ T[k][i] = T[k - 1][T[k - 1][i]];
47
+
48
+
49
+
50
+ for(int i = 1; i <= M; ++i)
51
+
52
+ {
53
+
54
+ int Q, P;
55
+
56
+ cin >> Q >> P;
57
+
58
+
59
+
60
+ int k = 0;
61
+
62
+
63
+
64
+ while(P)
65
+
66
+ {
67
+
68
+ if(P & 1) Q = T[k][Q];
69
+
70
+
71
+
72
+ ++k;
73
+
74
+ P = P >> 1;
75
+
76
+ }
77
+
78
+
79
+
80
+ cout << Q << '\n';
81
+
82
+
83
+
84
+ }
85
+
86
+ }
ancestors/dp_log/54f11f2e-139a-11eb-84f1-309c234925a0.cpp ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+ using namespace std;
3
+
4
+
5
+
6
+ ifstream f("stramosi.in");
7
+
8
+ ofstream g("stramosi.out");
9
+
10
+
11
+
12
+ const int NMAX = 250005;
13
+
14
+ const int PMAX = 19;
15
+
16
+ int dp[PMAX][NMAX], n, q, x, k, b;
17
+
18
+
19
+
20
+ int main()
21
+
22
+ {
23
+
24
+ f >> n >> q;
25
+
26
+ for(int i = 1; i <= n; i++) {
27
+
28
+ f >> dp[0][i];
29
+
30
+ }
31
+
32
+ for(int i = 1; (1 << i) <= n; i++) {
33
+
34
+ for(int j = 1; j <= n; j++) {
35
+
36
+ dp[i][j] = dp[i - 1][dp[i - 1][j]];
37
+
38
+ }
39
+
40
+ }
41
+
42
+ while(q--) {
43
+
44
+ f >> x >> k;
45
+
46
+ b = 0;
47
+
48
+ while(k) {
49
+
50
+ if(k % 2) {
51
+
52
+ x = dp[b][x];
53
+
54
+ }
55
+
56
+ b++;
57
+
58
+ k /= 2;
59
+
60
+ }
61
+
62
+ g << x << '\n';
63
+
64
+ }
65
+
66
+ return 0;
67
+
68
+ }
ancestors/dp_log/5500616e-139a-11eb-835a-309c234925a0.cpp ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <cstdio>
2
+ #include <cctype>
3
+
4
+ using namespace std;
5
+
6
+ FILE *fin = fopen( "stramosi.in", "r" ), *fout = fopen( "stramosi.out", "w" );
7
+
8
+ const int BufSz = (1 << 17);
9
+ const int Max10 = 10;
10
+
11
+ int rpos = BufSz - 1;
12
+ char rbuf[BufSz];
13
+ int p10[Max10 + 1] = { 0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
14
+ char wbuf[BufSz];
15
+ int wpos = 0;
16
+
17
+ static inline char readChar() {
18
+ if ( !(rpos = (rpos + 1) & (BufSz - 1)) ) {
19
+ fread( rbuf, 1, BufSz, fin );
20
+ }
21
+ return rbuf[rpos];
22
+ }
23
+ int readInt() {
24
+ int ch, res = 0;
25
+
26
+ while ( isspace( ch = readChar() ) );
27
+ do {
28
+ res = res * 10 + ch - '0';
29
+ } while ( isdigit( ch = readChar() ) );
30
+ return res;
31
+ }
32
+
33
+ static inline void writeChar( char ch ) {
34
+ wbuf[wpos] = ch;
35
+ if ( !(wpos = (wpos + 1) & (BufSz - 1)) ) {
36
+ fwrite( wbuf, 1, BufSz, fout );
37
+ }
38
+ }
39
+ static inline void writeInt( int x ) {
40
+ int i, cf;
41
+ i = Max10;
42
+ while ( p10[i] > x ) {
43
+ --i;
44
+ }
45
+ if ( i == 0 ) {
46
+ writeChar( '0' );
47
+ } else {
48
+ do {
49
+ cf = '0';
50
+ while ( p10[i] <= x ) {
51
+ x -= p10[i];
52
+ ++cf;
53
+ }
54
+ writeChar( cf );
55
+ } while ( --i > 0 );
56
+ }
57
+ writeChar( '\n' );
58
+ }
59
+ static inline void flushBuf() {
60
+ fwrite( wbuf, 1, wpos, fout );
61
+ }
62
+
63
+ const int MaxN = 250002;
64
+ const int MaxLog = 20;
65
+
66
+ int anc[MaxN][MaxLog];
67
+ int log[MaxN];
68
+
69
+ int main() {
70
+ int n, q, p, i, lg;
71
+
72
+ fscanf( fin, "%d%d", &n, &q );
73
+ for ( i = 1; i <= n; ++i ) {
74
+ fscanf( fin, "%d", &anc[i][0] );
75
+ }
76
+ for ( p = 1; p <= MaxLog - 1; ++p ) {
77
+ for ( i = 1; i <= n; ++i ) {
78
+ anc[i][p] = anc[anc[i][p - 1]][p - 1];
79
+ }
80
+ }
81
+ for ( i = 2; i <= n; ++i ) {
82
+ log[i] = log[i / 2] + 1;
83
+ }
84
+ while ( q-- ) {
85
+ i = readInt();
86
+ p = readInt();
87
+ lg = log[p];
88
+ while ( p ) {
89
+ i = anc[i][lg];
90
+ p -= (1 << lg);
91
+ lg = log[p];
92
+ }
93
+ writeInt( i );
94
+ }
95
+ flushBuf();
96
+ fclose( fin );
97
+ fclose( fout );
98
+ return 0;
99
+ }
ancestors/dp_log/5510b52e-139a-11eb-85a6-309c234925a0.cpp ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include<bits/stdc++.h>
2
+ using namespace std;
3
+ ifstream fin("stramosi.in");ofstream fout("stramosi.out");
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ long long t, n, m;
12
+
13
+ long long a[20][250010];
14
+
15
+
16
+
17
+ int32_t main(){
18
+
19
+ ios_base :: sync_with_stdio(false); fin.tie(NULL); fout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
20
+
21
+ fin>>n>>m;
22
+
23
+ for(long long i=1; i<=n; i++){
24
+
25
+ fin>>a[0][i];
26
+
27
+ }
28
+
29
+ for(long long j=1; j<=19; j++){
30
+
31
+ for(long long i=1; i<=n; i++){
32
+
33
+ a[j][i]=a[j-1][a[j-1][i]];
34
+
35
+ }
36
+
37
+ }
38
+
39
+
40
+
41
+ while(m--){
42
+
43
+ long long p, q;
44
+
45
+ fin>>q>>p;
46
+
47
+ while(p>0){
48
+
49
+ for(long long j=0; j<=19; j++){
50
+
51
+ if(p<(((long long)1)<<j) ){
52
+
53
+ q=a[j-1][q]; p-=(((long long)1)<<(j-1) ); break;
54
+
55
+ }
56
+
57
+ }
58
+
59
+ }
60
+
61
+ fout<<q<<"\n";
62
+
63
+ }
64
+ return 0;
65
+
66
+ }
ancestors/dp_log/551ee666-139a-11eb-bfb9-309c234925a0.cpp ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include<bits/stdc++.h>
2
+ using namespace std;
3
+ ifstream fin("stramosi.in");ofstream fout("stramosi.out");
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ long long t, n, m;
12
+
13
+ long long a[20][250010];
14
+
15
+
16
+
17
+ int32_t main(){
18
+
19
+ ios_base :: sync_with_stdio(false); fin.tie(NULL); fout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
20
+
21
+ fin>>n>>m;
22
+
23
+ for(long long i=1; i<=n; i++){
24
+
25
+ fin>>a[0][i];
26
+
27
+ }
28
+
29
+ for(long long j=1; j<=19; j++){
30
+
31
+ for(long long i=1; i<=n; i++){
32
+
33
+ a[j][i]=a[j-1][a[j-1][i]];
34
+
35
+ }
36
+
37
+ }
38
+
39
+
40
+
41
+ while(m--){
42
+
43
+ long long p, q;
44
+
45
+ fin>>q>>p;
46
+
47
+ while(p>0){
48
+
49
+ for(long long j=0; j<=19; j++){
50
+
51
+ if(p<(((long long)1)<<j) ){
52
+
53
+ q=a[j-1][q]; p-=(((long long)1)<<(j-1) ); break;
54
+
55
+ }
56
+
57
+ }
58
+
59
+ }
60
+
61
+ fout<<q<<"\n";
62
+
63
+ }
64
+ return 0;
65
+
66
+ }
ancestors/dp_log/555a6188-139a-11eb-88ba-309c234925a0.cpp ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+
5
+
6
+ using namespace std;
7
+
8
+
9
+
10
+ ifstream cin("stramosi.in");
11
+
12
+ ofstream cout("stramosi.out");
13
+
14
+ const int NMAX=250099;
15
+
16
+ long long n,m,a[30][NMAX],p,q,k;
17
+
18
+
19
+
20
+ int main(){
21
+
22
+ cin >>n>>m;
23
+
24
+ for(int i=1;i<=n;i++) cin>>a[0][i];
25
+
26
+
27
+
28
+ for(int i=1;(1<<i)<=n;i++){
29
+
30
+
31
+
32
+ for(int j=1;j<=n;j++){
33
+
34
+ a[i][j]=a[i-1][a[i-1][j]];
35
+
36
+
37
+
38
+ }
39
+
40
+ }
41
+
42
+ while(m--){
43
+
44
+ cin >>q>>p;
45
+
46
+ int flag=0;
47
+
48
+ while(p){
49
+
50
+ if(p %2==1)
51
+
52
+ q=a[flag][q];
53
+
54
+ flag++;
55
+
56
+ p/=2;
57
+
58
+ }
59
+
60
+ cout <<q<<"\n";
61
+
62
+ }
63
+
64
+ return 0;
65
+
66
+ }
ancestors/dp_log/5569ccc8-139a-11eb-928b-309c234925a0.cpp ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+
5
+
6
+ using namespace std;
7
+
8
+
9
+
10
+ ifstream cin("stramosi.in");
11
+
12
+ ofstream cout("stramosi.out");
13
+
14
+ const int NMAX=250099;
15
+
16
+ long long n,m,a[30][NMAX],p,q,k;
17
+
18
+
19
+
20
+ int main(){
21
+
22
+ cin >>n>>m;
23
+
24
+ for(int i=1;i<=n;i++) cin>>a[0][i];
25
+
26
+
27
+
28
+ for(int i=1;(1<<i)<=n;i++){
29
+
30
+ for(int j=1;j<=n;j++){
31
+
32
+ a[i][j]=a[i-1][a[i-1][j]];
33
+
34
+ }
35
+
36
+ }
37
+
38
+ while(m--){
39
+
40
+ cin >>q>>p;
41
+
42
+ int flag=0;
43
+
44
+ while(p){
45
+
46
+ if(p %2==1)
47
+
48
+ q=a[flag][q];
49
+
50
+ flag++;
51
+
52
+ p/=2;
53
+
54
+ }
55
+
56
+ cout <<q<<"\n";
57
+
58
+ }
59
+
60
+ return 0;
61
+
62
+ }
ancestors/dp_log/55795aec-139a-11eb-a6e0-309c234925a0.cpp ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+ using namespace std;
5
+
6
+ ifstream cin ("stramosi.in");
7
+
8
+ ofstream cout ("stramosi.out");
9
+
10
+ int dp[20][250000];
11
+
12
+ int main()
13
+
14
+ {
15
+
16
+ int n, m, i, j;
17
+
18
+ cin >> n >> m;
19
+
20
+ for (i = 1; i <= n; i++)
21
+
22
+ cin >> dp[0][i];
23
+
24
+ for (i = 1; (1 << i) <= n; i++)
25
+
26
+ for (j = 1; j <= n; j++)
27
+
28
+ dp[i][j] = dp[i - 1][dp[i - 1][j]];
29
+
30
+ for (i = 1; i <= m; i++)
31
+
32
+ {
33
+
34
+ int p, q;
35
+
36
+ cin >> q >> p;
37
+
38
+ int bit = 0;
39
+
40
+ while (p)
41
+
42
+ {
43
+
44
+ if (p % 2 == 1)
45
+
46
+ q = dp[bit][q];
47
+
48
+ bit++;
49
+
50
+ p /= 2;
51
+
52
+ }
53
+
54
+ cout << q << '\n';
55
+
56
+ }
57
+
58
+ return 0;
59
+
60
+ }
ancestors/dp_log/55896192-139a-11eb-8f27-309c234925a0.cpp ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+ using namespace std;
3
+
4
+ ifstream r("stramosi.in");
5
+
6
+ ofstream w("stramosi.out");
7
+
8
+ int n,m,stramos[20][250002];
9
+
10
+ int main()
11
+
12
+ {
13
+
14
+ r>>n>>m;
15
+
16
+ for(int i=1; i<=n; i++){
17
+
18
+ r>>stramos[0][i];
19
+
20
+ }
21
+
22
+ for(int i=1,p=2; p<=n; i++,p*=2){
23
+
24
+ for(int j=1; j<=n; j++){
25
+
26
+ stramos[i][j]=stramos[i-1][stramos[i-1][j]];
27
+
28
+ }
29
+
30
+ }
31
+
32
+ while(m--)
33
+
34
+ {
35
+
36
+ int p,q;
37
+
38
+ r>>q>>p;
39
+
40
+ while(p!=0)
41
+
42
+ {
43
+
44
+ int k=__builtin_ctz(p);
45
+
46
+ q=stramos[k][q];
47
+
48
+ p-=1<<k;
49
+
50
+ }
51
+
52
+ w<<q<<"\n";
53
+
54
+ }
55
+
56
+ return 0;
57
+
58
+ }
ancestors/dp_log/5598cae4-139a-11eb-af18-309c234925a0.cpp ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+ using namespace std;
3
+
4
+ ifstream fin("stramosi.in");
5
+ ofstream fout("stramosi.out");
6
+ int n, m;
7
+
8
+
9
+ int rmq[18][250010];
10
+ int logaritmi[250010];
11
+
12
+ void init() {
13
+ logaritmi[1] = 0;
14
+ for(int i=2; i <= n; i++) {
15
+ logaritmi[i] = logaritmi[i / 2] + 1;
16
+ }
17
+
18
+ for(int lungime = 1; (1 << lungime) <= n; lungime++) {
19
+ for(int el = 1; el <= n; el++) {
20
+ rmq[lungime][el] = rmq[lungime-1][rmq[lungime-1][el]];
21
+ }
22
+ }
23
+ }
24
+
25
+
26
+ void solve() {
27
+ fin >> n >> m;
28
+ for(int i=1; i <= n; i++) {
29
+ fin >> rmq[0][i];
30
+ }
31
+ init();
32
+
33
+ while(m--) {
34
+ int a, b;
35
+ fin >> a >> b;
36
+ if(b == 0) {
37
+ fout << a << "\n";
38
+ } else {
39
+ while(b > 0) {
40
+ a = rmq[logaritmi[b & (-b)]][a];
41
+ b -= b & (-b);
42
+ }
43
+
44
+ fout << a << "\n";
45
+ }
46
+ }
47
+ }
48
+
49
+
50
+ int main() {
51
+ ios_base::sync_with_stdio(0);
52
+ fin .tie(0);
53
+ fout.tie(0);
54
+
55
+ int testCases = 1;
56
+
57
+ while(testCases--) {
58
+ solve();
59
+ }
60
+ }
ancestors/dp_log/55ab4176-139a-11eb-a30f-309c234925a0.cpp ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+ using namespace std;
3
+
4
+ ifstream fin("stramosi.in");
5
+ ofstream fout("stramosi.out");
6
+
7
+ int rmq[19][250001];
8
+
9
+
10
+ inline int lsb(int x)
11
+ {
12
+ return x&(-x);
13
+ }
14
+
15
+ inline int log(int x)
16
+ {
17
+ int nr = 0;
18
+ while (x > 1)
19
+ {
20
+ x = x>>1;
21
+ nr++;
22
+ }
23
+ return nr;
24
+ }
25
+
26
+ int main()
27
+ {
28
+ int n, i, m, q, p, j;
29
+ fin >> n >> m;
30
+ for (i = 1; i<=n; i++)
31
+ fin >> rmq[0][i];
32
+ for (i = 1; (1<<i)<n; i++)
33
+ for (j = 1; j<=n; j++)
34
+ rmq[i][j] = rmq[i-1][rmq[i-1][j]];
35
+ for (i = 1; i<=m; i++)
36
+ {
37
+ fin >> q >> p;
38
+
39
+ if (p == 0)
40
+ {
41
+ fout << q << '\n';
42
+ continue;
43
+ }
44
+ while (p > 0)
45
+ {
46
+ q = rmq[log(lsb(p))][q];
47
+ p = p - lsb(p);
48
+ }
49
+ fout << q << '\n';
50
+ }
51
+ return 0;
52
+ }
ancestors/dp_log/55e78718-139a-11eb-84d2-309c234925a0.cpp ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+ ifstream f("stramosi.in");
7
+
8
+ ofstream g("stramosi.out");
9
+
10
+ const int N = 19;
11
+
12
+ const int M = 250001;
13
+
14
+ int n,m,stramos[N][M];
15
+
16
+ int main()
17
+
18
+ {
19
+
20
+ f>>n>>m;
21
+
22
+ for(int i=1; i<=n; i++)
23
+
24
+ f>>stramos[0][i];
25
+
26
+ for(int i=1,p=2;p<=n;i++,p*=2)
27
+
28
+ for(int j=1;j<=n;j++)
29
+
30
+ stramos[i][j]=stramos[i-1][stramos[i-1][j]];
31
+
32
+ for(;m;m--)
33
+
34
+ {
35
+
36
+ int p,q;
37
+
38
+ f>>q>>p;
39
+
40
+ while(p)
41
+
42
+ {
43
+
44
+ int k=__builtin_ctz(p);
45
+
46
+ q=stramos[k][q];
47
+
48
+ p-=1<<k;
49
+
50
+ }
51
+
52
+ g<<q<<'\n';
53
+
54
+ }
55
+
56
+ return 0;
57
+
58
+ }
ancestors/dp_log/560a78e8-139a-11eb-9736-309c234925a0.cpp ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+ ifstream f("stramosi.in");
7
+
8
+ ofstream g("stramosi.out");
9
+
10
+ const int N = 19;
11
+
12
+ const int M = 250001;
13
+
14
+ int n,m,stramos[N][M];
15
+
16
+ int main()
17
+
18
+ {
19
+
20
+ f>>n>>m;
21
+
22
+ for(int i=1; i<=n; i++)
23
+
24
+ f>>stramos[0][i];
25
+
26
+ for(int i=1,p=2;p<=n;i++,p*=2)
27
+
28
+ for(int j=1;j<=n;j++)
29
+
30
+ stramos[i][j]=stramos[i-1][stramos[i-1][j]];
31
+
32
+ for(;m;m--)
33
+
34
+ {
35
+
36
+ int p,q;
37
+
38
+ f>>q>>p;
39
+
40
+ while(p)
41
+
42
+ {
43
+
44
+ int k=__builtin_ctz(p);
45
+
46
+ q=stramos[k][q];
47
+
48
+ p-=1<<k;
49
+
50
+ }
51
+
52
+ g<<q<<'\n';
53
+
54
+ }
55
+
56
+ return 0;
57
+
58
+ }
ancestors/dp_log/56188240-139a-11eb-bfed-309c234925a0.cpp ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include<ext/pb_ds/assoc_container.hpp>
2
+ #include<ext/pb_ds/tree_policy.hpp>
3
+ #include <bits/stdc++.h>
4
+ using ull = unsigned long long;
5
+ using ll = long long;
6
+ using namespace std;
7
+
8
+ template<typename T>
9
+ ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
10
+ template<typename A, typename B>
11
+ ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
12
+
13
+ const int INF = INT_MAX, maxN = 250010;
14
+ int t, n, m, q, p, dp[maxN][21];
15
+
16
+
17
+
18
+ int query(int nod, int k){
19
+
20
+ int bit = 0;
21
+
22
+ while (k > 0) {
23
+
24
+ if (k & 1) {
25
+
26
+ nod = dp[nod][bit];
27
+
28
+ if (!nod) return 0;
29
+
30
+ }
31
+
32
+ ++bit;
33
+
34
+ k >>= 1;
35
+
36
+ }
37
+
38
+ return nod;
39
+
40
+ }
41
+
42
+
43
+ int main(){
44
+
45
+ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
46
+ ifstream cin("stramosi.in");
47
+ ofstream cout("stramosi.out");
48
+
49
+
50
+ cin >> n >> m;
51
+
52
+ for (int i = 1; i <= n; i++){
53
+
54
+ cin >> dp[i][0];
55
+
56
+ }
57
+
58
+ for (int i = 1; i <= n; i++){
59
+
60
+ for (int j = 1; j <= 20; j++){
61
+
62
+ dp[i][j] = dp[dp[i][j - 1]][j - 1];
63
+
64
+ }
65
+
66
+ }
67
+
68
+
69
+
70
+ while (m--){
71
+
72
+ cin >> q >> p;
73
+
74
+ cout << query(q, p) << "\n";
75
+
76
+ }
77
+
78
+ return 0;
79
+ }
ancestors/dp_log/562c0a2c-139a-11eb-a615-309c234925a0.cpp ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+ using namespace std;
4
+
5
+
6
+
7
+ int n, m, dp[19][250010], q, p, r, rez;
8
+
9
+
10
+
11
+ int main()
12
+
13
+ {
14
+
15
+ ifstream f("stramosi.in");
16
+
17
+ ofstream g("stramosi.out");
18
+
19
+ f>>n>>m;
20
+
21
+ int i, j;
22
+
23
+ for(i=1; i<=n; i++)
24
+
25
+ f>>dp[0][i];
26
+
27
+ for(j=1; j<=18; j++)
28
+
29
+ for(i = 1; i <= n; i++)
30
+
31
+ dp[j][i] = dp[j - 1][dp[j - 1][i]];
32
+
33
+ for(int i=1; i<=m; i++)
34
+
35
+ {
36
+
37
+ f>>q>>p;
38
+
39
+ r = 0;
40
+
41
+ rez = q;
42
+
43
+ while(p != 0)
44
+
45
+ {
46
+
47
+ if(p % 2 != 0)
48
+
49
+ rez = dp[r][rez];
50
+
51
+ p = p >> 1;
52
+
53
+ r++;
54
+
55
+ }
56
+
57
+ g<<rez<<'\n';
58
+
59
+ }
60
+
61
+ }
ancestors/dp_log/563eceda-139a-11eb-948b-309c234925a0.cpp ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include<ext/pb_ds/assoc_container.hpp>
2
+ #include<ext/pb_ds/tree_policy.hpp>
3
+ #include <bits/stdc++.h>
4
+ using ull = unsigned long long;
5
+ using ll = long long;
6
+ using namespace std;
7
+
8
+ template<typename T>
9
+ ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
10
+ template<typename A, typename B>
11
+ ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
12
+
13
+ const int INF = INT_MAX, maxN = 250010;
14
+ int t, n, m, q, p, dp[maxN][21];
15
+
16
+
17
+
18
+ int query(int nod, int k){
19
+
20
+ int bit = 0;
21
+
22
+ while (k > 0) {
23
+
24
+ if (k & 1) {
25
+
26
+ nod = dp[nod][bit];
27
+
28
+ if (!nod) return 0;
29
+
30
+ }
31
+
32
+ ++bit;
33
+
34
+ k >>= 1;
35
+
36
+ }
37
+
38
+ return nod;
39
+
40
+ }
41
+
42
+
43
+ int main(){
44
+
45
+ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
46
+ ifstream cin("stramosi.in");
47
+ ofstream cout("stramosi.out");
48
+
49
+
50
+ cin >> n >> m;
51
+
52
+ for (int i = 1; i <= n; i++){
53
+
54
+ cin >> dp[i][0];
55
+
56
+ }
57
+
58
+ for (int i = 1; i <= n; i++){
59
+
60
+ for (int j = 1; j <= 20; j++){
61
+
62
+ dp[i][j] = dp[dp[i][j - 1]][j - 1];
63
+
64
+ }
65
+
66
+ }
67
+
68
+
69
+
70
+ while (m--){
71
+
72
+ cin >> q >> p;
73
+
74
+ cout << query(q, p) << "\n";
75
+
76
+ }
77
+
78
+ return 0;
79
+ }
ancestors/dp_log/564da036-139a-11eb-89a6-309c234925a0.cpp ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <stdio.h>
3
+
4
+
5
+ using namespace std;
6
+
7
+
8
+
9
+ const int NMAX = 250005;
10
+
11
+ int t[20][NMAX];
12
+
13
+
14
+
15
+ int main() {
16
+
17
+
18
+
19
+ freopen ("stramosi.in", "r", stdin);
20
+
21
+ freopen ("stramosi.out", "w", stdout);
22
+
23
+
24
+
25
+ int n, teste, i, q, p, j, w;
26
+
27
+
28
+
29
+ scanf ("%d%d", &n, &teste);
30
+
31
+ for (i = 1; i <= n; i++)
32
+
33
+ scanf ("%d", &t[0][i]);
34
+
35
+
36
+
37
+ for (i = 1; 1 << i <= n; i++) {
38
+
39
+ for (j = 1; j <= n; j++)
40
+
41
+ t[i][j] = t[i - 1][t[i - 1][j]];
42
+
43
+ }
44
+
45
+
46
+
47
+ for (w = 1; w <= teste; w++) {
48
+
49
+ scanf ("%d%d", &q, &p);
50
+
51
+ for (i = 0; i <= 17; i++)
52
+
53
+ if (((1 << i) & p) != 0) {
54
+
55
+ q = t[i][q];
56
+
57
+ }
58
+
59
+ printf ("%d\n", q);
60
+
61
+
62
+
63
+ }
64
+
65
+
66
+
67
+ return 0;
68
+
69
+ }
ancestors/dp_log/565d7eba-139a-11eb-927a-309c234925a0.cpp ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+ using namespace std;
4
+
5
+
6
+
7
+ int n, m, dp[19][250010], q, p, r, rez;
8
+
9
+
10
+
11
+ int main()
12
+
13
+ {
14
+
15
+ ifstream f("stramosi.in");
16
+
17
+ ofstream g("stramosi.out");
18
+
19
+ f>>n>>m;
20
+
21
+ int i, j;
22
+
23
+ for(i=1; i<=n; i++)
24
+
25
+ f>>dp[0][i];
26
+
27
+ for(j=1; j<=18; j++)
28
+
29
+ for(i = 1; i <= n; i++)
30
+
31
+ dp[j][i] = dp[j - 1][dp[j - 1][i]];
32
+
33
+ for(int i=1; i<=m; i++)
34
+
35
+ {
36
+
37
+ f>>q>>p;
38
+
39
+ r = 0;
40
+
41
+ rez = q;
42
+
43
+ while(p != 0)
44
+
45
+ {
46
+
47
+ if(p % 2 != 0)
48
+
49
+ rez = dp[r][rez];
50
+
51
+ p = p >> 1;
52
+
53
+ r++;
54
+
55
+ }
56
+
57
+ g<<rez<<'\n';
58
+
59
+ }
60
+
61
+ }
ancestors/dp_log/56961ad8-139a-11eb-8038-309c234925a0.cpp ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+
4
+
5
+
6
+
7
+ using namespace std;
8
+
9
+
10
+
11
+ ifstream f("stramosi.in");
12
+
13
+ ofstream g("stramosi.out");
14
+
15
+
16
+
17
+ int n, m, x, y;
18
+
19
+ int tt[250005];
20
+
21
+ int dp[20][250005];
22
+
23
+
24
+
25
+ int main()
26
+
27
+ {
28
+
29
+ f >> n >> m;
30
+
31
+ for (int i = 1; i <= n; i++)
32
+
33
+ {
34
+
35
+ f >> dp[0][i];
36
+
37
+ }
38
+
39
+
40
+
41
+ for (int i = 1; i <= 18; i++)
42
+
43
+ {
44
+
45
+ for (int j = 1; j <= n; j++)
46
+
47
+ dp[i][j]=dp[i-1][dp[i-1][j]];
48
+
49
+ }
50
+
51
+
52
+
53
+ while (m--)
54
+
55
+ {
56
+
57
+ f >> x >> y;
58
+
59
+
60
+
61
+ for (int i = 0; i <= 18; i++)
62
+
63
+ if (y&(1<<i)) x=dp[i][x];
64
+
65
+ g << x << '\n';
66
+
67
+ }
68
+
69
+
70
+
71
+ return 0;
72
+
73
+ }
ancestors/dp_log/56b8e524-139a-11eb-8016-309c234925a0.cpp ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ ifstream f("stramosi.in");
9
+
10
+ ofstream g("stramosi.out");
11
+
12
+
13
+
14
+ int n, m, i, j, k, s, sol, stramos[20][250005];
15
+
16
+
17
+
18
+ int main()
19
+
20
+ {
21
+
22
+ f >> n >> m;
23
+
24
+ for(i = 1; i <= n; i++)
25
+
26
+ f >> stramos[0][i];
27
+
28
+ for(i = 1; (1 << i) < n; i++)
29
+
30
+ for(j = 1; j <= n; j++)
31
+
32
+ stramos[i][j] = stramos[i - 1][stramos[i - 1][j]];
33
+
34
+
35
+
36
+ while(m)
37
+
38
+ {
39
+
40
+ f >> k >> s;
41
+
42
+
43
+
44
+ sol = k;
45
+
46
+ for(i = 17; i >= 0; i--)
47
+
48
+ if((1 << i) <= s)
49
+
50
+ {
51
+
52
+ sol = stramos[i][sol];
53
+
54
+ s -= (1 << i);
55
+
56
+ }
57
+
58
+
59
+
60
+ g << sol << '\n';
61
+
62
+
63
+
64
+ m--;
65
+
66
+ }
67
+
68
+
69
+
70
+ f.close();
71
+
72
+ g.close();
73
+
74
+
75
+
76
+ return 0;
77
+
78
+ }
ancestors/dp_log/56c84e86-139a-11eb-8425-309c234925a0.cpp ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+
4
+
5
+ using namespace std;
6
+
7
+
8
+
9
+ ifstream in ("stramosi.in");
10
+
11
+ ofstream out ("stramosi.out");
12
+
13
+
14
+
15
+ const int N=250001;
16
+
17
+ const int M=350002;
18
+
19
+
20
+
21
+ int t[18][N];
22
+
23
+ int main()
24
+
25
+ {
26
+
27
+ int n,m,p,q;
28
+
29
+ in>>n>>m;
30
+
31
+ for (int i=1;i<=n;i++)
32
+
33
+ in>>t[0][i];
34
+
35
+ for (int i=1;i<18;i++)
36
+
37
+ for (int j=1;j<=n;j++)
38
+
39
+ t[i][j]=t[i-1][t[i-1][j]];
40
+
41
+ for (int k=1;k<=m;k++)
42
+
43
+ {
44
+
45
+ in>>q>>p;
46
+
47
+ int ans=q;
48
+
49
+ int i=0;
50
+
51
+ while (p)
52
+
53
+ {
54
+
55
+ if (p%2!=0) ans=t[i][ans];
56
+
57
+ p/=2;
58
+
59
+ i++;
60
+
61
+ }
62
+
63
+ out<<ans<<'\n';
64
+
65
+ }
66
+
67
+ return 0;
68
+
69
+ }
ancestors/dp_log/56d9176c-139a-11eb-988a-309c234925a0.cpp ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+
4
+
5
+ using namespace std;
6
+
7
+
8
+
9
+ ifstream in("stramosi.in");
10
+
11
+ ofstream out("stramosi.out");
12
+
13
+
14
+
15
+ const int N = 2500001;
16
+
17
+ const int L = 18;
18
+
19
+ int n, m, p, q, rez, t[L][N];
20
+
21
+
22
+
23
+ int main() {
24
+
25
+ in >> n >> m;
26
+
27
+ for (int i = 1; i <= n; i++)
28
+
29
+ in >> t[0][i];
30
+
31
+ for (int i = 1; i < L; i++)
32
+
33
+ for (int j = 1; j <= n; j++)
34
+
35
+ t[i][j] = t[i - 1][t[i - 1][j]];
36
+
37
+ while (m) {
38
+
39
+ int i = 0;
40
+
41
+ m--;
42
+
43
+ in >> q >> p;
44
+
45
+ rez = q;
46
+
47
+ while (p) {
48
+
49
+ if (p % 2 != 0) rez = t[i][rez];
50
+
51
+ p /= 2;
52
+
53
+ i++;
54
+
55
+ }
56
+
57
+ out << rez << '\n';
58
+
59
+ }
60
+
61
+ return 0;
62
+
63
+ }
ancestors/dp_log/56e80b76-139a-11eb-b7b3-309c234925a0.cpp ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+
4
+
5
+ using namespace std;
6
+
7
+
8
+
9
+ ifstream in("stramosi.in");
10
+
11
+ ofstream out("stramosi.out");
12
+
13
+
14
+
15
+ const int N = 2500001;
16
+
17
+ const int L = 18;
18
+
19
+ int n, m, p, q, rez, t[L][N];
20
+
21
+
22
+
23
+ int main() {
24
+
25
+ in >> n >> m;
26
+
27
+ for (int i = 1; i <= n; i++)
28
+
29
+ in >> t[0][i];
30
+
31
+ for (int i = 1; i < L; i++)
32
+
33
+ for (int j = 1; j <= n; j++)
34
+
35
+ t[i][j] = t[i - 1][t[i - 1][j]];
36
+
37
+ while (m) {
38
+
39
+ int i = 0;
40
+
41
+ m--;
42
+
43
+ in >> q >> p;
44
+
45
+ rez = q;
46
+
47
+ while (p) {
48
+
49
+ if (p % 2 != 0) rez = t[i][rez];
50
+
51
+ p /= 2;
52
+
53
+ i++;
54
+
55
+ }
56
+
57
+ out << rez << '\n';
58
+
59
+ }
60
+
61
+ return 0;
62
+
63
+ }
ancestors/dp_log/56f63c48-139a-11eb-a94f-309c234925a0.cpp ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ ifstream fin("stramosi.in");
9
+
10
+ ofstream fout("stramosi.out");
11
+
12
+
13
+
14
+ const int maxi=250001;
15
+
16
+ int d[18][maxi];
17
+
18
+
19
+
20
+ int main()
21
+
22
+ {
23
+
24
+ ios::sync_with_stdio(false);
25
+
26
+ int n,m,i,j,q,p;
27
+
28
+ fin>>n>>m;
29
+
30
+ for(i=1; i<=n; i++)
31
+
32
+ fin>>d[0][i];
33
+
34
+ for(i=1; i<18; i++)
35
+
36
+ for(j=1; j<=n; j++)
37
+
38
+ d[i][j]=d[i-1][d[i-1][j]];
39
+
40
+
41
+
42
+ for(i=0; i<m; i++)
43
+
44
+ {
45
+
46
+ fin>>q>>p;
47
+
48
+ j=0;
49
+
50
+ while(p!=0)
51
+
52
+ {
53
+
54
+ if(p%2!=0)
55
+
56
+ q=d[j][q];
57
+
58
+ p/=2;
59
+
60
+ j++;
61
+
62
+ }
63
+
64
+ fout<<q<<'\n';
65
+
66
+ }
67
+
68
+ fin.close();
69
+
70
+ fout.close();
71
+
72
+ return 0;
73
+
74
+ }
ancestors/dp_log/5705ccb4-139a-11eb-9fe8-309c234925a0.cpp ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ ifstream f("stramosi.in");
9
+
10
+ ofstream g("stramosi.out");
11
+
12
+
13
+
14
+ int n, m, dp[19][250010], q, p, r, rez;
15
+
16
+
17
+
18
+ int main() {
19
+
20
+ f>>n>>m;
21
+
22
+ for(int i = 1; i <= n; i++)
23
+
24
+ f>>dp[0][i];
25
+
26
+ for(int j = 1; j <= 18; j++)
27
+
28
+ for(int i = 1; i <= n; i++)
29
+
30
+ dp[j][i] = dp[j - 1][dp[j - 1][i]];
31
+
32
+
33
+
34
+ for(int i = 1; i <= m; i++) {
35
+
36
+ f>>q>>p;
37
+
38
+ r = 0;
39
+
40
+ rez = q;
41
+
42
+
43
+
44
+ while(p != 0) {
45
+
46
+ if(p % 2 != 0)
47
+
48
+ rez = dp[r][rez];
49
+
50
+ p = p >> 1;
51
+
52
+ r++;
53
+
54
+ }
55
+
56
+ g<<rez<<'\n';
57
+
58
+ }
59
+
60
+ }
ancestors/dp_log/571758e2-139a-11eb-83aa-309c234925a0.cpp ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include<bits/stdc++.h>
2
+
3
+
4
+
5
+
6
+
7
+
8
+ using namespace std;
9
+
10
+ int n,dp[250010][18 +1],q;
11
+
12
+ int main()
13
+
14
+ {
15
+
16
+ ifstream fin("stramosi.in");
17
+
18
+ ofstream fout("stramosi.out");
19
+
20
+ fin>>n>>q;
21
+
22
+ for(int i=1;i<=n;++i)
23
+
24
+ fin>>dp[i][0];
25
+
26
+ for(int i=1;i<=n;++i)
27
+
28
+ for(int j=1;j<=18;++j)
29
+
30
+ dp[i][j]=dp[dp[i][j-1]][j-1];
31
+
32
+ while(q--)
33
+
34
+ {
35
+
36
+ int d,p;
37
+
38
+ fin>>d>>p;
39
+
40
+ for(int i=18;i>=0;--i)
41
+
42
+ if(p & (1<<i))
43
+
44
+ d=dp[d][i];
45
+
46
+ fout<<d<<'\n';
47
+
48
+ }
49
+
50
+ fin.close();
51
+
52
+ fout.close();
53
+
54
+ }
ancestors/dp_log/57264cf4-139a-11eb-84cc-309c234925a0.cpp ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ ifstream cin("stramosi.in");
9
+
10
+ ofstream cout("stramosi.out");
11
+ int v[100][150];
12
+
13
+ int d[18][2500005];
14
+
15
+
16
+
17
+ int main()
18
+
19
+ {
20
+
21
+
22
+
23
+ int n, t;
24
+
25
+ cin >> n >> t;
26
+
27
+
28
+
29
+ for (int i = 1; i <= n; i++)
30
+
31
+ cin >> d[0][i];
32
+
33
+
34
+
35
+ for (int i = 1; i < 18; i++)
36
+
37
+ for (int j = 1; j <= n; j++)
38
+
39
+ d[i][j] = d[i - 1][d[i - 1][j]];
40
+
41
+
42
+
43
+ int ans = 0;
44
+
45
+ int x, y, k = 0;
46
+
47
+ while(t--)
48
+
49
+ {
50
+
51
+ k = 0;
52
+
53
+ cin >> x >> y;
54
+
55
+
56
+
57
+ ans = x;
58
+
59
+
60
+
61
+ while(y)
62
+
63
+ {
64
+
65
+ if (y % 2 != 0)
66
+
67
+ ans = d[k][ans];
68
+
69
+ y /= 2;
70
+
71
+ k++;
72
+
73
+ }
74
+
75
+
76
+
77
+ cout << ans << "\n";
78
+
79
+ }
80
+
81
+
82
+
83
+ return 0;
84
+
85
+ }
ancestors/dp_log/5735b646-139a-11eb-847e-309c234925a0.cpp ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+
4
+ using namespace std;
5
+
6
+ const int NMAX = 250000;
7
+ const int LOGMAX = 18;
8
+
9
+ ifstream fin("stramosi.in");
10
+ ofstream fout("stramosi.out");
11
+
12
+ int dp[LOGMAX + 1][NMAX + 1];
13
+
14
+ int main()
15
+ {
16
+ int n,m,i,j,p,q,ans;
17
+ fin >> n >> m;
18
+ for(i=1; i<=n; i++)
19
+ fin >> dp[0][i];
20
+ for(i=1; i<=LOGMAX; i++)
21
+ for(j=1; j<=n; j++)
22
+ dp[i][j] = dp[i-1][dp[i-1][j]];
23
+ while(m--){
24
+ fin >> q >> p;
25
+ ans = q;
26
+ j = 0;
27
+ while(p){
28
+ if(p % 2)
29
+ ans = dp[j][ans];
30
+ p /= 2;
31
+ j++;
32
+ }
33
+ fout << ans << "\n";
34
+ }
35
+ return 0;
36
+ }
ancestors/dp_log/57482cd8-139a-11eb-8c39-309c234925a0.cpp ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+ using namespace std;
4
+
5
+
6
+
7
+ ifstream in("stramosi.in");
8
+
9
+ ofstream out("stramosi.out");
10
+
11
+
12
+
13
+ int v[20][250001], n, m;
14
+
15
+
16
+
17
+ int main()
18
+
19
+ {
20
+
21
+ in >> n >> m;
22
+
23
+ for(int i = 1; i <= n; i++)
24
+
25
+ {
26
+
27
+ in >> v[0][i];
28
+
29
+ }
30
+
31
+ for(int i = 1; i <= 18; i++)
32
+
33
+ {
34
+
35
+ for(int j = 1; j <= n; j++)
36
+
37
+ {
38
+
39
+ v[i][j] = v[i-1][v[i-1][j]];
40
+
41
+ }
42
+
43
+ }
44
+
45
+ while(m)
46
+
47
+ {
48
+
49
+ m --;
50
+
51
+ int p, q, ans, j = 0;
52
+
53
+ in >> q >> p;
54
+
55
+ ans = q;
56
+
57
+ while(p)
58
+
59
+ {
60
+
61
+ if(p%2 != 0)
62
+
63
+ {
64
+
65
+ ans = v[j][ans];
66
+
67
+ }
68
+
69
+ p /= 2;
70
+
71
+ j++;
72
+
73
+ }
74
+
75
+ out << ans << "\n";
76
+
77
+ }
78
+
79
+ return 0;
80
+
81
+ }
ancestors/dp_log/575747f8-139a-11eb-bd5d-309c234925a0.cpp ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+
4
+
5
+ using namespace std;
6
+
7
+
8
+
9
+ ifstream in("stramosi.in");
10
+
11
+ ofstream out("stramosi.out");
12
+
13
+
14
+
15
+ const int maxx=250001;
16
+
17
+
18
+
19
+
20
+
21
+ int main()
22
+
23
+ {
24
+
25
+ int v[18][maxx];
26
+
27
+ ios::sync_with_stdio(false);
28
+
29
+ int n,m,i,j,q,p;
30
+
31
+ in>>n>>m;
32
+
33
+ for(i=1; i<=n; i++)
34
+
35
+ {
36
+
37
+ in>>v[0][i];
38
+
39
+ }
40
+
41
+ for(i=1; i<18; i++)
42
+
43
+ {
44
+
45
+ for(j=1; j<=n; j++)
46
+
47
+ {
48
+
49
+ v[i][j]=v[i-1][v[i-1][j]];
50
+
51
+ }
52
+
53
+ }
54
+
55
+ for(i=0; i<m; i++)
56
+
57
+ {
58
+
59
+ in>>q>>p;
60
+
61
+ j=0;
62
+
63
+ while(p!=0)
64
+
65
+ {
66
+
67
+ if(p%2!=0)
68
+
69
+ q=v[j][q];
70
+
71
+ p/=2;
72
+
73
+ j++;
74
+
75
+ }
76
+
77
+ out<<q<<'\n';
78
+
79
+ }
80
+
81
+ return 0;
82
+
83
+ }
ancestors/dp_log/57ab1924-139a-11eb-b836-309c234925a0.cpp ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ ifstream fin("stramosi.in");
9
+
10
+ ofstream fout("stramosi.out");
11
+
12
+
13
+
14
+ const int NMAX = 250000;
15
+
16
+ const int LOGMAX = 18;
17
+
18
+
19
+
20
+ int t[LOGMAX + 1][NMAX + 1];
21
+
22
+
23
+
24
+ int main() {
25
+
26
+ int n, m;
27
+
28
+ fin>>n>>m;
29
+
30
+ for( int i = 1; i <= n; i ++ )
31
+
32
+ fin>>t[0][i];
33
+
34
+ for( int dim = 1; (1<<dim) <= n; dim ++ )
35
+
36
+ for( int i = 1; i <= n; i ++ )
37
+
38
+ t[dim][i] = t[dim - 1][t[dim - 1][i]];
39
+
40
+ while( m -- ) {
41
+
42
+ int node, depth;
43
+
44
+ fin>>node>>depth;
45
+
46
+ int pas;
47
+
48
+ for( pas = 1; (1<<pas) <= depth; pas ++ );
49
+
50
+ int level = 0;
51
+
52
+ while( pas ) {
53
+
54
+ pas --;
55
+
56
+ if( level + (1<<pas) <= depth )
57
+
58
+ level += (1<<(pas)), node = t[pas][node];
59
+
60
+ }
61
+
62
+ fout<<node<<"\n";
63
+
64
+ }
65
+
66
+ return 0;
67
+
68
+ }
ancestors/dp_log/57bacb8c-139a-11eb-b5a6-309c234925a0.cpp ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+ using namespace std;
4
+
5
+ ifstream fin("stramosi.in");
6
+ ofstream fout("stramosi.out");
7
+
8
+
9
+
10
+ int n, m, dp[19][250005], q, p, r, rez;
11
+
12
+ int main() {
13
+ fin >> n >> m;
14
+ for(int i = 1; i <= n; ++i)
15
+ fin >> dp[0][i];
16
+ for(int j = 1; j <= 18; ++j)
17
+ for(int i = 1; i <= n; ++i)
18
+ dp[j][i] = dp[j - 1][dp[j - 1][i]];
19
+
20
+ for(int i = 1; i <= m; ++i) {
21
+ fin >> q >> p;
22
+ r = 0; rez = q;
23
+
24
+ while(p) {
25
+ if(p % 2) rez = dp[r][rez];
26
+ p >>= 1; ++r;
27
+ }
28
+
29
+ fout << rez << '\n';
30
+ }
31
+ }
ancestors/dp_log/57cab226-139a-11eb-8e00-309c234925a0.cpp ADDED
@@ -0,0 +1,316 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+ int n, Q, x, c, p, t[250005][18];
7
+
8
+ void build()
9
+
10
+ {
11
+
12
+ for(int i=1; i<=17; i++)
13
+
14
+ {
15
+
16
+ for(int nod=1; nod<=n; nod++)
17
+
18
+ t[nod][i] = t[t[nod][i-1]][i-1];
19
+
20
+ }
21
+
22
+ }
23
+
24
+ class InParser {
25
+
26
+ private:
27
+
28
+ FILE *fin;
29
+
30
+ char *buff;
31
+
32
+ int sp;
33
+
34
+
35
+
36
+ char read_ch() {
37
+
38
+ ++sp;
39
+
40
+ if (sp == 4096) {
41
+
42
+ sp = 0;
43
+
44
+ fread(buff, 1, 4096, fin);
45
+
46
+ }
47
+
48
+ return buff[sp];
49
+
50
+ }
51
+
52
+
53
+
54
+ public:
55
+
56
+ InParser(const char* nume) {
57
+
58
+ fin = fopen(nume, "r");
59
+
60
+ buff = new char[4096]();
61
+
62
+ sp = 4095;
63
+
64
+ }
65
+
66
+
67
+
68
+ InParser& operator >> (int &n) {
69
+
70
+ char c;
71
+
72
+ while (!isdigit(c = read_ch()) && c != '-');
73
+
74
+ int sgn = 1;
75
+
76
+ if (c == '-') {
77
+
78
+ n = 0;
79
+
80
+ sgn = -1;
81
+
82
+ } else {
83
+
84
+ n = c - '0';
85
+
86
+ }
87
+
88
+ while (isdigit(c = read_ch())) {
89
+
90
+ n = 10 * n + c - '0';
91
+
92
+ }
93
+
94
+ n *= sgn;
95
+
96
+ return *this;
97
+
98
+ }
99
+
100
+
101
+
102
+ InParser& operator >> (long long &n) {
103
+
104
+ char c;
105
+
106
+ n = 0;
107
+
108
+ while (!isdigit(c = read_ch()) && c != '-');
109
+
110
+ long long sgn = 1;
111
+
112
+ if (c == '-') {
113
+
114
+ n = 0;
115
+
116
+ sgn = -1;
117
+
118
+ } else {
119
+
120
+ n = c - '0';
121
+
122
+ }
123
+
124
+ while (isdigit(c = read_ch())) {
125
+
126
+ n = 10 * n + c - '0';
127
+
128
+ }
129
+
130
+ n *= sgn;
131
+
132
+ return *this;
133
+
134
+ }
135
+
136
+ };
137
+
138
+ class OutParser {
139
+
140
+ private:
141
+
142
+ FILE *fout;
143
+
144
+ char *buff;
145
+
146
+ int sp;
147
+
148
+
149
+
150
+ void write_ch(char ch) {
151
+
152
+ if (sp == 50000) {
153
+
154
+ fwrite(buff, 1, 50000, fout);
155
+
156
+ sp = 0;
157
+
158
+ buff[sp++] = ch;
159
+
160
+ } else {
161
+
162
+ buff[sp++] = ch;
163
+
164
+ }
165
+
166
+ }
167
+
168
+
169
+
170
+
171
+
172
+ public:
173
+
174
+ OutParser(const char* name) {
175
+
176
+ fout = fopen(name, "w");
177
+
178
+ buff = new char[50000]();
179
+
180
+ sp = 0;
181
+
182
+ }
183
+
184
+ ~OutParser() {
185
+
186
+ fwrite(buff, 1, sp, fout);
187
+
188
+ fclose(fout);
189
+
190
+ }
191
+
192
+
193
+
194
+ OutParser& operator << (int vu32) {
195
+
196
+ if (vu32 <= 9) {
197
+
198
+ write_ch(vu32 + '0');
199
+
200
+ } else {
201
+
202
+ (*this) << (vu32 / 10);
203
+
204
+ write_ch(vu32 % 10 + '0');
205
+
206
+ }
207
+
208
+ return *this;
209
+
210
+ }
211
+
212
+
213
+
214
+ OutParser& operator << (long long vu64) {
215
+
216
+ if (vu64 <= 9) {
217
+
218
+ write_ch(vu64 + '0');
219
+
220
+ } else {
221
+
222
+ (*this) << (vu64 / 10);
223
+
224
+ write_ch(vu64 % 10 + '0');
225
+
226
+ }
227
+
228
+ return *this;
229
+
230
+ }
231
+
232
+
233
+
234
+ OutParser& operator << (char ch) {
235
+
236
+ write_ch(ch);
237
+
238
+ return *this;
239
+
240
+ }
241
+
242
+ OutParser& operator << (const char *ch) {
243
+
244
+ while (*ch) {
245
+
246
+ write_ch(*ch);
247
+
248
+ ++ch;
249
+
250
+ }
251
+
252
+ return *this;
253
+
254
+ }
255
+
256
+ };
257
+
258
+
259
+
260
+ int main()
261
+
262
+ {
263
+
264
+ InParser fin("stramosi.in");
265
+
266
+ OutParser fout("stramosi.out");
267
+
268
+ fin >> n >> Q;
269
+
270
+ for(int i=1; i<=n; i++)
271
+
272
+ fin >> t[i][0];
273
+
274
+ build();
275
+
276
+ while(Q--)
277
+
278
+ {
279
+
280
+ fin >> x >> c;
281
+
282
+ if(c > n)
283
+
284
+ fout << 0 << '\n';
285
+
286
+ else
287
+
288
+ {
289
+
290
+ p = 0;
291
+
292
+ while(c)
293
+
294
+ {
295
+
296
+ if(c&1)
297
+
298
+ x = t[x][p];
299
+
300
+ c>>=1;
301
+
302
+ ++p;
303
+
304
+ }
305
+
306
+ fout << x << '\n';
307
+
308
+ }
309
+
310
+ }
311
+
312
+
313
+
314
+ return 0;
315
+
316
+ }
ancestors/dp_log/57d97f28-139a-11eb-b768-309c234925a0.cpp ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <fstream>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ ifstream in("stramosi.in");
9
+
10
+ ofstream out("stramosi.out");
11
+
12
+
13
+
14
+ int n, v[20][250001], m;
15
+
16
+
17
+
18
+ int main()
19
+
20
+ {
21
+
22
+ in>>n>>m;
23
+
24
+ for(int i=1; i<=n; i++)
25
+
26
+ in>>v[0][i];
27
+
28
+ for(int i=1; i<=18; i++)
29
+
30
+ for(int j=1; j<=n; j++)
31
+
32
+ v[i][j]=v[i-1][v[i-1][j]];
33
+
34
+ while(m)
35
+
36
+ {
37
+
38
+ m--;
39
+
40
+ int p,q, ans,j=0;
41
+
42
+ in>>q>>p;
43
+
44
+ ans=q;
45
+
46
+ while(p)
47
+
48
+ {
49
+
50
+ if(p%2!=0)
51
+
52
+ ans=v[j][ans];
53
+
54
+ p/=2;
55
+
56
+ j++;
57
+
58
+ }
59
+
60
+ out<<ans<<"\n";
61
+
62
+ }
63
+
64
+ return 0;
65
+
66
+ }
ancestors/dp_log/57e90f92-139a-11eb-b35d-309c234925a0.cpp ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+
3
+
4
+ using namespace std;
5
+
6
+
7
+
8
+ ifstream in("stramosi.in");
9
+
10
+ ofstream out("stramosi.out");
11
+
12
+
13
+
14
+ const int MAX = 250001;
15
+
16
+
17
+
18
+ int dp[18][MAX];
19
+
20
+ int n, m, x, y;
21
+
22
+
23
+
24
+ int main()
25
+
26
+ {
27
+
28
+ in >> n >> m;
29
+
30
+ for(int i = 1; i <= n; i++)
31
+
32
+ {
33
+
34
+ in >> dp[0][i];
35
+
36
+ }
37
+
38
+ for(int i = 1; i < 18; i++)
39
+
40
+ {
41
+
42
+ for(int j = 1; j <= n; j++)
43
+
44
+ {
45
+
46
+ dp[i][j] = dp[i - 1][dp[i - 1][j]];
47
+
48
+ }
49
+
50
+ }
51
+
52
+ while(m--)
53
+
54
+ {
55
+
56
+ in >> x >> y;
57
+
58
+ int j = 0;
59
+
60
+ while(y)
61
+
62
+ {
63
+
64
+ if(y % 2 != 0)
65
+
66
+ {
67
+
68
+ x = dp[j][x];
69
+
70
+ }
71
+
72
+ y /= 2;
73
+
74
+ j++;
75
+
76
+ }
77
+
78
+ out << x << "\n";
79
+
80
+ }
81
+
82
+ return 0;
83
+
84
+ }
ancestors/dp_log/57fac2f8-139a-11eb-ba35-309c234925a0.cpp ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <bits/stdc++.h>
2
+ using namespace std;
3
+
4
+
5
+
6
+ ifstream in("stramosi.in");
7
+
8
+ ofstream out("stramosi.out");
9
+
10
+ int n,m,p[250005],dp[19][250005];
11
+
12
+ int main()
13
+
14
+ {int i,j,q,P,p_2,stramos;
15
+
16
+ in >> n >> m;
17
+
18
+ for (i = 1; i <= n; i++) in >> dp[0][i];
19
+
20
+
21
+
22
+ for (j = 1; j <= 18; j++)
23
+
24
+ for (i = 1; i <= n; i++)
25
+
26
+ dp[j][i] = dp[j - 1][dp[j - 1][i]];
27
+
28
+
29
+
30
+ for (i = 1; i <= m; i++)
31
+
32
+ {
33
+
34
+ in >> q >> P;
35
+
36
+ p_2 = 0;
37
+
38
+ stramos = q;
39
+
40
+ while (P)
41
+
42
+ {
43
+
44
+ if (P % 2 == 1)
45
+
46
+ stramos = dp[p_2][stramos];
47
+
48
+ P >>= 1;
49
+
50
+ p_2++;
51
+
52
+ }
53
+
54
+ out << stramos << "\n";
55
+
56
+ }
57
+
58
+ return 0;
59
+
60
+ }