Advertisement
pasholnahuy

ZZZ

Jun 14th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6. constexpr int len = 320;
  7.  
  8. void modify(vector<int> &jump, vector<int> &last_jump, vector<int> &jump_count, const vector<int> &block, int ind,
  9. int val) {
  10. int cur_block = block[ind];
  11. int n = jump.size();
  12. jump[ind] = val;
  13. for (int i = ind; i >= 0; --i) {
  14. if (block[i] != cur_block){
  15. break;
  16. }
  17. if (i + jump[i] > n - 1 || (i + jump[i] <= n - 1 && block[i + jump[i]] < block[i])) {
  18. last_jump[i] = i;
  19. jump_count[i] = 1;
  20. } else {
  21. last_jump[i] = last_jump[i + jump[i]];
  22. jump_count[i] = jump_count[i + jump[i]] + 1;
  23. }
  24. }
  25. }
  26. pair<int, int> GetResult(vector<int> &jump, vector<int> &last_jump, vector<int> &jump_count, int ind){
  27. int cur = ind;
  28. int n = jump.size();
  29. int jump_cnt = 0;
  30. while (cur < n - 1){
  31. if (last_jump[cur] == cur){
  32. jump_cnt += 1;
  33. cur = cur + jump[cur];
  34. } else {
  35. jump_cnt += jump_count[cur];
  36. cur = last_jump[cur];
  37. }
  38.  
  39. }
  40. return {jump_cnt, cur};
  41. }
  42. int main() {
  43. ios::sync_with_stdio(0);
  44. cin.tie(0);
  45. int n, m;
  46. cin >> n >> m;
  47. vector<int> jump(n);
  48. for (int i = 0; i < n; ++i) {
  49. cin >> jump[i];
  50. }
  51. vector<int> last_jump(n);
  52. vector<int> jump_count(n);
  53. vector<int> block(n);
  54. int last_block = 0;
  55. for (int i = n - 1; i >= 0; --i) {
  56. if (i + jump[i] > n - 1 || (i + jump[i] <= n - 1 && block[i + jump[i]] < block[i])) {
  57. last_jump[i] = i;
  58. jump_count[i] = 1;
  59. if (i + jump[i] <= n - 1) {
  60. ++last_block;
  61. }
  62. } else {
  63. last_jump[i] = last_jump[i + jump[i]];
  64. jump_count[i] = jump_count[i + jump[i]] + 1;
  65. }
  66. block[i] = last_block;
  67. }
  68. for (int i = 0; i < m; ++i){
  69. int num;
  70. cin >> num;
  71. if (num == 1){
  72. int ind;
  73. cin >> ind, --ind;
  74. auto [cnt, last] = GetResult(jump, last_jump, jump_count, ind);
  75. cout << last + 1 << " " << cnt << '\n';
  76. } else {
  77. int ind, val;
  78. cin >> ind >> val, --ind;
  79. modify(jump, last_jump, jump_count, block, ind, val);
  80. }
  81. }
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement