Bzoj 2002: [hnoi2010] bounce Bullet Flying sheep LCT

Source: Internet
Author: User


LCT maintains the subtree size .....

2002: [hnoi2010] bounce time limit: 10 sec memory limit: 259 MB
Submit: 4085 solved: 2188
[Submit] [Status] Description

One day, lostmonkey invented a super elastic device. He invited little sheep to play a game in order to show it in front of his friends. At the beginning of the game, lostmonkey placed n devices along a straight line on the ground, each of which sets the initial elasticity factor Ki. When the sheep reach the I device, it will play the ki step back, reaches the I + Ki device. If the I + Ki device does not exist, the sheep will fly. The sheep wanted to know that when it started from the I device, it would fly after several rounds. To make the game more interesting, lostmonkey can modify the elasticity coefficient of a certain stretch device. The elasticity coefficient is a positive integer at any time.

Input

The first line contains an integer N, indicating that there are n devices on the ground. The device number ranges from 0 to n-1, And the next line contains n positive integers, the initial elasticity coefficient of the N devices in turn. The third row has a positive integer m, and the next row of M has at least two numbers I and J. If I = 1, you need to output the data to be played several times from J and then fly, if I = 2, a positive integer k is input, indicating that the coefficient of the J-step elastic device is changed to K. For 20% of Data n, m <= 10000, for 100% of Data n <= 200000, m <= 100000

Output

For each I = 1, you need to output a required number of steps, occupying one row.

Sample input4
1 2 1 1
3
1 1
2 1 1
1 1
Sample output2
3
Hint

Source

Splay heuristic merge

[Submit] [Status] Bytes



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=220000;int n,m;int pre[maxn],ch[maxn][2],size[maxn];bool rt[maxn];void init(){  for(int i=0;i<=n+10;i++)    rt[i]=1,pre[i]=0,ch[i][0]=ch[i][1]=0,size[i]=1;  size[0]=0;}void push_up(int r){  size[r]=size[ch[r][0]]+size[ch[r][1]]+1;}void Rotate(int x){  int y=pre[x],kind=ch[y][1]==x;  ch[y][kind]=ch[x][!kind];  pre[ch[y][kind]]=y;  pre[x]=pre[y];  pre[y]=x;  ch[x][!kind]=y;  if(rt[y]) rt[y]=false,rt[x]=true;  else ch[pre[x]][ch[pre[x]][1]==y]=x;  push_up(y);}void Splay(int r){  while(!rt[r])    {      int f=pre[r],ff=pre[f];      if(rt[f]) Rotate(r);      else if((ch[ff][1]==f)==(ch[f][1]==r)) Rotate(f),Rotate(r);      else Rotate(r),Rotate(r);    }  push_up(r);}int Access(int x){  int y=0;  for(;x;x=pre[y=x])    {      Splay(x);      rt[ch[x][1]]=true; rt[ch[x][1]=y]=false;      push_up(x);    }  return y;}int main(){  while(scanf("%d",&n)!=EOF)    {      init();      for(int i=1;i<=n;i++)        {          int t;          scanf("%d",&t);          pre[i]=(i+t>n+1)?n+1:i+t;        }      scanf("%d",&m);      while(m--)        {          int tk,u,v;          scanf("%d",&tk);          if(tk==1)            {              scanf("%d",&u);              u++;              Access(u);              Splay(u);              printf("%d\n",size[ch[u][0]]);            }          else            {              scanf("%d%d",&u,&v);              u++;              Access(u);              Splay(u);              pre[ch[u][0]]=pre[u];              pre[u]=0;              rt[ch[u][0]]=true;              ch[u][0]=0;              push_up(u);              pre[u]=(u+v)>n+1?n+1:u+v;            }        }    }  return 0;}


Bzoj 2002: [hnoi2010] bounce Bullet Flying sheep LCT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.