当前位置:网站首页>[Luogu] p1383 advanced typewriter
[Luogu] p1383 advanced typewriter
2022-07-26 05:02:00 【Record algorithm solution】
Title address :
https://www.luogu.com.cn/problem/P1383
Title Description :
Zaomiao started with the latest advanced typewriter . The latest model naturally has different functions from the past , It has Undo function , Terrible! . Please design a program for this advanced typewriter , Support the following 3 3 3 Kind of operation :
1.T x: Type a lowercase letter at the end of the article x x x.(Type operation )
2.U x: Undo the last x x x The first modification operation .(Undo operation )( Be careful Query Operation is not a modification operation )
3.Q x: Ask the number in the current article x x x Letters and output .(Query operation )
The article can be regarded as an empty string at the beginning .
Input format :
The first 1 1 1 That's ok : An integer n n n, Represents the number of operations .
following n n n That's ok , One command per line . Ensure that the input command is legal .
Output format :
Output one letter per line , Express Query The answer to the operation .
Data range :
about 40 % 40\% 40% The data of n ≤ 200 n \le 200 n≤200.
about 100 % 100\% 100% The data of n ≤ 100000 n \le 100000 n≤100000; Guarantee Undo The operation will not be undone Undo operation .
Advanced challenges :
about 200 % 200\% 200% The data of n ≤ 100000 n \le 100000 n≤100000;Undo The operation can be undone Undo operation .
IOI Challenge :
You must use an online algorithm to complete the problem .
Obviously, we should use the chairman tree . If the undo operation is not undone Undo operation , Then just roll back the version x x x Just step by step , And the version number is the length of the string ( The version number of the initial version is 0 0 0). However, due to the possible cancellation operation in this topic, the previous cancellation operation is cancelled , So when we perform the undo operation , Open a new version directly , This version is equal to x x x Previous version . Because of this, the information of how long each version string is lost , We need another array to record this information . The code is as follows :
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n;
struct Node {
int l, r;
char ch;
} tr[(N << 2) + N * 17];
// root[i] by i The root subscript of the No. version ,len[i] by i The string length of version number ,ver Number the current version
int root[N], idx, len[N], ver;
int build(int l, int r) {
int p = ++idx;
if (l == r) return p;
int mid = l + r >> 1;
tr[p].l = build(l, mid), tr[p].r = build(mid + 1, r);
return p;
}
int update(int p, int l, int r, int k, char ch) {
int q = ++idx;
tr[q] = tr[p];
if (l == r) tr[q].ch = ch;
else {
int mid = l + r >> 1;
if (k <= mid) tr[q].l = update(tr[q].l, l, mid, k, ch);
else tr[q].r = update(tr[q].r, mid + 1, r, k, ch);
}
return q;
}
char query(int p, int l, int r, int k) {
if (l == r) return tr[p].ch;
int mid = l + r >> 1;
if (k <= mid) return query(tr[p].l, l, mid, k);
else return query(tr[p].r, mid + 1, r, k);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
char op, ch;
int x;
cin >> op;
if (op == 'T') {
cin >> ch;
ver++;
len[ver] = len[ver - 1] + 1;
root[ver] = update(root[ver - 1], 1, n, len[ver], ch);
} else {
cin >> x;
if (op == 'U') {
// For undo , Open a new version directly , It is equivalent to x Pre step version
ver++;
root[ver] = root[ver - x - 1];
len[ver] = len[ver - x - 1];
} else cout << query(root[ver], 1, n, x) << '\n';
}
}
}
operation 1 1 1 and 3 3 3 Time complexity O ( log n ) O(\log n) O(logn), operation 2 2 2 Time O ( 1 ) O(1) O(1), Space O ( n log n ) O(n\log n) O(nlogn).
边栏推荐
- 2022 Henan Mengxin League game (3): Henan University a - corn cannon
- 2022 Henan Mengxin League game (3): Henan University J - magic number
- What are the restrictions on opening futures accounts? Where is the safest place to open an account?
- 2022 a.static query on tree (tree section)
- Nacos 介绍和部署
- Switch and router technology: dynamic routing protocol, rip routing protocol and OSPF routing protocol
- Excel VBA:实现自动下拉填充公式至最后一行
- [mathematical modeling] basic knowledge of MATLAB
- Google Emoji guessing game helps parents guide their children to surf the Internet safely
- What is the difference between asynchronous and synchronous transmission signals (electronic hardware)
猜你喜欢

SQL加解密注入详解

How to connect tdengine through idea database management tool?

What are the demand management software for small and medium-sized enterprises

【语义分割】2018-DeeplabV3+ ECCV

Rman-06031 cannot convert database keywords

What are the characteristics of the grammar of Russian documents in the translation of scientific papers

创建MySQL数据库的两种方式

阿里三面:MQ 消息丢失、重复、积压问题,如何解决?

Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!

Calculate the curvature of discrete points (matlab)
随机推荐
C语言——字符串函数,内存函数集锦以及模拟实现
Rman-06031 cannot convert database keywords
你对“happen-before原则”的理解可能是错的?
提高shuffle操作中的reduce并行度
To study the trend of open source and gain insight into the future of the industry, stonedb community and the China Academy of communications and communications released the Research Report on the dev
Redis过期删除策略和内存淘汰策略
columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by mysql8.0解决办法
Axi protocol (4): signals on the Axi channel
STM32 development | ad7606 parallel multi-channel data acquisition
CountLaunch Demo的测试
Add and modify the verification logic, and use -validation- to complete the group verification
C language lseek() function: move the read and write location of the file
基于R语言的Meta分析【全流程、不确定性分析】方法与Meta机器学习
Ansible tutorial
【Leetcode】493. Reverse Pairs
AXI协议(5):AXI协议的burst机制
Fill in the vacancy, and fill in later
基于通用优化软件GAMS的数学建模和优化分析
Distance between bus stops: simple simulation problem
MySQL基础学习