zoukankan      html  css  js  c++  java
  • 【Luogu】P1383高级打字机

    可持久化线段树模板题之一。

    权当温习主席树模板

    #include<cstdio>
    #include<cstdlib>
    #include<cctype>
    #define left (rt<<1)
    #define right (rt<<1|1)
    #define mid ((l+r)>>1)
    #define lson l,mid,left
    #define rson mid+1,r,right
    
    inline long long read(){
        long long num=0,f=1;
        char ch=getchar();
        while(!isdigit(ch)){
            if(ch=='-')    f=-1;
            ch=getchar();
        }
        while(isdigit(ch)){
            num=num*10+ch-'0';
            ch=getchar();
        }
        return num*f;
    }
    
    
    int root[2000000];
    int ls[2000000];
    int rs[2000000];
    char tree[2000000];
    int len[2000000];
    int ID;
    void add(int l,int r,int &rt,char x,int pos,int last){
        rt=++ID;
        if(l==r){
            tree[rt]=x;
            return;
        }
        ls[rt]=ls[last];
        rs[rt]=rs[last];
        if(pos<=mid)    add(l,mid,ls[rt],x,pos,ls[last]);
        else             add(mid+1,r,rs[rt],x,pos,rs[last]);
    }
    
    int query(int pos,int l,int r,int rt){
        if(l==r)    return rt;
        if(pos<=mid)    return query(pos,l,mid,ls[rt]);
        else             return query(pos,mid+1,r,rs[rt]);
    }
    
    int now;
    
    int main(){
        int n=read();
        for(int i=1;i<=n;++i){
            char ch[10];int x;
            scanf("%s",ch);
            if(ch[0]=='Q'){
                x=read();
                int pos=query(x,1,n,root[now]);
                printf("%c
    ",tree[pos]);
            }
            else if(ch[0]=='U'){
                x=read();
                now++;
                len[now]=len[now-x-1];
                root[now]=root[now-x-1]; 
            }
            
            else{
                char c[10];
                scanf("%s",c);
                now++;len[now]=len[now-1]+1;
                add(1,n,root[now],c[0],len[now],root[now-1]);
            }
        }
        return 0;
    }
                
        
  • 相关阅读:
    【zookeeper】
    关于redis-windows环境下的一些配置:
    mybatis-注解开发
    jQuery的Validate插件
    Thymeleaf 学习笔记-实例demo(中文教程)
    thymeleaf 学习笔记-基础篇(中文教程)
    AGC 043C
    JOISC 2020 部分题解
    Loj #2687
    CF 1270I
  • 原文地址:https://www.cnblogs.com/cellular-automaton/p/7509828.html
Copyright © 2011-2022 走看看