2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!!

模板题水啊水~~~

第一次写块状链表,先写一个模板题(⊙o⊙)

块状链表虽然效率大概在O(n√n),但它几乎没有常数,相比较理论上复杂度较快的O(nlogn)的Splay还是要快,因为Splay常数太大啦!感觉比较笨重。也许这就是许多OIer热衷分块的原因吧。

BZOJ 1507(因为O2优化,没有内存池)

  1. #include<cmath>
  2. #include<queue>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  7. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  8. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  9. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  10. #define CC(i,a) memset(i,a,sizeof(i))
  11. using namespace std;
  12. int now=0,min_tot=0,max_tot=0,tot=0,sq;
  13. struct BLOCK{
  14. BLOCK();
  15. BLOCK *next;
  16. char c[100003];
  17. int size;
  18. }*head,*null;
  19. BLOCK::BLOCK(){next=null;size=0;}
  20. inline void build(){
  21. null=new BLOCK;
  22. *null=BLOCK();
  23. head=new BLOCK;
  24. }
  25. inline BLOCK *find(int &k){
  26. BLOCK *r=head;
  27. while (k-r->size>0&&r->next!=null) k-=r->size,r=r->next;
  28. return r;
  29. }
  30. inline void cut(BLOCK *r,int pos){
  31. BLOCK *k=new BLOCK;
  32. k->size=r->size-pos;
  33. for1(i,pos+1,r->size) k->c[i-pos]=r->c[i];
  34. r->size=pos;
  35. k->next=r->next;
  36. r->next=k;
  37. }
  38. inline void merge(BLOCK *r){
  39. if (r->next==null) return;
  40. BLOCK *k=r->next;
  41. for1(i,1,k->size) r->c[r->size+i]=k->c[i];
  42. r->size+=k->size;
  43. r->next=k->next;
  44. delete k;
  45. }
  46. inline void balance(){
  47. BLOCK *r=head;
  48. sq=floor(sqrt(tot)); min_tot=sq/2; max_tot=sq*2;
  49. while (r!=null){
  50. if (r->size<min_tot) if (r->next==null) return; else {merge(r); continue;}
  51. else if (r->size>max_tot) {cut(r,r->size/2); continue;}
  52. r=r->next;
  53. }
  54. }
  55. inline void work1(int x){
  56. int pnow=now; BLOCK *r=find(pnow),*k=new BLOCK,*sta=k;
  57. cut(r,pnow);
  58. for1(i,1,x){
  59. char cs=getchar();
  60. while (cs=='\n') cs=getchar();
  61. k->size++; k->c[k->size]=cs;
  62. if (k->size>=sq){
  63. k->next=new BLOCK;
  64. k=k->next;
  65. }
  66. }k->next=r->next;r->next=sta;
  67. }
  68. inline void work2(int x){
  69. int pnow=now; BLOCK *r=find(pnow);
  70. if (x<=r->size-pnow){
  71. cut(r,pnow); cut(r->next,x);
  72. BLOCK *xx=r->next;
  73. r->next=r->next->next;
  74. delete xx;
  75. return;
  76. }
  77. x-=r->size-pnow;
  78. cut(r,pnow);
  79. BLOCK *xy=r->next;
  80. r->next=r->next->next;
  81. delete xy;
  82. while (x-r->next->size>0&&r->next!=null){
  83. x-=r->next->size;
  84. BLOCK *xx=r->next;
  85. r->next=r->next->next;
  86. delete xx;
  87. }cut(r->next,x);
  88. BLOCK *xx=r->next;
  89. r->next=r->next->next;
  90. delete xx;
  91. }
  92. inline void work3(int x){
  93. int pnow=now; BLOCK *r=find(pnow);
  94. if (x<=r->size-pnow){for1(i,pnow+1,pnow+x) putchar(r->c[i]); return;}
  95. for1(i,pnow+1,r->size) putchar(r->c[i]);
  96. x-=r->size-pnow; r=r->next;
  97. while (x-r->size>0&&r!=null){
  98. for1(i,1,r->size) putchar(r->c[i]);
  99. x-=r->size; r=r->next;
  100. }for1(i,1,x)putchar(r->c[i]);
  101. }
  102. int main(){
  103. build();
  104. int n;scanf("%d\n",&n);
  105. while (n--){
  106. char str=getchar(),st=getchar();
  107. int x;
  108. switch(str){
  109. case 'I':
  110. while (st!=' ') st=getchar();
  111. scanf("%d",&x); tot+=x; sq=floor(sqrt(tot)); min_tot=sq/2; max_tot=sq*2;
  112. work1(x);
  113. balance();
  114. scanf("\n");
  115. break;
  116. case 'M':
  117. while (st!=' ') st=getchar();
  118. scanf("%d\n",&now);
  119. break;
  120. case 'D':
  121. while (st!=' ') st=getchar();
  122. scanf("%d\n",&x); work2(x);
  123. tot-=x; balance();
  124. break;
  125. case 'G':
  126. while (st!=' ') st=getchar();
  127. scanf("%d\n",&x); work3(x); printf("\n");
  128. break;
  129. case 'P':
  130. now--;st=getchar();st=getchar();st=getchar();
  131. break;
  132. case 'N':
  133. now++;st=getchar();st=getchar();st=getchar();
  134. break;
  135. }
  136. }
  137. return 0;
  138. }

Tyvj P2388(Windows下评测的new和delete慢得飞起,所以写了个内存池)

  1. #include<cmath>
  2. #include<queue>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  7. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  8. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  9. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  10. #define CC(i,a) memset(i,a,sizeof(i))
  11. using namespace std;
  12. int now=0,min_tot=0,max_tot=0,tot=0,sq;
  13. struct BLOCK{
  14. BLOCK();
  15. BLOCK *next;
  16. char c[10003];
  17. int size;
  18. }*head,*null,pool[10003];
  19. int top=0;
  20. BLOCK::BLOCK(){next=null;size=0;}
  21. inline BLOCK *newBLOCK(){
  22. BLOCK *t=&pool[top++];
  23. t->next=null; t->size=0;
  24. return t;
  25. }
  26. inline void build(){
  27. null=newBLOCK();
  28. null->next=null; null->size=0;
  29. head=newBLOCK();
  30. }
  31. inline BLOCK *find(int &k){
  32. BLOCK *r=head;
  33. while (k-r->size>0&&r->next!=null) k-=r->size,r=r->next;
  34. return r;
  35. }
  36. inline void cut(BLOCK *r,int pos){
  37. BLOCK *k=newBLOCK();
  38. k->size=r->size-pos;
  39. for1(i,pos+1,r->size) k->c[i-pos]=r->c[i];
  40. r->size=pos;
  41. k->next=r->next;
  42. r->next=k;
  43. }
  44. inline void merge(BLOCK *r){
  45. if (r->next==null) return;
  46. BLOCK *k=r->next;
  47. for1(i,1,k->size) r->c[r->size+i]=k->c[i];
  48. r->size+=k->size;
  49. r->next=k->next;
  50. //delete k;
  51. }
  52. inline void balance(){
  53. BLOCK *r=head;
  54. sq=floor(sqrt(tot)); min_tot=sq/2; max_tot=sq*2;
  55. while (r!=null){
  56. if (r->size<min_tot) if (r->next==null) return; else {merge(r); continue;}
  57. else if (r->size>max_tot) {cut(r,r->size/2); continue;}
  58. r=r->next;
  59. }
  60. }
  61. inline void work1(int x){
  62. int pnow=now; BLOCK *r=find(pnow),*k=newBLOCK(),*sta=k;
  63. cut(r,pnow);
  64. for1(i,1,x){
  65. char cs=getchar();
  66. while (cs=='\n') cs=getchar();
  67. k->size++; k->c[k->size]=cs;
  68. if (k->size>=sq){
  69. k->next=newBLOCK();
  70. k=k->next;
  71. }
  72. }k->next=r->next;r->next=sta;
  73. }
  74. inline void work2(int x){
  75. int pnow=now; BLOCK *r=find(pnow);
  76. if (x<=r->size-pnow){
  77. cut(r,pnow); cut(r->next,x);
  78. BLOCK *xx=r->next;
  79. r->next=r->next->next;
  80. //delete xx;
  81. return;
  82. }
  83. x-=r->size-pnow;
  84. cut(r,pnow);
  85. BLOCK *xy=r->next;
  86. r->next=r->next->next;
  87. //delete xy;
  88. while (x-r->next->size>0&&r->next!=null){
  89. x-=r->next->size;
  90. BLOCK *xx=r->next;
  91. r->next=r->next->next;
  92. //delete xx;
  93. }cut(r->next,x);
  94. BLOCK *xx=r->next;
  95. r->next=r->next->next;
  96. //delete xx;
  97. }
  98. inline void work3(int x){
  99. int pnow=now; BLOCK *r=find(pnow);
  100. if (x<=r->size-pnow){for1(i,pnow+1,pnow+x) putchar(r->c[i]); return;}
  101. for1(i,pnow+1,r->size) putchar(r->c[i]);
  102. x-=r->size-pnow; r=r->next;
  103. while (x-r->size>0&&r!=null){
  104. for1(i,1,r->size) putchar(r->c[i]);
  105. x-=r->size; r=r->next;
  106. }for1(i,1,x)putchar(r->c[i]);
  107. }
  108. int main(){
  109. build();
  110. int n;scanf("%d\n",&n);
  111. while (n--){
  112. char str=getchar(),st=getchar();
  113. int x;
  114. switch(str){
  115. case 'I':
  116. while (st!=' ') st=getchar();
  117. scanf("%d",&x); tot+=x; sq=floor(sqrt(tot)); min_tot=sq/2; max_tot=sq*2;
  118. work1(x);
  119. balance();
  120. scanf("\n");
  121. break;
  122. case 'M':
  123. while (st!=' ') st=getchar();
  124. scanf("%d\n",&now);
  125. break;
  126. case 'D':
  127. while (st!=' ') st=getchar();
  128. scanf("%d\n",&x); work2(x);
  129. tot-=x; balance();
  130. break;
  131. case 'G':
  132. while (st!=' ') st=getchar();
  133. scanf("%d\n",&x); work3(x); printf("\n");
  134. break;
  135. case 'P':
  136. now--;st=getchar();st=getchar();st=getchar();
  137. break;
  138. case 'N':
  139. now++;st=getchar();st=getchar();st=getchar();
  140. break;
  141. }
  142. }
  143. return 0;
  144. }

分块真心强大,,,

【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题的更多相关文章

  1. 【BZOJ-1507】Editor 块状链表

    1507: [NOI2003]Editor Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 3397  Solved: 1360[Submit][Stat ...

  2. BZOJ 4864: [BeiJing 2017 Wc]神秘物质 (块状链表/平衡树 )

    这就是一道数据结构裸题啊,最大极差就是区间最大值减最小值,最小极差就是相邻两个数差的最小值.然后平衡树splay/treap或者块状链表维护就行了. 第一次自己写块状链表,蛮好写,就是长..然后就BZ ...

  3. 【块状链表】AutSky_JadeK的块状链表模板+总结(STL版)

    Part 1.块状链表.   定位 插入 删除 数组 O(1) O(n) O(n) 链表 O(n) O(1) O(1) 对于线性表的以上常见操作来说,数组和链表都无法有效地解决.但是,若我们将链表的每 ...

  4. bzoj 4622: [NOI 2003] 智破连环阵【dfs+匈牙利算法】

    一个炸弹炸一个区间的武器,想到二分图匹配 但是直接dfs断点显然不行,预处理出dis[i]为i到m的至多值来最优性剪枝,并且标记ok[i][j]为炸弹i可以炸到j武器,mx[i][j]为i炸弹从j武器 ...

  5. BZOJ 1509[NOI 2003]逃学的小孩 树形dp

    1509: [NOI2003]逃学的小孩 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 995  Solved: 505[Submit][Status][ ...

  6. 【BZOJ 3188】【Coci 2011】Upit Splay模板题

    转啊转终于转出来了,然而我的模板跟陈竞潇学长的模板一模一样,还是太弱啊,第一次用指针. #include<cstdio> #include<cstring> #include& ...

  7. BZOJ 1036 树的统计Count 树链剖分模板题

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1036 题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将 ...

  8. [BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆)

    [BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆) 题面 给出一个长度为n的序列,选k段长度在L到R之间的区间,一个区间的值等于区间内所有元素之的和,使得k个区间的值之和最大.区 ...

  9. BZOJ 1507 Editor(块状链表)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...

随机推荐

  1. SharePoint Error:a system restart from a previous installation or update is pending

    run:regedit 打开注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager 删除PendingFileR ...

  2. OpenCV的安装与系统环境变量

    OpenCV的安装与系统环境变量 安装OpenCV本来是很简单的一件事,但配置却很麻烦.而且在配置过程中尤为重要的步骤就是系统环境变量的配置.我使用的是CodeBlick13.12与OpenCV1.0 ...

  3. POJ 3150 Cellular Automaton --矩阵快速幂及优化

    题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值. 解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有 ...

  4. MySQL数据库学习笔记(三)----基本的SQL语句

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  5. 第22章 DLL注入和API拦截(2)

    22.4 使用远程线程来注入DLL 22.4.1 概述 (1)远程线程注入是指一个进程在另一个进程中创建线程,然后载入我们编写的DLL,并执行该DLL代码的技术.其基本思路是通过CreateRemot ...

  6. js常用宽高属性

    document.body.clientWidth //body对象的宽度 document.body.clientHeight //body对象的高度 document.documentElemen ...

  7. js中的垃圾回收机制

    代码回收规则如下: 1.全局变量不会被回收. 2.局部变量会被回收,也就是函数一旦运行完以后,函数内部的东西都会被销毁. 3.只要被另外一个作用域所引用就不会被回收  (闭包)

  8. Linux Linux程序练习十(网络编程大文件发送)

    //网络编程客户端--大文件传输 #include <stdio.h> #include <stdlib.h> #include <string.h> #inclu ...

  9. ZooKeeper学习第八期——ZooKeeper伸缩性

    一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我想大家都已经知道了在ZooKeeper集群当中有两种角色Leader和Follower.Leader可以接受 ...

  10. JAVA中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowStrin ...