Raslboyy

Лунки

Jan 8th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <math.h>
  6. #include <climits>
  7.  
  8. //#pragma GCC optimize("O3, unroll-loops");
  9.  
  10. #include <list>
  11. #include <map>
  12. #include <set>
  13. #include <queue>
  14. #include <unordered_map>
  15. #include <unordered_set>
  16.  
  17. //#include <chrono>
  18. //#include <fstream>
  19. //#include <bits/stdc++.h>
  20.  
  21. #define mp make_pair
  22. #define pb push_back
  23. #define eb emplace_back
  24. #define x first
  25. #define y second
  26. #define sz(x) (int)x.size()
  27. #define all(x) begin(x), end(x)
  28. #define rall(x) rbegin(x), rend(x)
  29. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  30. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  31.  
  32. using namespace std;
  33.  
  34. typedef unsigned long long ull;
  35. typedef long long ll;
  36. typedef long double ld;
  37. typedef pair<int, int> pi;
  38. typedef pair<ll, ll> pl;
  39. typedef vector<int> veci;
  40. typedef vector<bool> vecb;
  41. typedef vector<vector<int>> vvi;
  42. typedef vector<vector<bool>> vvb;
  43.  
  44. const int INF_I = 1e9;
  45. const ll INF_LL = 1e18;
  46.  
  47. int n, len;
  48. veci a;
  49. vector<pi> sq;
  50. veci to;
  51.  
  52. void build() {
  53.     RFOR(i, n, 0) {
  54.         int gr = i / len;
  55.         if (i + a[i] >= n || i + a[i] >= (gr + 1) * len) {
  56.             sq[i].x = 1;
  57.             sq[i].y = (i + a[i] < n ? i + a[i] : -1);
  58.         }
  59.         else {
  60.             if (sq[i + a[i]].y == -1) {
  61.                 sq[i].x = 1;
  62.                 sq[i].y = i + a[i];
  63.             }
  64.             else {
  65.                 sq[i].x = sq[i + a[i]].x + 1;
  66.                 sq[i].y = sq[i + a[i]].y;
  67.             }
  68.         }
  69.     }
  70. }
  71.  
  72. pi start(int in) {
  73.     in--;
  74.     int d = 0;
  75.     int cur = in;
  76.     for (; sq[cur].y != -1; cur = sq[cur].y)
  77.         d += sq[cur].x;
  78.     return make_pair(d + 1, cur + 1);
  79. }
  80.  
  81. void update(int in, int val) {
  82.     in--;
  83.     a[in] = val;
  84.     int gr = in / len;
  85.     RFOR(i, in + 1, gr*len) {
  86.         if (i + a[i] >= n || i + a[i] >= (gr + 1) * len) {
  87.             sq[i].x = 1;
  88.             sq[i].y = (i + a[i] < n ? i + a[i] : -1);
  89.         }
  90.         else {
  91.             if (sq[i + a[i]].y == -1) {
  92.                 sq[i].x = 1;
  93.                 sq[i].y = i + a[i];
  94.             }
  95.             else {
  96.                 sq[i].x = sq[i + a[i]].x + 1;
  97.                 sq[i].y = sq[i + a[i]].y;
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103. int main() {
  104.     /*ifstream cin("in.txt");
  105.     ofstream cout("out.txt");
  106.     auto start_time = chrono::high_resolution_clock::now();*/
  107.  
  108.     ios::sync_with_stdio(false);
  109.     cin.tie(0);
  110.     cout.tie(0);
  111.  
  112.     int m;
  113.     cin >> n >> m;
  114.     len = ceil(sqrt(n));
  115.     sq.assign(n, mp(0, 0));
  116.     a.assign(n, 0);
  117.     for (int i = 0; i < n; i++)
  118.         cin >> a[i];
  119.     build();
  120.     while (m--) {
  121.         int z;
  122.         cin >> z;
  123.         if (!z) {
  124.             int a, b;
  125.             cin >> a >> b;
  126.             update(a, b);
  127.         }
  128.         else {
  129.             int a;
  130.             cin >> a;
  131.             pi res = start(a);
  132.             cout << res.y << " " << res.x << "\n";
  133.         }
  134.     }
  135.    
  136.     /*auto end_time = chrono::high_resolution_clock::now();
  137.     chrono::duration<double> duration = end_time - start_time;
  138.     cout << duration.count() << "\n";
  139.     cin.close();
  140.     cout.close();*/
  141.     return 0;
  142. }
Add Comment
Please, Sign In to add comment