从 -INF 开始的平衡树
前序
此文主要讲的是
你以为我是来深度解读
证明先咕着。
#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;
}
这是一道平衡树入门题,
首先把题目转化成形式化题意。
假设有一个序列,该序列初始只有
操作 D x
,等价于在序列中插入一个值为
操作 R
,等价于删去一个最新插入的数。
操作 Q x
,相当于求
以上
事实上这道题比模板题更好写,因为不用查询排名之类的东西。
#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;
}
这题目名挺奇怪的
这题没有上面一题那么裸,但还是很水(对于我这种蒟蒻而言)。
注意到题目中一个很重要的性质:每个区间
这样一来,如果我们把询问离线并按 左/右 端点排序,那么每只狗至多被 加入/删除 当前的询问队列
如果我们使用平衡树来维护,每只狗对时间复杂度的贡献只有
#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;
}
不管怎么说这题是一道紫题...所以说还是有一定的实现难度。
操作一 先对每一本书从上到下赋一个权值,并在权值前留
操作二 和操作一一样的做法,
操作三 直接分类讨论。对于
操作四 根据
操作五 最平凡的操作。根据排名查询即可。
#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;
}
好题,独立想出来之后还是很有成就感的。
考虑使用哈希来判断两个序列是否完全相同。预处理出排列
开一个长度为
我们按照
先将
时间复杂度
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】