Advertisement
MinhNGUYEN2k4

Untitled

Sep 20th, 2021
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. int n, m;
  5. int a[100005];
  6. int nex[100005];
  7. int cnt[100005];
  8. int par[100005];
  9. int s;
  10.  
  11. int main()
  12. {
  13.     if(fopen("test.inp","r")) freopen("test.inp","r",stdin);
  14.     scanf("%d%d",&n,&m);
  15.     s = sqrt(n);
  16.     for(int i=1; i<=n; i++) scanf("%d",&a[i]);
  17.     nex[n] = n+1; cnt[n] = 1;
  18.     par[n] = n;
  19.     for(int i=n-1; i>=1; i--){
  20.         int u = a[i] + i;
  21.         if (u > n) nex[i] = n+1, cnt[i] = 1, par[i] = i;
  22.         else if ((u-1)/s == (i-1)/s){
  23.             nex[i] = nex[u];
  24.             cnt[i] = cnt[u]+1;
  25.             par[i] = par[u];
  26.         }
  27.         else nex[i] = u, cnt[i] = 1, par[i] = par[u];
  28.     }
  29.     while (m--){
  30.         int ty; scanf("%d",&ty);
  31.         if (ty==0){
  32.             int x, val; scanf("%d%d",&x,&val);
  33.             a[x] = val;
  34.             int k = (x-1)/s;
  35.             for(int u=x; u>=k*s+1; u--){
  36.                 int j = u+a[u];
  37.                 if (j > n) nex[u] = n+1, cnt[u] = 1, par[u] = u;
  38.                 else if ((j-1)/s == (u-1)/s){
  39.                     nex[u] = nex[j];
  40.                     cnt[u] = cnt[j]+1;
  41.                     par[u] = par[j];
  42.                 }
  43.                 else nex[u] = j, cnt[u] = 1, par[u] = par[j];
  44.             }
  45.         }
  46.         else{
  47.             int x; scanf("%d",&x);
  48.             int res=0;
  49.             int lst;
  50.             while (x <= n){
  51.                 if (nex[x]>n){
  52.                     lst=par[x];
  53.                     res+=cnt[x];
  54.                     x=nex[x];
  55.                 }
  56.                 else res+=cnt[x], x=nex[x];
  57.             }
  58.             std::cout << lst << ' ' << res << '\n';
  59.         }
  60.     }
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement