Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- int n, m;
- int a[100005];
- int nex[100005];
- int cnt[100005];
- int par[100005];
- int s;
- int main()
- {
- if(fopen("test.inp","r")) freopen("test.inp","r",stdin);
- scanf("%d%d",&n,&m);
- s = sqrt(n);
- for(int i=1; i<=n; i++) scanf("%d",&a[i]);
- nex[n] = n+1; cnt[n] = 1;
- par[n] = n;
- for(int i=n-1; i>=1; i--){
- int u = a[i] + i;
- if (u > n) nex[i] = n+1, cnt[i] = 1, par[i] = i;
- else if ((u-1)/s == (i-1)/s){
- nex[i] = nex[u];
- cnt[i] = cnt[u]+1;
- par[i] = par[u];
- }
- else nex[i] = u, cnt[i] = 1, par[i] = par[u];
- }
- while (m--){
- int ty; scanf("%d",&ty);
- if (ty==0){
- int x, val; scanf("%d%d",&x,&val);
- a[x] = val;
- int k = (x-1)/s;
- for(int u=x; u>=k*s+1; u--){
- int j = u+a[u];
- if (j > n) nex[u] = n+1, cnt[u] = 1, par[u] = u;
- else if ((j-1)/s == (u-1)/s){
- nex[u] = nex[j];
- cnt[u] = cnt[j]+1;
- par[u] = par[j];
- }
- else nex[u] = j, cnt[u] = 1, par[u] = par[j];
- }
- }
- else{
- int x; scanf("%d",&x);
- int res=0;
- int lst;
- while (x <= n){
- if (nex[x]>n){
- lst=par[x];
- res+=cnt[x];
- x=nex[x];
- }
- else res+=cnt[x], x=nex[x];
- }
- std::cout << lst << ' ' << res << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement