【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树
让蒟蒻见识到了常数大+滥用STL的危害。
<法一>很久之前的Splay
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 110000
#define INF 2147483647
int n,m,l,r,fa[maxn],c[maxn][2],val[maxn],head,tail,root,tot,siz[maxn];
bool delta[maxn];
inline void Maintain(int x)
{
if(x)
siz[x]=siz[c[x][0]]+siz[c[x][1]]+1;
}
inline void Pushdown(int x)
{
if(x&&delta[x])
{
if(c[x][0])
delta[c[x][0]]^=true;
if(c[x][1])
delta[c[x][1]]^=true;
swap(c[x][0],c[x][1]);
delta[x]=false;
}
}
inline void NewNode(int &x,int Fa,int key)
{
x=++tot;
fa[x]=Fa;
val[x]=key;
siz[x]=1;
}
inline void Rotate(int x,bool flag)//flag指左旋还是右旋;
{
int y=fa[x];
Pushdown(x);
Pushdown(y);
c[y][!flag]=c[x][flag];
fa[c[x][flag]]=y;
if(fa[y])
c[fa[y]][c[fa[y]][1]==y]=x;
fa[x]=fa[y];
c[x][flag]=y;
fa[y]=x;
Maintain(y);
}
inline void Splay(int x,int goal)//指要将x旋转为goal节点的子节点 :双旋!!!!
{
if(!x||x==goal)
return;
int y;
Pushdown(x);
while((y=fa[x])!=goal)
{
if(fa[y]==goal)
Rotate(x,c[y][0]==x);
else
{
if((c[y][0]==x)==(c[fa[y]][0]==y))
Rotate(y,c[fa[y]][0]==y);
else
{
Rotate(x,c[y][0]==x);
y=fa[x];
}
Rotate(x,c[y][0]==x);
}
}
Maintain(x);
if(!goal)
root=x;
}
inline int Find(int &root,int key)
{
int x=root;
while(c[x][val[x]<key])
{
if(val[x]==key)
return x;
x=c[x][val[x]<key];
}
return x;
}
inline void Insert(int &root,int key)
{
if(!root)
{
NewNode(root,0,key);
return;
}
int x=Find(root,key);
if(val[x]==key)
{
Splay(x,0);
return;
}
NewNode(c[x][val[x]<key],x,key);
Splay(c[x][val[x]<key],0);
}
inline int Kth(int &root,int K)
{
int x=root;
while(1)
{
Pushdown(x);
int Siz0=siz[c[x][0]];
if(K<=Siz0)
x=c[x][0];
else if(K==Siz0+1)
break;
else
{
K-=(Siz0+1);
x=c[x][1];
}
}
return x;
}
inline void Print(int x)
{
if(!x)
return;
Pushdown(x);
Print(c[x][0]);
printf("%d ",val[x]);
Print(c[x][1]);
Maintain(x);
return;
}
int main()
{
scanf("%d%d",&n,&m);
Insert(root,0);
for(int i=1;i<=n;i++)
Insert(root,i);
Insert(root,n+1);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&l,&r);
Splay(Kth(root,l),0);
Splay(Kth(root,r+2),root);
delta[c[c[root][1]][0]]^=true;
Pushdown(c[c[root][1]][0]);
}
Splay(Kth(root,1),0);
Splay(Kth(root,n+2),root);
Print(c[c[root][1]][0]);
return 0;
}
<法二>块状链表维护翻转标记,维护块形态。50'
#include<cstdio>
#include<list>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int f,c;
inline void Read(int &x){
c=0;f=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c>='0'&&c<='9';c=getchar())(x*=10)+=(c-'0');
x*=f;
}
void Write(int x){
if(x<10)putchar(x+'0');
else{Write(x/10);putchar(x%10+'0');}
}
typedef vector<int>::iterator VER;
struct Node
{
bool Reversed;
vector<int>v;
Node(){Reversed=0;}
};
list<Node>List;
int n,m,tmp[100001],x,y,sz,L,R;
typedef list<Node>::iterator LER;
typedef pair<LER,VER> Point;
inline Point Find(const int &p)
{
int cnt=0; LER i=List.begin();
for(;i!=List.end();++i)
{
cnt+=(*i).v.size();
if(cnt>=p)
{
cnt-=(*i).v.size();
for(VER j=(*i).v.begin();j!=(*i).v.end();++j)
if((++cnt)==p)
return make_pair(i,j);
}
}
--i; return make_pair(i,(*i).v.end());
}
void Makeblock()
{
sz=sqrt(n); int tot=1; if(!sz) sz=1;
for(;tot*sz<n;++tot)
{
LER End=List.insert(List.end(),Node());
(*End).v.assign(tmp+(tot-1)*sz+1,tmp+tot*sz+1);
}
LER End=List.insert(List.end(),Node());
(*End).v.assign(tmp+(tot-1)*sz+1,tmp+n+1);
}
inline void pushdown(const LER &p)
{
if((*p).Reversed)
{
reverse((*p).v.begin(),(*p).v.end());
(*p).Reversed=0;
}
}
inline LER Split(const int &p)
{
Point Pos=Find(p);
pushdown(Pos.first);
if(Pos.second==(*Pos.first).v.begin()) return Pos.first;
LER newB=List.insert(Pos.first,Node());
(*newB).v.assign((*Pos.first).v.begin(),Pos.second);
(*Pos.first).v.erase((*Pos.first).v.begin(),Pos.second);
return Pos.first;
}
void Printans()
{
for(LER i=List.begin();i!=List.end();++i)
{
if((*i).Reversed)
for(VER j=(*i).v.end()-1;j>=(*i).v.begin();--j)
Write(*j),putchar(' ');
else
for(VER j=(*i).v.begin();j!=(*i).v.end();++j)
Write(*j),putchar(' ');
}
puts("");
}
inline void Merge(LER &a,LER &b)
{
pushdown(a);
pushdown(b);
(*a).v.insert((*a).v.begin(),(*b).v.begin(),(*b).v.end());
List.erase(b);
}
inline void MaintainList()
{
LER curB=List.begin();
while(curB!=List.end())
{
LER nexB=curB; ++nexB;
while(nexB!=List.end()
&&(*curB).v.size()+(*nexB).v.size()<=(sz<<1))
{
Merge(nexB,curB);
curB=nexB;
++nexB;
}
++curB;
}
}
inline void Reverse()
{
Read(L); Read(R);
if(L==R) return;
Point Pl=Find(L),Pr=Find(R);
if(Pl.first==Pr.first)
{
pushdown(Pl.first);
reverse(Pl.second,++Pr.second);
return;
}
LER Lb=Split(L),Rb=Split(R+1);
for(LER i=Lb;i!=Rb;++i)
(*i).Reversed^=1;
reverse(Lb,Rb);
MaintainList();
}
int main()
{
Read(n);
Read(m);
for(int i=1;i<=n;++i)
tmp[i]=i;
Makeblock();
for(int i=1;i<=m;++i)
Reverse();
Printans();
return 0;
}
【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树的更多相关文章
- [BZOJ3223]Tyvj 1729 文艺平衡树
[BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- bzoj3223: Tyvj 1729 文艺平衡树 splay裸题
splay区间翻转即可 /************************************************************** Problem: 3223 User: walf ...
- 【Splay】bzoj3223 Tyvj 1729 文艺平衡树
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #i ...
- 2018.08.05 bzoj3223: Tyvj 1729 文艺平衡树(非旋treap)
传送门 经典的平衡树问题,之前已经用splay写过一次了,今天我突发奇想,写了一发非旋treap的版本,发现挺好写的(虽然跑不过splay). 代码: #include<bits/stdc++. ...
- BZOJ3223——Tyvj 1729 文艺平衡树
1.题目大意:维护序列,只有区间翻转这个操作 2.分析:splay的经典操作就是实现区间翻转,就是在splay中有一个标记,表示这个区间被翻转了 然后就是记得各种的操作访问某个点时,记得下传,顺便交换 ...
- BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap
一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
随机推荐
- CentOS 安装 debuginfo-install
安装debuginfo相关的包步骤如下: 1. 修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo中的enabled参数,将其值修改为1 2. 使用命令: yum i ...
- netty学习指南
这段时间领导让我熟悉Socket开发,我花了三周时间左右去学习相关的知识,包括Java socket开发,重点学习了netty这个异步非阻塞通信框架. 在这里把我学习过程中遇到的有用资料整理了,供大家 ...
- Spring 学习笔记(三)之注解
一.在classpath下扫描组件 •组件扫描(component scanning): Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. •特定组件包括: ...
- loj6043 「雅礼集训 2017 Day7」蛐蛐国的修墙方案
传送门:https://loj.ac/problem/6043 [题解] 我们考虑这是个置换,所以一定形成了很多不相交的环. 对于每个环,我们只能选一段.不选.选一段.不选这样交替下去. 显然只有偶环 ...
- 汕头市队赛 C SRM 05 - YYL 杯 R1 T3!
C SRM 05 - YYL 杯 R1 背景 tjmak 描述 给一个大小为n的序列V.序列里的元素有正有负.问至少要删除多少个元素使得序列里不存在区间(要求非空)和 >= S.如果答案大于m, ...
- 【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会
[算法]树型DP||树的重心(贪心) [题解] 两遍DFS,第一次得到所有节点子树的路径和,第二次给出除了该子树外其它部分的路径和,时时计算答案. long long!!! #include<c ...
- 归档普通对象Demo示例程序源代码
源代码下载链接:06-归档普通对象.zip34.2 KB // MJPerson.h // // MJPerson.h // 06-归档普通对象 // // Created by apple o ...
- 【洛谷】P1648 看守 (数学)
题目链接 直接暴力搞\(O(n^2)\)显然是布星滴. 试想,若是一维,最远距离就是最大值减最小值. 现在推广到二维,因为有绝对值的存在,所以有四种情况 \((x1+y1) - (x2+y2), (x ...
- 【洛谷 P1445】 [Violet]樱花(唯一分解定理)
做了题还是忍不住要写一发题解,感觉楼下的不易懂啊. 本题解使用latex纯手写精心打造. 题意:求\(\frac{1}{x}+\frac{1}{y}=\frac{1}{n!}\)的正整数解总数. 首先 ...
- 转: Java安全停止线程方法
转: http://blog.csdn.net/flyingpig4/article/details/7675557 1.早期Java提供java.lang.Thread类型包含了一些列的方法star ...