2887 Big String

splay瞎搞一下,正解是分块数组或分块链表,但是学不会啊!

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 2005
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 9997
int son[maxn][2],fa[maxn],sz[maxn],root,len,q;
char s[maxn],v[maxn],ch;
//kpmsrzxxzu
inline void update(int o){
    sz[o] = 1;
    if(son[o][0] != -1) sz[o] += sz[son[o][0]];
    if(son[o][1] != -1) sz[o] += sz[son[o][1]];
}
inline int isrson(int p,int f){
    if(f == -1) return 0;
    return son[f][1] == p;
}
inline void changeson(int p,int f,int v){
    if(f != -1) son[f][v] = p;
    if(p != -1) fa[p] = f;
    if(f == -1) root = p;
}
int build(int l,int r){
    if(l > r) return -1;
    int o = len;
    len++;
    if(l == r){son[o][0] = son[o][1] = -1,sz[o] = 1,v[o] = s[l];return o;}
    int mid = (l + r) >> 1,ls,rs;
    v[o] = s[mid],ls = build(l,mid-1),rs = build(mid+1,r);
    changeson(ls,o,0),changeson(rs,o,1),update(o);
    return o;
}
void rotate(int p){//只会出现祖辈不存在的情况
    int f = fa[p],g = fa[f];
    int u = isrson(p,f),v = isrson(f,g);
    changeson(son[p][u^1],f,u),changeson(f,p,u^1),changeson(p,g,v);
    update(f);
}
void splay(int p,int tar){
    if(fa[p] == tar) return;
    while(fa[p] != tar && fa[fa[p]] != tar){
        int f = fa[p],g = fa[f];//保证了父亲及其祖先存在
        int u = isrson(p,f),v = isrson(f,g);
        if(u ^ v) rotate(p),rotate(p);
        else rotate(f),rotate(p);
    }
    if(fa[p] != tar) rotate(p);
    update(p);
}
int findtr(){
    int p = root,ls = son[p][0],rs = son[p][1];
    if(q >= sz[p]) q = sz[p] - 1;
    while(1){
        if(ls == -1){
            if(q == 1) break;
            else p = rs,q--;
            ls = son[p][0],rs = son[p][1];
            continue;
        }
        if(sz[ls] >= q) p = ls;
        else if(sz[ls] + 1 == q) break;
        else p = rs,q = q  - sz[ls] - 1;
        ls = son[p][0],rs = son[p][1];
    }
    return p;
}
void insert(){
    int p = findtr();
    splay(p,-1);
    int o = len;
    len++,v[o] = ch;
    changeson(root,o,0),changeson(son[root][1],o,1);
    son[root][1] = -1,fa[o] = -1;
    update(root);
    root = o;
    update(root);
}
void query(){
    int p = findtr();
    printf("%c\n",v[p]);
    splay(p,-1);
}
signed main(){
  //  freopen("a.in","r",stdin);
 //   freopen("b.out","w",stdout);
  //  int k = 0;
    while(~scanf("%s",s + 1)){
        int t = strlen(s + 1);
        len = 0;
        s[0] = '\0',s[t + 1] = '\0';
        root = build(0,t + 1);
        fa[root] = -1;
        int n;
        char op[5];
        scanf("%d",&n);
  //      printf("Case %d:\n",++k);
        for(register int i = 1;i <= n;++i){
            scanf("%s",op);
            if(op[0] == 'Q') scanf("%d",&q),q++,query();
            else scanf("%s%d",op,&q),ch = op[0],insert();
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zhuiyicc/p/9546998.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值