poj 2887 Big String

题目连接

http://poj.org/problem?id=2887  

Big String

Description

You are given a string and supposed to do some string manipulations.

Input

The first line of the input contains the initial string. You can assume that it is non-empty and its length does not exceed 1,000,000.

The second line contains the number of manipulation commands N (0 < N ≤ 2,000). The following N lines describe a command each. The commands are in one of the two formats below:

  1. I ch p: Insert a character ch before the p-th character of the current string. If p is larger than the length of the string, the character is appended to the end of the string.
  2. Q p: Query the p-th character of the current string. The input ensures that the p-th character exists.

All characters in the input are digits or lowercase letters of the English alphabet.

Output

For each Q command output one line containing only the single character queried.

Sample Input

abcdf
7
Q 1
I g 2
I h 10
I k 1
Q 1
Q 3
Q 8

Sample Output

a
k
g
f

伸展树的插入,单点查询。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::vector;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1 << 20;
char buf[N];
struct Node {
    int s;
    char dat;
    Node *fa, *ch[2];
    inline void set(int i, char j, Node *p) {
        s = i, dat = j;
        fa = ch[0] = ch[1] = p;
    }
    inline void push_up() {
        s = ch[0]->s + ch[1]->s + 1;
    }
    inline bool dir() const {
        return fa->ch[0] == this;
    }
    inline void link(Node *x, bool d) {
        ch[d] = x;
        x->fa = this;
    }
};
struct SplayTree {
    Node *root, *tail, *null, stack[N];
    inline void init(int n) {
        tail = &stack[0];
        null = tail++;
        null->set(0, '.', NULL);
        root = newNode('.');
        root->link(newNode('.'), 1);
        Node *x = built(1, n);
        root->ch[1]->link(x, 0);
        root->ch[1]->push_up();
        root->push_up();
        splay(x, null);
    }
    inline Node *built(int l, int r) {
        if(l > r) return null;
        int mid = (l + r) >> 1;
        Node *p = newNode(buf[mid - 1]);
        p->ch[0] = built(l, mid -1);
        if(p->ch[0] != null) p->ch[0]->fa = p;
        p->ch[1] = built(mid + 1, r);
        if(p->ch[1] != null) p->ch[1]->fa = p;
        p->push_up();
        return p;
    }
    inline Node *newNode(char dat) {
        Node *p = tail++;
        p->set(1, dat, null);
        return p;
    }
    inline void rotate(Node *&x, bool d) {
        Node *y = x->fa;
        y->ch[!d] = x->ch[d];
        x->fa = y->fa;
        if(x->ch[d]->s) x->ch[d]->fa = y;
        if(y->fa->s) y->fa->ch[!y->dir()] = x;
        x->ch[d] = y;
        y->fa = x, y->push_up();
        if (y == root) root = x;
    }
    inline void splay(Node *x, Node *f) {
        if(x == root) return;
        while(x->fa != f) {
            if(x->fa->fa == f) {
                rotate(x, x->dir());
            } else {
                bool d = x->fa->dir();
                if(d == x->dir()) rotate(x->fa, d), rotate(x, d);
                else rotate(x, !d), rotate(x, d);
            }
        }
        x->push_up();
    }
    inline Node *select(Node *x, int k) {
        for(int t = 0; x->s; ) {
            t = x->ch[0]->s;
            if(t == k) break;
            else if(k < t) x = x->ch[0];
            else k -= t + 1, x = x->ch[1];
        }
        return x;
    }
    inline void insert(char dat, int k) {
        if(k > root->s) k = root->s - 2;
        k--;
        splay(select(root, k), null);
        splay(select(root, k + 1), root);
        Node *x = newNode(dat);
        root->ch[1]->link(x, 0);
        root->ch[1]->push_up();
        root->push_up();
        splay(x, null);
    }
    inline char operator[](int k) {
        splay(select(root, k), null);
        return root->dat;
    }
}spt;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int q, k;
    char op, dat;
    while(~scanf("%s", buf)) {
        scanf("%d", &q);
        spt.init(strlen(buf));
        while(q--) {
            getchar();
            scanf("%c", &op);
            if(op == 'Q') {
                scanf(" %d", &k);
                printf("%c\n", spt[k]);
            } else {
                scanf(" %c %d", &dat, &k);
                spt.insert(dat, k);
            }
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4773422.html

Python实现ARIMA模型 以下是一个使用Python实现ARIMA(自回归移动平均模型)的简单示例代码。在运行代码之前,请确保已经安装了pandasnumpy和库。ARIMAorder(2,0,2)p = 2d = 0q = 2forecastmatplotlib在实际应用中,你需要将模拟数据替换为真实的时间序列数据,并且通过合适的方法(如信息准则等)来确定pdq的最佳值。除了auto_arima在使用pmdarima。 阅读详情

相关推荐

rv1126 SDK编译【2020-0912版本SDK包】

rv1126 sdk 编译

学习记录 1万+

【POJ2887】【块状链表】Big String

【POJ2887】【块状链表】Big String 转载自:http://www.cnblogs.com/hoskey/p/4322417.html Description You are given a string and supposed to do some string manipulations. Input The fir

梧桐下的四叶草 678

从波形到代码:手把手调试GaN图腾柱PFC的过零点切换与软启动策略

本文详细解析了GaN图腾柱无桥Boost PFC电路在过零点切换与软启动策略中的调试方法。通过波形分析、代码实现和寄存器配置,提供了解决误开通、震荡波形等问题的实用方案,帮助工程师优化电源设计效率与可靠性。

weixin_30831529的博客 424

POJ 2887 Big String

分析:给你一串字符,让你进行一系列插入和查询操作,查询时输出第p个字符,数组插入慢,链表查找快,于是我们用块状链表。 # include # include # include typedef struct node { int len; char a[1005]; struct node *next; }Node; int max;

670

第一届“龙信杯”电子数据取证竞赛Writeup

第一届“龙信杯”电子数据取证竞赛Writeup

toto的博客 1万+

EDSR图像超分辨程序复现(踩坑及解决记录)

EDSR图像超分辨程序复现(踩坑及解决记录)

Sunny_Coder的博客 3642

POJ 2887:Big String ← 块状链表

●​​​​​​​ 顾名思义,块状链表=分块+链表,或者理解为用链表串起来的分块。根据对分块最优性的思思,n 个数据分为 sqrt(n) 块,每块 sqrt(n) 个数据。 ​​​​​​​​​​​● 块状链表结合了数组与链表的优势,优化了计算复杂度。

hnjzsyjyj的专栏 863

中兴U30air随身WiFi刷机全攻略:从ABD开启到系统调试(附工具包)

本文提供中兴U30air随身WiFi刷机全攻略,从ADB开启到系统调试的详细步骤,包括必备工具清单、ADB调试模式解锁、Bootloader解锁及自定义ROM刷入与调优。适合技术爱好者深度定制设备,提升网络性能和功能扩展。

weixin_29235923的博客 1942

[POJ2887]Big String 做题笔记

题目来源:http://poj.org/problem?id=2887 块状链表模板题#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define N 1000010 #define L 2020 char s[N],c,opt; int n,po

mhlwsk的博客 916

【腾讯小龙虾 WorkBuddy 专栏 03】技能(Skills)制作全教程!自定义技能编写、导出分享、导入使用一步到位

🔥腾讯AI智能体WorkBuddy自定义技能全攻略 本文为零基础用户提供WorkBuddy自定义技能的完整教程,涵盖技能概念、编写规范到实操全流程。核心内容包括: 技能本质:标准化任务指令模板,可固化重复操作流程 三大价值:一键执行重复任务/跨设备共享/降低使用门槛 实操演示:以"桌面文档自动分类"为例,分步讲解编写、测试、导出 共享方案:支持单技能/批量导出,通过任意渠道传输分享 导入指南:3秒完成他人技能加载,并提供常见问题解决方案 高阶优化:增加异常处理、版本管理等提升技能普适性

AI人人懂 2959

MAC-电脑中的user文件夹在哪?

MAC-电脑中的user文件夹在哪?

兴趣是最好的老师 1万+

poj 2887 Big String(块状链表)

题目链接:poj 2887 Big String 题意: 让你设计一个数据结构,支持将一个字符插在p位置,询问第p个字符。 题解: 块状链表搞搞就行。 1 #include<cstdio> 2 #include<queue> 3 #include<cmath> 4 #include<cstring> 5 ...

weixin_30708329的博客 156

让 RAP 弹窗参数真正变成必填:Excel Upload Action 的两种实现路径

摘要 在RAP应用中,action弹窗参数经常缺乏必填校验,导致用户体验差。本文提出两种解决方案:1)在action实现中手动校验,简单但交互不流畅;2)通过root abstract entity和abstract BDEF建模,利用field(mandatory)实现框架级校验,推荐此长期方案。后者将约束集中到模型层,使UI与执行期校验一致,特别适合ABAP Cloud环境。以Excel导入为例,强制填写备注和明确选择试运行,可避免审计问题,提升系统可靠性。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 198

POJ2887 Big String

通过数组分块技术优化算法的实验范例 #include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;vector&gt; #include&lt;iostream&gt; #include&lt;set&gt; #include&lt;map&gt; #include&lt;queue&gt; #include&lt;ctype.

兜率工的博客 393

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Poj 2887-Big String Splay

题目:http://poj.org/problem?id=2887 Big String Time Limit:1000MS Memory Limit:131072K Total Submissions:6527 Accepted:1563 Description You are giv...

weixin_34007291的博客 97

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Big String poj 2887

题目 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 分析 分块 把队列分为分块后暴力操作即可 ps:可以用二分节省时间。 code #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;iostream&gt; #includ...

A_loud_name 342

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

POJ 2887 Big String 笔记

已知原始字符串,N个操作,操作 I ch p 表示将 ch 插入到字符串第 p 个字符前,操作 Q p 查询当前字符串第 p 个字符。

woniupengpeng的专栏 257

POJ 2887:Big String(分块)

http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二种询问是给出一个位置p,查找第p个位置的字符(p保证合法)。 思路:暴力分块。一开始建成块之后,每次插入就在每个块的字符串插入字符,因为询问最多只有2000个,所以就算极端情况也不会超时。 注意...

weixin_30558305的博客 132

POJ2887 Big String【例题精讲】

题意:给出一字符串,然后有两种操作,在某个位置后面插入某个字符,查询更新后字符串中某个位置的字符。   这个题,需要用到一个神奇的数据结构:块状链表;   推荐论文可自学:对块状链表的一点研究;   题中提到的两种操作都是非常理论的,可以直接套用块状链表的功能。高级的块状链表的维护操作会对根据某个块的大小做出裁决,是保留,是分裂,亦或是合并。但是这题的操作数量最多有2000。完全没...

daxian3723的博客 209

[POJ 2887] Big String [块状链表]

给一个字符串,有两个操作:查询第i个字符,在第i个字符前加入一个其他字符 可以用块状链表 以根号n为块大小,复杂度为每次操作根号n #include #include using namespace std; const int SIZE=1010; struct Node { char a[SIZE*2]; Node *next; int length; void p

jinzhao1994的专栏 652

POJ2887 Big String 块状链表

第一次写块矿链表, 完全按照自己的理解写的, 所以代码很挫 ╮(╯▽╰)╭  还好n

yamiedie_ 522

Big String-POJ2887块状数组

Time Limit: 1000MS Memory Limit: 131072KDescriptionYou are given a string and supposed to do some string manipulations.InputThe first line of the input contains the initial string. You can assume tha

绝尘 1173

【POJ 2887】Big String(块状数组)

【POJ 2887】Big String(块状数组) Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 6380   Accepted: 1537 Description You are given a string and supposed to

ACMer' 4976
上一篇: poj 2631 Roads in the North
下一篇: poj 3070 Fibonacci
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值