bzoj_p2002 [Hnoi2010]bounce Fly Sheep (LCT)

Source: Internet
Author: User
Tags time limit

Bzoj Portal
Time Limit:10 Sec Memory limit:259 MB
submit:6636 solved:3478
[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. At the beginning of the game, Lostmonkey on the ground along a straight line to put n devices, each device set the initial elastic coefficient ki, when the sheep reached the first device, it will bounce Ki step back to the first I+ki device, if there is no i+ki device, then the sheep were bounced. The sheep wanted to know that when it started from the device I, it was bounced a few times. In order to make the game more interesting, Lostmonkey can modify the elastic coefficient of an elastic device, and the elastic coefficient is a positive integer at any time.

Input
The first line contains an integer n, which indicates that there are n devices on the ground, the device is numbered from 0 to N-1, and the next line has n positive integers, followed by the initial elastic coefficients of the n units. The third line has a positive integer m, the next m row each line has at least two number I, J, if I=1, you want to output from J is bounced several times after being bounced, if i=2 will also enter a positive integer k, indicating that the coefficient of the J-Elastic device is modified to K. For data n,m<=10000 of 20%, for 100% of data n<=200000,m<=100000

Output
For each i=1, you will output a required number of steps, one row.

Sample Input
4
1 2 1 1
3
1 1
2 1 1
1 1

Sample Output
2
3

HINT

Source

The first LCT, because the LCT play is not complete, so there is no first hair template.
The idea is very simple, set up a virtual node, all the last pop-up nodes are connected, each time you and the virtual node is located in the root of the splay to u, output u number of child nodes;
for modification is to cut the edge of U to F[u] and then connect u to the new parent node

#include <cstdio> #include <iostream> using namespace std;
    #define N 200005 struct linkcuttree{int n,m;int f[n],ch[n][2],s[n],a[n]; inline bool Is_root (int o) {return (!f[o]) | |
    (O!=ch[f[o]][0]&&o!=ch[f[o]][1]);}
    inline void push (int o) {s[o]=s[ch[o][0]]+s[ch[o][1]]+1;}
        inline void rot (int o) {int p=f[o],k=f[p],d=ch[p][0]==o;
        if (!is_root (p)) ch[k][ch[k][1]==p]=o;
        F[o]=k,f[ch[o][d]]=p;
        Ch[p][d^1]=ch[o][d],ch[o][d]=p;
    F[p]=o;push (p), push (O);
            } inline void splay (int o) {while (!is_root (o)) {int p=f[o],k=f[p];
            if (Is_root (f[o))) rot (o);
            else if ((ch[p][1]==o) = = (ch[k][1]==p)) Rot (p), rot (o);
        else Rot (o), rot (o);
    } push (O);
    } inline void Access (int o,int son=0) {for (; O;o=f[son=o]) splay (o), Ch[o][1]=son,push (o); } inline int in (int x=0,char Ch=getchar ()) {while (ch> ' 9 ' | |
        ch< ' 0 ') Ch=getchar (); while (ch>=' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();
    return x;
        } void Init () {n=in (); int opt,u,v;
        for (int i=1;i<=n;i++) a[i]=in (), s[i]=1;s[n+1]=1;
        for (int i=1;i<=n;i++) f[i]= (I+a[i]>n+1?n+1:i+a[i]);
        M=in ();
            while (m--) {opt=in (), U=in (); u++;
            if (opt==1) {Access (U), splay (U), printf ("%d\n", s[u]-1);
                } else{v=in ();
                Access (U), splay (U), f[ch[u][0]]=f[u];
                Ch[u][0]=0,f[u]=0;push (U);
            F[u]= (V+U&GT;N+1?N+1:U+V);
}}}}LCT; int main () {lct.init (); return 0;}

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.