Advertisement
cincout

Untitled

Sep 30th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1e5 + 50;
  6. const int L = 500;
  7.  
  8. int dp[N];
  9. int link[N];
  10. int l[N], r[N];
  11. int a[N];
  12. int n, m;
  13.  
  14. void build(int num) {
  15. int f = min(n - 1, r[num]);
  16. for (int i = f; i >= l[num]; --i) {
  17. int nxt = a[i] + i;
  18. if (nxt > f) {
  19. dp[i] = 1;
  20. link[i] = i;
  21. }
  22. else {
  23. dp[i] = dp[nxt] + 1;
  24. link[i] = link[nxt];
  25. }
  26. }
  27. }
  28.  
  29. int main() {
  30. ios::sync_with_stdio(false);
  31. cin.tie(0);
  32. l[0] = 0;
  33. r[0] = L - 1;
  34. for (int i = 1; i < N; ++i) {
  35. l[i] = r[i - 1] + 1;
  36. r[i] = l[i] + L - 1;
  37. }
  38. cin >> n >> m;
  39. for (int i = 0; i < n; ++i) {
  40. cin >> a[i];
  41. }
  42. for (int i = 0; l[i] < n; ++i) {
  43. build(i);
  44. }
  45. for (int i = 0; i < m; ++i) {
  46. int t, pos, elem;
  47. cin >> t >> pos;
  48. pos--;
  49. if (t == 0) {
  50. cin >> elem;
  51. a[pos] = elem;
  52. build(pos / L);
  53. }
  54. else {
  55. int res = 0, lst = -1;
  56. while (pos < n) {
  57. lst = link[pos];
  58. res += dp[pos];
  59. pos = link[pos] + a[link[pos]];
  60. }
  61. cout << lst + 1 << " " << res << "\n";
  62. }
  63. }
  64. return 0;
  65. }
  66.  
  67. /*
  68. * int overflow, array bounds
  69. * special cases (n=1?), set tle
  70. * do smth instead of nothing and stay organized
  71. * double troubles
  72. * same points in geoma
  73. * in game theory find win cases
  74. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement