BZOJ 2002: [Hnoi2010]Bounce flying sheep

Keywords: C++

2002: [Hnoi2010]Bounce flying sheep

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 13360  Solved: 6782
[Submit][Status][Discuss]

Description

One day, Lostmonkey invented a super elastic device. In order to show off in front of his sheep friends, he invited the little sheep to play a game together. At the beginning of the game, Lostmonkey placed n devices along a straight line on the ground. Each device set the initial elastic coefficient ki. When the sheep reached the I device, it would play back ki steps to reach the i+ki device. If there was no i+ki device, the sheep would be bounced. The sheep wanted to know that when it started from the i-th device, it would be bounced off after being bounced several times. In order to make the game more interesting, Lostmonkey can modify the elasticity coefficient of a certain elastic device, which is a positive integer at any time.

Input

The first line contains an integer n, indicating that there are N devices on the ground, and the number of devices ranges from 0 to n-1. The next line has n positive integers, which are the initial elastic coefficients of those N devices in turn. The third line has a positive integer m, and then there are at least two numbers i and j in each line of M. if i=1, you need to output a positive integer k that is bounced several times from J and then bounced. If i=2, you need to input another positive integer k, indicating that the coefficient of the j elastic device is modified to K. For 20% of data n, m < = 10000, for 100% of data n < = 200000, m < = 100000

Output

For each i=1 case, you need to output a required number of steps, accounting for one line.

Sample Input

4
1 2 1 1
3
1 1
2 1 1
1 1

Sample Output

2
3

HINT

 

Source

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long ll;
 5 const int maxn=200000+5;
 6 
 7 int block,belong[maxn],a[maxn],n,m,num;
 8 int l[maxn],r[maxn],cnt[maxn],end[maxn];
 9 
10 inline void bt()
11 {
12     block=sqrt(n);
13     num=n/block;
14     if(n%block)
15         num++;
16     for(int i=1;i<=num;++i)
17         l[i]=(i-1)*block+1,r[i]=i*block;
18     r[num]=n;
19     for(int i=1;i<=n;i++)
20         belong[i]=(i-1)/block+1;
21     
22     for(int i=n;i>0;i--)
23     {
24         if(i+a[i]>n)
25             cnt[i]=1,end[i]=-1;
26         else if(belong[i]==belong[i+a[i]])    
27             cnt[i]=cnt[i+a[i]]+1,end[i]=end[i+a[i]];
28         else     
29             cnt[i]=1,end[i]=i+a[i];
30     }
31 }
32 
33 inline int work(int x)
34 {
35     int ans=0;
36     while(true)
37     {
38         ans+=cnt[x];
39         if(end[x]<0)
40             break;
41         x=end[x];
42     }
43     return ans;
44 }
45 
46 inline void update(int x,int y)
47 {
48     a[x]=y;
49     for(int i=x;i>=l[belong[x]];i--)
50     {
51         if(i+a[i]>n)
52             cnt[i]=1,end[i]=-1;
53         else if(belong[i]==belong[i+a[i]])
54             cnt[i]=cnt[i+a[i]]+1,end[i]=end[i+a[i]];
55         else 
56             cnt[i]=1,end[i]=i+a[i];
57     }
58     return ;
59 }
60 
61 int main()
62 {
63     scanf("%d",&n);
64     for(int i=1;i<=n;i++)
65         scanf("%d",&a[i]);
66     bt();
67     scanf("%d",&m);
68     for(int i=1;i<=m;i++)
69     {
70         int x;
71         scanf("%d",&x);
72         if(x==1)
73         {
74             int y;
75             scanf("%d",&y);
76             y++;
77             printf("%d\n",work(y));
78         }
79         else
80         {
81             int y,z;
82             scanf("%d%d",&y,&z);
83             y++;
84             update(y,z);
85         }
86     }
87     return 0;
88 }

Posted by lobo235 on Sun, 05 Apr 2020 04:00:21 -0700