• zjnu1726 STOGOVI (lca)


    Description

    Mirko is playing with stacks. In the beginning of the game, he has an empty stack denoted with number 0. In the
    ith step of the game he will choose an existing stack denoted with v, copy it and do one of the following actions:
    a. place number i on top of the new stack
    b. remove the number from the top of the new stack
    c. choose another stack denoted with w and count how many different numbers exist that are in the new stack
    and in the stack denoted with w
    The newly created stack is denoted with i.
    Mirko doesn’t like to work with stacks so he wants you to write a programme that will do it for him. For each
    operation of type b output the number removed from stack and for each operation of type c count the required
    numbers and output how many of them there are.

    Input

    The first line of input contains the integer N (1 <= N <= 300000), the number of steps in Mirko’s game.
    The steps of the game are chronologically denoted with the first N integers.
    The ith of the following N lines contains the description of the ith step of the game in one of the following three
    forms:
    "a v" for operation of type a.
    "b v" for operation of type b.
    "c v w" for operation of type c.
    The first character in the line denotes the type of operation and the following one or two denote the accompanying
    stack labels that will always be integers from the interval [0,i-1].
    For each operation of type b, the stack we’re removing the element from will not be empty.

    Output

    For each operation type b or c output the required number, each in their own line, in the order the operations
    were given in the input.

    Sample Input

    5 a 0 a 1 b 2 c 2 3 b 4 11 a 0 a 1 a 2 a 3 a 2 c 4 5 a 5 a 6 c 8 7 b 8 b 8

    Sample Output

    2 1 2 2 2 8 8

    Hint

    In the beginning, we have the stack S0 = {}. In the first step, we copy S0 and place
    number 1 on top, so S1 = {1}. In the second step, we copy S1 and place 2 on top of it, S2 = {1,2}. In the third step we
    copy S2 and remove number 2 from it, S3 = {1}. In the fourth step we copy S2 and denote the copy with S4, then count
    the numbers appearing in the newly created stack S4 and stack S3, the only such number is number 1 so the solution is 1.

    In the fifth step we copy S4 and remove number 2 from it, S5 = {1}.

    题意:一开始给你一个空栈,有3个操作。1.a v:先把编号为v的栈复制,然后在栈顶上放i 2.b v:先把编号为v的栈复制,然后去掉栈顶元素 3.c v w:先把编号为v的栈复制,然后数出同时存在于v,w栈的数的个数。开一个二叉树,如果是a操作,那么加入新的节点,如果是b操作,那么找到v的父节点,如果是c操作,那么全部输入读入后,求一个lca。


    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<bitset>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef long double ldb;
    #define inf 99999999
    #define pi acos(-1.0)
    #define maxn 300005
    vector<int>vec[maxn];
    vector<int>::iterator it;
    struct node{
        int x,y;
    }c[maxn];
    
    int jd[maxn],fa[maxn],f[maxn][25],dep[maxn],ans[maxn],daibiao[maxn],vis[maxn],siz[maxn];
    
    int lca(int x,int y){
        int i;
        if(dep[x]<dep[y]){
            swap(x,y);
        }
        for(i=20;i>=0;i--){
            if(dep[f[x][i] ]>=dep[y]){
                x=f[x][i];
            }
        }
        if(x==y)return x;
        for(i=20;i>=0;i--){
            if(f[x][i]!=f[y][i]){
                x=f[x][i];y=f[y][i];
            }
        }
        return f[x][0];
    }
    
    int main()
    {
        int n,m,i,j,x,jiedian,jiedian1,y,k;
        char s[10];
        while(scanf("%d",&m)!=EOF)
        {
            jd[1]=1;
            for(i=1;i<=m+1;i++)vec[i].clear();
            int t=1;
            daibiao[1]=1;
            dep[1]=1;
            for(i=2;i<=m+1;i++){
                scanf("%s%d",s,&x);x++;
                jiedian=jd[x];
                if(s[0]=='a'){
                    t++;
                    dep[t]=dep[jiedian]+1;
                    jd[i]=t;
                    f[t][0]=jiedian;
                    daibiao[t]=i;
                    c[i].x=c[i].y=-2;
                }
                else if(s[0]=='b'){
                    ans[i]=daibiao[jiedian];
                    jiedian1=f[jiedian][0];
                    jd[i]=jiedian1;
                    c[i].x=c[i].y=-1;
                }
                else if(s[0]=='c'){
                    scanf("%d",&y);y++;
                    jd[i]=jiedian;
                    c[i].x=jd[i];c[i].y=jd[y];
                }
            }
            for(k=1;k<=20;k++){
                for(i=1;i<=m+1;i++){
                    f[i][k]=f[f[i][k-1]][k-1];
                }
            }
            for(i=2;i<=m+1;i++){
                if(c[i].x==-2)continue;
                if(c[i].x==-1){
                    printf("%d
    ",ans[i]-1);
                }
                else{
                    int gong=lca(c[i].x,c[i].y );
                    printf("%d
    ",dep[gong]-1);
    
                }
            }
        }
        return 0;
    }



  • 相关阅读:
    SAP CRM WebClient UI的Delta处理机制介绍
    三种动态控制SAP CRM WebClient UI assignment block显示与否的方法
    SAPGUI软件里做的设置,本地操作系统保存的具体位置
    SAP CRM附件在应用服务器上的存储原理解析
    FLINK实例(2):CONNECTORS(1)如何正确使用 Flink Connector?
    shell脚本执行报错:/bin/bash^M: bad interpreter: No such file or directory
    FLINK实例(6): CONNECOTRS(5)Flink Kafka Connector 与 Exactly Once 剖析
    java.lang.IllegalStateException(Connection to remote Spark driver was lost)
    java.security.cert.CertificateNotYetValidException: NotBefore
    Hadoop问题:org.apache.hadoop.ipc.RpcException: RPC response exceeds maximum data length 错误
  • 原文地址:https://www.cnblogs.com/herumw/p/9464506.html
Copyright © 2020-2023  润新知