首页 > 其他分享 >*【学习笔记】(10) 块状链表

*【学习笔记】(10) 块状链表

时间:2023-08-19 22:47:59浏览次数:61  
标签:opt 10 ch int pos len 链表 块状 read

块状链表(尚未完善)

对于线性表,可以 \(O(1)\) 的访问,但是插入和删除操作是 \(O(n)\)

对于链表,可以 \(O(1)\) 的进行插入和删除,但是是 \(O(n)\) 的访问。

于是本着分块的思想,有了块状链表 。

大概长这个样子。每个块的大小数量级在 \(O(\sqrt{n})\) , 块数的量级 \(O(\sqrt{n})\)

主要有以下几种操作:

插入

1.分裂节点 \(O(\sqrt{n})\)
2.在分裂点插入 \(O(\sqrt{n})\)

删除

1.删除开头节点的后半部分 \((\sqrt{n})\)

2.删除中心完整节点 \(O(\sqrt{n})\)

3.删除结尾节点的前半部分 \(O(\sqrt{n})\)

合并

为了保证正确的复杂度,要不定期的进行合并操作。

所谓合并操作,就是从第一个块开始,如果把下一个块合并过来之后大小不大于 \(O(\sqrt{n})\),就把两个块合并

若没有合并操作,则可能会有很多小块,导致 TLE

P4008 [NOI2003] 文本编辑器

暂时不想打,有时间的话会补回来的

可持久化杀手——rope

#include<ext/rope> // 头文件 
using namespace __gnu_cxx; //引用 rope

rope<char> a; //建立一个存储 char 的 rope
// rope<变量类型> 变量名称;
crope a; //实际就是 rope<char> a

// 下标从 0 开始
a.push_back(x)    //在末尾添加 x
a.insert(pos, x)   //在 pos 插入 x
a.erase(pos, x)    //从 pos 开始删除 x个
a.replace(pos, x)  //从 pos 开始换成 x
a.substr(pos, x)   //提取 pos开始 x个
a.at(x) / a[x]   //访问第 x个元素
a.length() / a.size() //返回 a 的大小
a.copy(pos, x, s) //将从 pos位置开始 x个元素提取到 s中  O(1)
a.append(string &s / int *a, int 串s的起始位置, int 长度)  //插入 s的 pos开始的 n位  (暂时还不知道怎么用)
//如果需要翻转平衡树,就维护一正一反两个rope,翻转就把两个rope交换一下就行了。

P4008 [NOI2003] 文本编辑器

用上述的操作就可以实现了

#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
inline int read(){
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-') f = -f; ch = getchar();}
	while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
	return x * f; 
}
crope a;
int m, pos;
inline void in(int x){
	int pos1 = pos;
	while(x){
		char ch = getchar();
		if((int)ch >= 32 && (int)ch <= 126) a.insert(pos1, ch), --x, pos1++;
	}
	return ;
}
int main(){
	m = read();
	while(m--){
		char opt[51]; scanf("%s", opt + 1);
		if(opt[1] == 'M') pos = read();
		else if(opt[1] == 'I') in(read());
		else if(opt[1] == 'D') a.erase(pos, read());
		else if(opt[1] == 'G') cout << a.substr(pos, read()) << endl;
		else if(opt[1] == 'P') --pos;
		else if(opt[1] == 'N') ++pos;
	}
	return 0;
} 

P4567 [AHOI2006]文本编辑器

较上题多了反转操作,我们可以维护两个 rope ,一个正序,一个逆序

#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int N = 1 << 22;
inline int read(){
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-') f = -f; ch = getchar();}
	while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
	return x * f; 
}
rope<char> a, b, t;
int m, pos;
char a1[N], a2[N];
inline void in(int x){
	int pos1 = pos;
	for(int i = 0; i < x; ++i) a1[i] = a2[x - i - 1] = getchar();
	a1[x] = '\0', a2[x] = '\0';
	int len = a.size();
	a.insert(pos, a1), b.insert(len - pos, a2);
	return ;
}
inline void del(int x){
	int len = a.size();
	a.erase(pos, x), b.erase(len - pos - x, x);
}
inline void rotate(int x){
	int len = a.size();
	t = a.substr(pos, x);
	a = a.substr(0, pos) + b.substr(len - pos - x, x) + a.substr(pos + x, len - pos - x);
	b = b.substr(0, len - pos - x) + t + b.substr(len - pos, pos);
}
inline void out(){
	putchar(a[pos]);
	if(a[pos] != '\n') putchar('\n');
}
int main(){
	cin.tie(0), cout.tie(0);
	m = read();
	while(m--){
		char opt[51]; scanf("%s", opt + 1);
		if(opt[1] == 'M') pos = read();
		else if(opt[1] == 'I') in(read());
		else if(opt[1] == 'D') del(read());
		else if(opt[1] == 'G') out();
		else if(opt[1] == 'P') --pos;
		else if(opt[1] == 'N') ++pos;
		else if(opt[1] == 'R') rotate(read());
	}
	return 0;
} 

P3850 [TJOI2007]书架

按题意操作即可

#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int N = 2e5 + 51;
inline int read(){
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-') f = -f; ch = getchar();}
	while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
	return x * f; 
}
int n, m, q;
rope<int> a;
string s[N];
int main(){
	cin.tie(0), cout.tie(0);
	n = read();
	for(int i = 1; i <= n; ++i) cin >> s[i], a.push_back(i);
	m = read();
	for(int i = 1; i <= m; ++i) cin >> s[i + m], a.insert(read(), i + m);
	q = read();
	while(q--) cout << s[a.at(read())] << endl;
	return 0;
} 

标签:opt,10,ch,int,pos,len,链表,块状,read
From: https://www.cnblogs.com/jiangchen4122/p/17417628.html

相关文章

  • 力扣101. 对称二叉树
    给你一个二叉树的根节点 root ,检查它是否轴对称。 示例1:输入:root=[1,2,2,3,4,4,3]输出:true 示例2:输入:root=[1,2,2,null,3,null,3]输出:false  提示:树中节点数目在范围 [1,1000] 内-100<=Node.val<=100 康复训练1/*2*@lcapp=......
  • 强化学习算法如何将GPU利用率提高到100%——在线强化学习如何将GPU利用率提升至100%
    一直有个疑问,那就是“强化学习算法如何将GPU利用率提高到100%”,在一些论坛中也有人会提出这样的问题,但是一直也没有人比较正面的回答过这个问题,为此正好自己又想到了这么一个问题,于是想在这里正面的谈论下这个问题。......
  • MacbookPro 17年款老机器升级Macos10.15.7挺好的
    MacbookPro17年款老机器升级Macos10.15.7挺好的由于需要安装一些软件,至少需要10.14或者10.15,所以,把MacBookPro17年款的老机器进行了升级,原装的系统是10.12.6.安装之前在网上搜索了各种升级的利弊,有升级成功的,也有很多说升级之后不能使用,然后又降级的。搞得犹豫了好一会,最后还......
  • 2-10-Feign-最佳实践分析(11-Feign-实现Feign最佳实践)
    所谓的最佳实践是针对发请求与收请求两个接口而言的总共分两种规范:继承+抽取由于继承会出现多次实现且不同模块的维护人还不一样要是出现更新人力安排也是一个问题抽取方式则不会出现这些问题因为实现仅一份而且还都是由服务维护方维护的不存在人力安排问题从生产者......
  • 【10.0】后台首页搭建和banner
    【一】创建后台主页模块python../../manage.pystartapphome【二】创建模型表(轮播图)luffyCity\luffyCity\utils\common_models.pyfromdjango.dbimportmodelsclassBaseModel(models.Model):'''公共字段创建基表----其他表也可能用idim......
  • vue.js:5108 [Vue warn]: Cannot find element: #body_container
    1、原因:我把Vue挂载元素的JS放在了html加载完成的前面了2、解决:放到html加载完成之后就可以了 ......
  • 【wxauto】新版PC端微信报错:LookupError: Find Control Timeout(10s): {Name: ‘输入
    微信版本:3.9.5.81调用后报错“LookupError:FindControlTimeout(10s):{Name:'输入',ControlType:EditControl}”按照Issues#107说的修改后是不报错,但是没有效果,不能自动发送消息 解决方案:在wxauto.py的文件中找到WeChat的类,并添加下述方法defChangeWindo......
  • LeetCode 1020.飞地的数量
    1.题目:给你一个大小为 mxn 的二进制矩阵 grid ,其中 0 表示一个海洋单元格、1 表示一个陆地单元格。一次 移动 是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过 grid 的边界。返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数......
  • 洛谷P5410 【模板】扩展 KMP(Z 函数)题解
    题目链接P5410【模板】扩展KMP(Z函数)-洛谷|计算机科学教育新生态(luogu.com.cn) 分析先考虑z数组设nx[i]为字符串b与b以b[i]开头的后缀最长公共前缀设i为当前需要求的位置当前i+nx[i]-1的最大值所对应的i为q p为i对应的位置当i大于q+nx[q......
  • 【故障公告】多年的故障老朋友又来了:数据库服务器 CPU 100%
    数据库服务器CPU100%问题几乎每年都要来几次,从来都不事先打一声招呼,今年的第2次在我们正忙着会员救园的时候来了。今天13:35首先收到我们自己的异常告警通知:ExecutionTimeoutExpired.Thetimeoutperiodelapsedpriortocompletionoftheoperationortheserver......