Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- constexpr int len = 320;
- void modify(vector<int> &jump, vector<int> &last_jump, vector<int> &jump_count, const vector<int> &block, int ind,
- int val) {
- int cur_block = block[ind];
- int n = jump.size();
- jump[ind] = val;
- for (int i = ind; i >= 0; --i) {
- if (block[i] != cur_block){
- break;
- }
- if (i + jump[i] > n - 1 || (i + jump[i] <= n - 1 && block[i + jump[i]] < block[i])) {
- last_jump[i] = i;
- jump_count[i] = 1;
- } else {
- last_jump[i] = last_jump[i + jump[i]];
- jump_count[i] = jump_count[i + jump[i]] + 1;
- }
- }
- }
- pair<int, int> GetResult(vector<int> &jump, vector<int> &last_jump, vector<int> &jump_count, int ind){
- int cur = ind;
- int n = jump.size();
- int jump_cnt = 0;
- while (cur < n - 1){
- if (last_jump[cur] == cur){
- jump_cnt += 1;
- cur = cur + jump[cur];
- } else {
- jump_cnt += jump_count[cur];
- cur = last_jump[cur];
- }
- }
- return {jump_cnt, cur};
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- int n, m;
- cin >> n >> m;
- vector<int> jump(n);
- for (int i = 0; i < n; ++i) {
- cin >> jump[i];
- }
- vector<int> last_jump(n);
- vector<int> jump_count(n);
- vector<int> block(n);
- int last_block = 0;
- for (int i = n - 1; i >= 0; --i) {
- if (i + jump[i] > n - 1 || (i + jump[i] <= n - 1 && block[i + jump[i]] < block[i])) {
- last_jump[i] = i;
- jump_count[i] = 1;
- if (i + jump[i] <= n - 1) {
- ++last_block;
- }
- } else {
- last_jump[i] = last_jump[i + jump[i]];
- jump_count[i] = jump_count[i + jump[i]] + 1;
- }
- block[i] = last_block;
- }
- for (int i = 0; i < m; ++i){
- int num;
- cin >> num;
- if (num == 1){
- int ind;
- cin >> ind, --ind;
- auto [cnt, last] = GetResult(jump, last_jump, jump_count, ind);
- cout << last + 1 << " " << cnt << '\n';
- } else {
- int ind, val;
- cin >> ind >> val, --ind;
- modify(jump, last_jump, jump_count, block, ind, val);
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment