前序
此文主要讲的是 \(\text{Treap}\)。
你以为我是来深度解读 \(\text{Treap}\) 的吗?不不不,我是来背诵代码的(bushi)。
证明先咕着。
#include<bits/stdc++.h>
using namespace std;
const int SIZE=1e5+5;
int tot,root,n;
struct Treap
{
int l,r,val,dat,cnt,size;
}a[SIZE];
int New(int val)
{
a[++tot].val=val; a[tot].dat=rand();a[tot].cnt=a[tot].size=1;
return tot;
}
void Update(int p){a[p].size=a[a[p].l].size+a[a[p].r].size+a[p].cnt;}
void Build()
{
New(-1e9); New(1e9);
root=1,a[1].r=2; Update(root);
}
int GetRankByVal(int p,int val)
{
if(!p) return p;
if(val==a[p].val) return a[a[p].l].size+1;
if(val<a[p].val) return GetRankByVal(a[p].l,val);
return GetRankByVal(a[p].r,val)+a[a[p].l].size+a[p].cnt;
}
int GetValByRank(int p,int rank)
{
if(!p) return 1e9;
if(a[a[p].l].size>=rank) return GetValByRank(a[p].l,rank);
if(a[a[p].l].size+a[p].cnt>=rank) return a[p].val;
return GetValByRank(a[p].r,rank-a[a[p].l].size-a[p].cnt);
}
void zig(int &p)
{
int q=a[p].l;
a[p].l=a[q].r,a[q].r=p,p=q;
Update(a[p].r),Update(p);
}
void zag(int &p)
{
int q=a[p].r;
a[p].r=a[q].l,a[q].l=p,p=q;
Update(a[p].l),Update(p);
}
void Insert(int &p,int val)
{
if(!p){p=New(val);return;}
if(val==a[p].val){a[p].cnt++,Update(p);return;}
if(val<a[p].val)
{
Insert(a[p].l,val);
if(a[p].dat<a[a[p].l].dat) zig(p);
}
else
{
Insert(a[p].r,val);
if(a[p].dat<a[a[p].r].dat) zag(p);
}
Update(p);
}
int GetPre(int val)
{
int ans=1,p=root;
while(p)
{
if(val==a[p].val)
{
if(a[p].l>0)
{
p=a[p].l;
while(a[p].r>0) p=a[p].r;
ans=p;
}
break;
}
if(a[p].val<val&&a[p].val>a[ans].val) ans=p;
p=val<a[p].val?a[p].l:a[p].r;
}
return a[ans].val;
}
int GetNext(int val)
{
int ans=2,p=root;
while(p)
{
if(val==a[p].val)
{
if(a[p].r>0)
{
p=a[p].r;
while(a[p].l>0) p=a[p].l;
ans=p;
}
}
if(a[p].val>val&&a[p].val<a[ans].val) ans=p;
p=val<a[p].val?a[p].l:a[p].r;
}
return a[ans].val;
}
void Remove(int &p,int val)
{
if(!p) return;
if(val==a[p].val)
{
if(a[p].cnt>1)
{
a[p].cnt--; Update(p);
return;
}
if(a[p].l||a[p].r)
{
if(!a[p].r||a[a[p].l].dat>a[a[p].r].dat) zig(p),Remove(a[p].r,val);
else zag(p),Remove(a[p].l,val);
Update(p);
}
else p=0;
return;
}
val<a[p].val?Remove(a[p].l,val):Remove(a[p].r,val);
Update(p);
}
int main()
{
Build(); scanf("%d",&n);
while(n--)
{
int opt,x;
scanf("%d%d",&opt,&x);
switch(opt)
{
case 1:
Insert(root,x);
break;
case 2:
Remove(root,x);
break;
case 3:
printf("%d\n",GetRankByVal(root,x)-1);
break;
case 4:
printf("%d\n",GetValByRank(root,x+1));
break;
case 5:
printf("%d\n",GetPre(x));
break;
case 6:
printf("%d\n",GetNext(x));
break;
}
}
return 0;
}
这是一道平衡树入门题,\(\green{AC}\) 了之后看了看题解区,发现基本都是用其他数据结构(如线段树,分块,树状数组)实现的。事实上用平衡树实现就毫无思维难度。
首先把题目转化成形式化题意。
假设有一个序列,该序列初始只有 \(0\) 和 \(n+1\)。
操作 \(1\) D x
,等价于在序列中插入一个值为 \(x\) 的数。
操作 \(2\) R
,等价于删去一个最新插入的数。
操作 \(3\) Q x
,相当于求 \(x\) 的后继 \(-\) \(x\) 的前驱 \(-1\)。
以上 \(3\) 个操作均可以使用平衡树(\(\text{Treap}\))维护。同时记得开个栈。
事实上这道题比模板题更好写,因为不用查询排名之类的东西。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct tree
{
int l,r,val,dat;
}a[N];
int tot,root,n,m,v[N];
stack<int> s;
int New(int val)
{
a[++tot].val=val,a[tot].dat=rand();
return tot;
}
void build()
{
New(0),New(n+1),a[1].r=2,root=1;
}
void zig(int &p)
{
int q=a[p].l;
a[p].l=a[q].r,a[q].r=p,p=q;
}
void zag(int &p)
{
int q=a[p].r;
a[p].r=a[q].l,a[q].l=p,p=q;
}
void Insert(int &p,int val)
{
if(!p){p=New(val);return;}
if(val<a[p].val)
{
Insert(a[p].l,val);
if(a[p].dat<a[a[p].l].dat) zig(p);
}
else
{
Insert(a[p].r,val);
if(a[p].dat<a[a[p].r].dat) zag(p);
}
}
int GetPre(int val)
{
int ans=1,p=root;
while(p)
{
if(val==a[p].val)
{
if(a[p].l>0)
{
p=a[p].l;
while(a[p].r>0) p=a[p].r;
ans=p;
}
}
if(a[ans].val<a[p].val&&a[p].val<val) ans=p;
p=val<a[p].val?a[p].l:a[p].r;
}
return a[ans].val;
}
int GetNext(int val)
{
int ans=2,p=root;
while(p)
{
if(val==a[p].val)
{
if(a[p].r>0)
{
p=a[p].r;
while(a[p].l>0) p=a[p].l;
ans=p;
}
}
if(a[p].val>val&&a[p].val<a[ans].val) ans=p;
p=val<a[p].val?a[p].l:a[p].r;
}
return a[ans].val;
}
void Remove(int &p,int val)
{
if(!p) return;
if(val==a[p].val)
{
if(a[p].l||a[p].r)
{
if(!a[p].r||a[a[p].l].dat>a[a[p].r].dat) zig(p),Remove(a[p].r,val);
else zag(p),Remove(a[p].l,val);
}
else p=0;
return;
}
val<a[p].val?Remove(a[p].l,val):Remove(a[p].r,val);
}
int main()
{
scanf("%d%d",&n,&m); build();
while(m--)
{
char ch; int x; cin>>ch;
if(ch=='D')
{
scanf("%d",&x);
s.push(x); v[x]=1;
Insert(root,x);
}
else if(ch=='R') Remove(root,s.top()),v[s.top()]=0,s.pop();
else
{
scanf("%d",&x);
if(v[x]) puts("0");
else printf("%d\n",GetNext(x)-GetPre(x)-1);
}
}
return 0;
}
这题目名挺奇怪的
这题没有上面一题那么裸,但还是很水(对于我这种蒟蒻而言)。
注意到题目中一个很重要的性质:每个区间 \([i,j]\) 不互相包含。
这样一来,如果我们把询问离线并按 左/右 端点排序,那么每只狗至多被 加入/删除 当前的询问队列 \(2\) 次。
如果我们使用平衡树来维护,每只狗对时间复杂度的贡献只有 \(\mathcal{O}(\log n)\),同时每个询问的时间复杂度也是 \(\mathcal{O}(\log n)\),所以总的时间复杂度是 \(\mathcal{O}((n+m) \log n)\)。
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int tot,root,n,m,b[N],ans[N];
struct Treap
{
int l,r,val,dat,cnt,size;
}a[N];
struct node
{
int l,r,val,id;
}c[N];
bool cmp(node a,node b){return a.l<b.l;}
int New(int val)
{
a[++tot].val=val; a[tot].dat=rand();a[tot].cnt=a[tot].size=1;
return tot;
}
void Update(int p){a[p].size=a[a[p].l].size+a[a[p].r].size+a[p].cnt;}
void Build()
{
New(-2147483648); New(2147483647);
root=1,a[1].r=2; Update(root);
}
int GetValByRank(int p,int rank)
{
if(!p) return 1e9;
if(a[a[p].l].size>=rank) return GetValByRank(a[p].l,rank);
if(a[a[p].l].size+a[p].cnt>=rank) return a[p].val;
return GetValByRank(a[p].r,rank-a[a[p].l].size-a[p].cnt);
}
void zig(int &p)
{
int q=a[p].l;
a[p].l=a[q].r,a[q].r=p,p=q;
Update(a[p].r),Update(p);
}
void zag(int &p)
{
int q=a[p].r;
a[p].r=a[q].l,a[q].l=p,p=q;
Update(a[p].l),Update(p);
}
void Insert(int &p,int val)
{
if(!p){p=New(val);return;}
if(val==a[p].val){a[p].cnt++,Update(p);return;}
if(val<a[p].val)
{
Insert(a[p].l,val);
if(a[p].dat<a[a[p].l].dat) zig(p);
}
else
{
Insert(a[p].r,val);
if(a[p].dat<a[a[p].r].dat) zag(p);
}
Update(p);
}
void Remove(int &p,int val)
{
if(!p) return;
if(val==a[p].val)
{
if(a[p].cnt>1)
{
a[p].cnt--; Update(p);
return;
}
if(a[p].l||a[p].r)
{
if(!a[p].r||a[a[p].l].dat>a[a[p].r].dat) zig(p),Remove(a[p].r,val);
else zag(p),Remove(a[p].l,val);
Update(p);
}
else p=0;
return;
}
val<a[p].val?Remove(a[p].l,val):Remove(a[p].r,val);
Update(p);
}
int main()
{
Build(); scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
for(int i=1;i<=m;i++) scanf("%d%d%d",&c[i].l,&c[i].r,&c[i].val),c[i].id=i;
sort(c+1,c+1+m,cmp);
int l=0,r=0;
for(int i=1;i<=m;i++)
{
if(c[i].l>r)
{
for(int j=l;j<=r;j++) Remove(root,b[j]);
for(int j=c[i].l;j<=c[i].r;j++) Insert(root,b[j]);
}
else
{
for(int j=l;j<=c[i].l-1;j++) Remove(root,b[j]);
for(int j=r+1;j<=c[i].r;j++) Insert(root,b[j]);
}
ans[c[i].id]=GetValByRank(root,c[i].val+1);
l=c[i].l,r=c[i].r;
}
for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
return 0;
}
不管怎么说这题是一道紫题...所以说还是有一定的实现难度。
操作一 先对每一本书从上到下赋一个权值,并在权值前留 \(m\) 个空位,权值后也同样留 \(m\) 个空位(方便后面的操作一和操作二)。拿两个指针 \(mn,mx\) 记录最上面的书本权值和最下面的书本权值。那么对于操作一,现将 \(s\) 从平衡树里删除,再以 \(--mn\) 插入到平衡树。
操作二 和操作一一样的做法,\(++mx\) 即可。
操作三 直接分类讨论。对于 \(t=1\),交换 \(s\) 和 \(s\) 的后继即可。对于 \(t=-1\),交换 \(s\) 和 \(s\) 的前驱即可。
操作四 根据 \(s\) 的权值查询排名即可。
操作五 最平凡的操作。根据排名查询即可。
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int tot,root,n,m,v[N],mx,mn,mp[N];
char ch[10];
struct Treap
{
int l,r,val,dat,size,id;
}a[N];
int New(int val,int id)
{
a[++tot].val=val; a[tot].dat=rand(); a[tot].size=1; a[tot].id=id;
return tot;
}
void Update(int p){a[p].size=a[a[p].l].size+a[a[p].r].size+1;}
int GetRankByVal(int p,int val)
{
if(!p) return p;
if(val==a[p].val) return a[a[p].l].size+1;
if(val<a[p].val) return GetRankByVal(a[p].l,val);
return GetRankByVal(a[p].r,val)+a[a[p].l].size+1;
}
int GetValByRank(int p,int rank)
{
if(!p) return 1e9;
if(a[a[p].l].size>=rank) return GetValByRank(a[p].l,rank);
if(a[a[p].l].size+1>=rank) return a[p].id;
return GetValByRank(a[p].r,rank-a[a[p].l].size-1);
}
void zig(int &p)
{
int q=a[p].l;
a[p].l=a[q].r,a[q].r=p,p=q;
Update(a[p].r),Update(p);
}
void zag(int &p)
{
int q=a[p].r;
a[p].r=a[q].l,a[q].l=p,p=q;
Update(a[p].l),Update(p);
}
void Insert(int &p,int val,int id)
{
if(!p){p=New(val,id);return;}
if(val<a[p].val)
{
Insert(a[p].l,val,id);
if(a[p].dat<a[a[p].l].dat) zig(p);
}
else
{
Insert(a[p].r,val,id);
if(a[p].dat<a[a[p].r].dat) zag(p);
}
Update(p);
}
int GetPre(int val)
{
int ans=0,p=root;
while(p)
{
if(val==a[p].val)
{
if(a[p].l>0)
{
p=a[p].l;
while(a[p].r>0) p=a[p].r;
ans=p;
}
break;
}
if(a[p].val<val&&a[p].val>a[ans].val) ans=p;
p=val<a[p].val?a[p].l:a[p].r;
}
return a[ans].id;
}
int GetNext(int val)
{
int ans=N-1,p=root;
while(p)
{
if(val==a[p].val)
{
if(a[p].r>0)
{
p=a[p].r;
while(a[p].l>0) p=a[p].l;
ans=p;
}
}
if(a[p].val>val&&a[p].val<a[ans].val) ans=p;
p=val<a[p].val?a[p].l:a[p].r;
}
return a[ans].id;
}
void Remove(int &p,int val)
{
if(!p) return;
if(val==a[p].val)
{
if(a[p].l||a[p].r)
{
if(!a[p].r||a[a[p].l].dat>a[a[p].r].dat) zig(p),Remove(a[p].r,val);
else zag(p),Remove(a[p].l,val);
Update(p);
}
else p=0;
return;
}
val<a[p].val?Remove(a[p].l,val):Remove(a[p].r,val);
Update(p);
}
int main()
{
scanf("%d%d",&n,&m); mx=m+n+2,mn=m+1; a[0].val=-1e9,a[N-1].val=1e9;
for(int i=1,x;i<=n;i++) scanf("%d",&x),v[x]=m+1+i,Insert(root,v[x],x),mp[x]=tot;
for(int i=1,s,t;i<=m;i++)
{
scanf("%s%d",ch,&s);
if(ch[0]=='T') Remove(root,v[s]),v[s]=mn--,Insert(root,v[s],s),mp[s]=tot;
else if(ch[0]=='B') Remove(root,v[s]),v[s]=mx++,Insert(root,v[s],s),mp[s]=tot;
else if(ch[0]=='I')
{
scanf("%d",&t); int ss;
if(t==1)
{
if(v[s]!=mx-1)
{
ss=GetNext(v[s]);
swap(a[mp[s]].id,a[mp[ss]].id);
swap(mp[s],mp[ss]); swap(v[s],v[ss]);
}
}
else if(t==-1)
{
if(v[s]!=mn+1)
{
ss=GetPre(v[s]);
swap(a[mp[s]].id,a[mp[ss]].id);
swap(mp[s],mp[ss]); swap(v[s],v[ss]);
}
}
}
else if(ch[0]=='A') printf("%d\n",GetRankByVal(root,v[s])-1);
else printf("%d\n",GetValByRank(root,s));
}
return 0;
}
好题,独立想出来之后还是很有成就感的。
考虑使用哈希来判断两个序列是否完全相同。预处理出排列 \(a\) 的哈希值,并以此线性推出 \(x \in [0,m-n]\) 的哈希值(相信大家都会)。
开一个长度为 \(m\) 的数组 \(pos\),\(pos_i\) 表示数字 \(i\) 在排列 \(b\) 中的位置。
我们按照 \(pos\) 建立一棵平衡树,并在每个节点上记录该子树的哈希值。
先将 \(i\in [1,n] \ pos_i\) 插入到平衡树中。接下来从小到大枚举 \(x\),判断整棵平衡树的哈希值是否和 \(a_1+x,a_2+x...a_n+x\) 的哈希值相等,并更新答案,最后从平衡树中删除 \(pos_{x+1}\)。
时间复杂度 \(\mathcal{O}(m \log n)\)。
AC Code
#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5;
int tot,root,n,m,ans,f[N];
typedef unsigned long long ll;
ll h,t,hah[N];
struct Treap
{
int l,r,val,dat,cnt,size;
ll sum,h;
}a[N];
int New(int val,ll sum)
{
a[++tot].val=val; a[tot].dat=rand(); a[tot].cnt=a[tot].size=1; a[tot].sum=a[tot].h=sum;
return tot;
}
void Update(int p)
{
a[p].size=a[a[p].l].size+a[a[p].r].size+a[p].cnt;
a[p].h=(a[a[p].l].h*131+a[p].sum)*hah[a[a[p].r].size]+a[a[p].r].h;
}
void zig(int &p)
{
int q=a[p].l;
a[p].l=a[q].r,a[q].r=p,p=q;
Update(a[p].r),Update(p);
}
void zag(int &p)
{
int q=a[p].r;
a[p].r=a[q].l,a[q].l=p,p=q;
Update(a[p].l),Update(p);
}
void Insert(int &p,int val,ll sum)
{
if(!p){p=New(val,sum);return;}
if(val<a[p].val)
{
Insert(a[p].l,val,sum);
if(a[p].dat<a[a[p].l].dat) zig(p);
}
else
{
Insert(a[p].r,val,sum);
if(a[p].dat<a[a[p].r].dat) zag(p);
}
Update(p);
}
void Remove(int &p,int val)
{
if(!p) return;
if(val==a[p].val)
{
if(a[p].cnt>1)
{
a[p].cnt--; Update(p);
return;
}
if(a[p].l||a[p].r)
{
if(!a[p].r||a[a[p].l].dat>a[a[p].r].dat) zig(p),Remove(a[p].r,val);
else zag(p),Remove(a[p].l,val);
Update(p);
}
else p=0;
return;
}
val<a[p].val?Remove(a[p].l,val):Remove(a[p].r,val);
Update(p);
}
int main()
{
scanf("%d%d",&n,&m);
hah[0]=t=1llu;
for(int i=1;i<n;i++) hah[i]=hah[i-1]*131,t+=hah[i];
for(ll i=1,x;i<=n;i++) scanf("%llu",&x),h=h*131+x;
for(int i=1,x;i<=m;i++) scanf("%d",&x),f[x]=i;
for(ll i=1;i<=n-1;i++) Insert(root,f[i],i);
for(ll i=n;i<=m;i++)
{
Insert(root,f[i],i);
if(h==a[root].h) ans++;
h+=t;
Remove(root,f[i-n+1]);
}
printf("%d",ans);
return 0;
}