zoukankan      html  css  js  c++  java
  • 高级打字机

    简单的可持久化。

    是刚学主席树的练手好题!!

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=2*1e6+10;
    struct node{
        int l,r,val;
    }tr[maxn];
    int n,cnt,tot,root[maxn],sz[maxn];
    inline int build(int l,int r){
        int h=++cnt;
        if(l==r)return h;
        int mid=(l+r)>>1;
        tr[h].l=build(l,mid);
        tr[h].r=build(mid+1,r);
        return h;
    } 
    inline int modify(int rt,int l,int r,int pos,int x){
        int h=++cnt;
        tr[h]=tr[rt];
        if(l==r){
            tr[h].val=x;
            return h;
        }
        int mid=(l+r)>>1;
        if(mid>=pos)tr[h].l=modify(tr[rt].l,l,mid,pos,x);
        else tr[h].r=modify(tr[rt].r,mid+1,r,pos,x);
        return h;
    }
    inline int query(int h,int l,int r,int pos){
        if(l==r)return tr[h].val;
        int mid=(l+r)>>1;
        if(mid>=pos)return query(tr[h].l,l,mid,pos);
        else return query(tr[h].r,mid+1,r,pos);
    }
    int main(){
        cin>>n;
        char opt;
        root[0]=build(1,n);
        for(int i=1;i<=n;i++){
            cin>>opt;
            if(opt=='T'){
                char x;cin>>x;
                tot++;sz[tot]=sz[tot-1]+1;
                root[tot]=modify(root[tot-1],1,n,sz[tot],x);
            }else if(opt=='U'){
                int x;cin>>x;
                tot++;root[tot]=root[tot-1-x];sz[tot]=sz[tot-1-x];
            }else{
                int x;cin>>x;
                printf("%c
    ",query(root[tot],1,n,x));
            }
        }
        return 0;
    }

    深深地感到自己的弱小。

  • 相关阅读:
    nginx:安装成windows服务
    org.aspectj.apache.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 18
    数据库中间件
    架构策略
    谈判
    设计模式 总结 常用10种
    08 状态模式 state
    07 策略模式 strategy
    06 命令模式(不用)
    05 观察者模式 Observer
  • 原文地址:https://www.cnblogs.com/syzf2222/p/12509606.html
Copyright © 2011-2022 走看看