码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 13E

时间:2019-03-05 18:46:47      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:sub   OLE   图片   nta   red   scac   immediate   sam   pen   

 

Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for one person with following rules:

There are N holes located in a single row and numbered from left to right with numbers from 1 to N. Each hole has it‘s own power (hole number i has the power ai). If you throw a ball into hole i it will immediately jump to hole i + ai, then it will jump out of it and so on. If there is no hole with such number, the ball will just jump out of the row. On each of the M moves the player can perform one of two actions:

  • Set the power of the hole a to value b.
  • Throw a ball into the hole a and count the number of jumps of a ball before it jump out of the row and also write down the number of the hole from which it jumped out just before leaving the row.

Petya is not good at math, so, as you have already guessed, you are to perform all computations.


Input

The first line contains two integers N and M (1 ≤ N ≤ 105, 1 ≤ M ≤ 105) — the number of holes in a row and the number of moves. The second line contains N positive integers not exceeding N — initial values of holes power. The following M lines describe moves made by Petya. Each of these line can be one of the two types:

  • 0 a b
  • 1 a
Type 0 means that it is required to set the power of hole a to b, and type 1 means that it is required to throw a ball into the a-th hole. Numbers a and b are positive integers do not exceeding N.Output

For each move of the type 1 output two space-separated numbers on a separate line — the number of the last hole the ball visited before leaving the row and the number of jumps it made.

Examples
Input
8 5
1 1 1 1 1 2 8 2
1 1
0 1 3
1 1
0 3 4
1 2
Output
8 7
8 5
7 3
分块,对每次更新,维护每个块里面的每个元素的三个属性:下一个到达的位置,跳的次数,最后一次跳的位置。
技术图片
 1 #include <cstdio>
 2 #include <stack>
 3 #include <cmath>
 4 #include <queue>
 5 #include <string>
 6 #include <queue>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 
11 #define lid id<<1
12 #define rid id<<1|1
13 #define closein cin.tie(0)
14 #define scac(a) scanf("%c",&a)
15 #define scad(a) scanf("%d",&a)
16 #define print(a) printf("%d\n",a)
17 #define debug printf("hello world")
18 #define form(i,n,m) for(int i=n;i<m;i++)
19 #define mfor(i,n,m) for(int i=n;i>m;i--)
20 #define nfor(i,n,m) for(int i=n;i>=m;i--)
21 #define forn(i,n,m) for(int i=n;i<=m;i++)
22 #define scadd(a,b) scanf("%d%d",&a,&b)
23 #define memset0(a) memset(a,0,sizeof(a))
24 #define scaddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
25 #define scadddd(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
26 
27 #define INF 0x3f3f3f3f
28 #define maxn 100005
29 typedef long long ll;
30 using namespace std;
31 //---------AC(^-^)AC---------\\
32 
33 int n,m,blo;
34 int cnt[maxn],nxt[maxn],last[maxn],power[maxn];
35 
36 void update(int i,int j)
37 {
38     if(j>n)
39     {
40         cnt[i]=1;
41         last[i]=i;
42         nxt[i]=n+1;
43     }
44     else if(i/blo==j/blo)
45     {
46         nxt[i]=nxt[j];
47         cnt[i]=cnt[j]+1;
48         last[i]=last[j];
49     }
50     else
51     {
52         nxt[i]=j;
53         last[i]=i;
54         cnt[i]=1;
55     }
56 }
57 void query(int x)
58 {
59     int num,ans;
60     ans=cnt[x];
61     num=last[x];
62     while(true)
63     {
64         x=nxt[x];
65         if(x>n) break;
66         num=last[x];
67         ans+=cnt[x];
68     }
69     printf("%d %d\n",num,ans);
70 }
71 
72 int main()
73 {
74     scadd(n,m);
75     blo=ceil(sqrt(n));
76     forn(i,1,n) scad(power[i]);
77     nfor(i,n,1) update(i,i+power[i]);
78     while(m--)
79     {
80         int op;
81         scad(op);
82         if(op==1)
83         {
84             int x;
85             scad(x);
86             query(x);
87         }
88         else
89         {
90             int x,y;
91             scadd(x,y);
92             power[x]=y;
93             for(int i=x;i&&i/blo==x/blo;i--)    update(i,i+power[i]);
94 
95         }
96     }
97     return 0;
98 }
<-click here to see the code

CodeForces - 13E

标签:sub   OLE   图片   nta   red   scac   immediate   sam   pen   

原文地址:https://www.cnblogs.com/mile-star/p/10478563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!