splay是支持区间操作的,先做这道题入个门

大多数操作都和普通splay一样,就不多解释了,只解释一下不大一样的操作

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define INF 0x3f3f3f3f
  4. inline int read(){
  5. int w=,f=;
  6. char ch=getchar();
  7. while(ch<''||ch>''){
  8. if(ch=='-') f=-;
  9. ch=getchar();
  10. }
  11. while(ch>=''&&ch<=''){
  12. w=(w<<)+(w<<)+ch-;
  13. ch=getchar();
  14. }
  15. return w*f;
  16. }
  17. int n,m,tot,cnt,root;
  18. struct node{
  19. int ch[],sum,cnt,val,f,rev;//比普通平衡树多一个lazy tag
  20. }st[];
  21. inline void push_up(int p){
  22. st[p].sum=st[st[p].ch[]].sum+st[st[p].ch[]].sum+st[p].cnt;
  23. }
  24. inline bool identify(int p){
  25. return st[st[p].f].ch[]==p;
  26. }
  27. inline void connect(int x,int fa,int son){
  28. st[x].f=fa;st[fa].ch[son]=x;return;
  29. }
  30. inline void rotate(int x){
  31. int y=st[x].f;int z=st[y].f;
  32. int yson=identify(x);int zson=identify(y);
  33. int b=st[x].ch[yson^];
  34. connect(b,y,yson);connect(y,x,(yson^));connect(x,z,zson);
  35. push_up(y);push_up(x);return;
  36. }
  37. inline void splay(int x,int goal){
  38. while(st[x].f!=goal){
  39. int y=st[x].f;int z=st[y].f;
  40. int yson=identify(x);int zson=identify(y);
  41. if(z!=goal){
  42. if(yson==zson) rotate(y);
  43. else rotate(x);
  44. }
  45. rotate(x);
  46. }
  47. if(!goal) root=x;
  48. return;
  49. }
  50. inline void insert(int x){
  51. int now=root;int f=;
  52. while(st[now].val!=x&&now){
  53. f=now;
  54. now=st[now].ch[x>st[now].val];
  55. }
  56. if(now){
  57. st[now].cnt++;
  58. }
  59. else{
  60. tot++;now=tot;
  61. if(f){
  62. st[f].ch[x>st[f].val]=now;
  63. }
  64. st[now].ch[]=st[now].ch[]=;
  65. st[now].cnt=st[now].sum=;
  66. st[now].val=x;st[now].f=f;
  67. }
  68. splay(now,);return;
  69. }
  70. inline void push_down(int p){
  71. int ls=st[p].ch[];int rs=st[p].ch[];
  72. if(st[p].rev){
  73. swap(st[p].ch[],st[p].ch[]);
  74. st[st[p].ch[]].rev^=;
  75. st[st[p].ch[]].rev^=;
  76. st[p].rev=;
  77. }
  78. }
  79. inline void find(int x){
  80. int now=root;if(!now) return;
  81. while(st[now].val!=x&&st[now].ch[x>st[now].val]){
  82. now=st[now].ch[x>st[now].val];
  83. }
  84. splay(now,);return;
  85. }
  86. inline int Next(int x,int f){
  87. find(x);int now=root;
  88. if(st[now].val<x&&!x) return now;
  89. if(st[now].val>x&&x) return now;
  90. now=st[now].ch[f];
  91. while(st[now].ch[f^]) now=st[now].ch[f^];
  92. return now;
  93. }
  94. inline int k_th(int x){
  95. int now=root;
  96. if(st[now].sum<x) return false;
  97. while(true){
  98. push_down(now);//在查找的时候记得下移标记
  99. int ls=st[now].ch[];
  100. if(x>st[ls].sum+st[now].cnt){
  101. x-=st[ls].sum+st[now].cnt;
  102. now=st[now].ch[];
  103. }
  104. else if(x<=st[ls].sum){
  105. now=ls;
  106. }
  107. else return now;//这个地方把返回原值改成返回位置
  108. }
  109. }inline void rev(int l,int r){
  110. int x=k_th(l-);int y=k_th(r+);
  111. splay(x,);splay(y,x);
  112. st[st[y].ch[]].rev^=;
  113. }//翻转的操作就是将l-1转到根上,r+1转到根的右儿子,然后l到r这个区间就是根右儿子的左儿子(比较绕,可以画个图想一想
  114. inline void output(int p){
  115. push_down(p);
  116. if(st[p].ch[]) output(st[p].ch[]);
  117. if(st[p].val>=&&st[p].val<=n) printf("%d ",st[p].val);
  118. if(st[p].ch[]) output(st[p].ch[]);
  119. }//输出的时候下推一下标记,输出顺序就是二叉树的顺序
  120. int main(){
  121. n=read();m=read();int i,j,k;
  122. insert(INF);insert(-INF);
  123. for(i=;i<=n;i++){
  124. insert(i);
  125. }
  126. while(m--){
  127. int x,y;x=read();y=read();
  128. rev(x+,y+);
  129. }
  130. output(root);
  131. return ;
  132. }

文艺平衡树 lg3391(splay维护区间入门)的更多相关文章

  1. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2202  Solved: 1226[Submit][Sta ...

  2. 【模板】文艺平衡树(Splay) 区间翻转 BZOJ 3223

    您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 N,M<= ...

  3. splay模板三合一 luogu2042 [NOI2005]维护数列/bzoj1500 [NOI2005]维修数列 | poj3580 SuperMemo | luogu3391 【模板】文艺平衡树(Splay)

    先是维修数列 题解看这里,但是我写的跑得很慢 #include <iostream> #include <cstdio> using namespace std; int n, ...

  4. P3391 【模板】文艺平衡树(Splay)新板子

    P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...

  5. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  6. 洛谷 P3391 【模板】文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  7. 洛谷 P3391【模板】文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  8. 文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  9. 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

随机推荐

  1. 剑指offer-面试题32-分行从上到下打印二叉树-二叉树遍历

    /* 题目: 分行按层自上向下打印二叉树. */ /* 思路: 使用队列,将节点压入队列中,再弹出来,压入其左右子节点,循环,直到栈为空. 添加两个计数器,current记录当前行的节点数,next记 ...

  2. P1980 计数问题(int,string,stringstream)

    题目描述 试计算在区间 1 到 n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1 到 11 中,即在 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 4 ...

  3. 一文看懂AI深度学习丨曼孚科技

    深度学习(Deep Learning)是机器学习的一种,而机器学习是实现人工智能的必经途径. 目前大部分表现优异的AI应用都使用了深度学习技术,引领了第三次人工智能的浪潮. 一. 深度学习的概念 深度 ...

  4. wxpython 简单例子:显示文本框的窗口显示鼠标位置

    简单例子来自教程: #!/bin/env python import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__ ...

  5. Spring mvc拦截器防御CSRF攻击

    CSRF(具体参考百度百科) CSRF(Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSR ...

  6. 深入理解Java内存模型中的虚拟机栈

    深入理解Java内存模型中的虚拟机栈 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域,这些区域都会有各自的用途,以及创建和销毁的时间,有的区域会随着虚拟机进程的启 ...

  7. udp_demo(傻瓜来回发送)

    代码讲解 import socket # 发送数据 def send_data(udp_socket, dest_ip, dest_port): send_msg = input('请输入要发送的数据 ...

  8. 微信小程序调起支付API

    官方文档: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7 https://developers.weixin.q ...

  9. Costco这样的超级零售商,能不能干掉电商?

    名创优品创始人叶国富曾说过,Costco只是没有来到中国(大陆),如果它来了,中国现在的零售业全部都会"死光".叶国富的话,似乎一语成箴. 随着Costco正式入华,其正在彻底搅动 ...

  10. vue动态绑定图片和背景图

    1.动态绑定图片 <img class="binding-img" :src="require('../assets/images/test.png')" ...