跳到正文

个人记录

713-FHQ_TREAP解题报告

_KRST_约 26 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月30日
篇幅
约 26 分钟

版本

  1. v1当前

A - 普通平衡树

思路:板子,普通平衡树即可

代码:

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const double alpha=0.75;
struct Node{
	int ls,rs;
	int val;
	int tot;
	int sz;
	int del;
}t[N];
int order[N],cnt;
int tree_stack[N],top=0;
int root=0;
void inorder(int u){
	if(!u)return;
	inorder(t[u].ls);
	if(t[u].del)order[++cnt]=u;
	else tree_stack[++top]=u;
	inorder(t[u].rs);
}
void InitNode(int u){
	t[u].ls=t[u].rs=0;
	t[u].sz=t[u].tot=t[u].del=1;
}
void pushup(int u){
	t[u].sz=t[t[u].ls].sz+t[t[u].rs].sz+t[u].del;
	t[u].tot=t[t[u].ls].tot+t[t[u].rs].tot+1;
}
void build(int l,int r,int &u){
	int mid=(l+r)>>1;
	u=order[mid];
	if(l==r){InitNode(u);return;}
	if(l<mid)build(l,mid-1,t[u].ls);
	if(l==mid)t[u].ls=0;
	build(mid+1,r,t[u].rs);
	pushup(u);
}
void rebuild(int &u){
	cnt=0;
	inorder(u);
	if(cnt)build(1,cnt,u);
	else u=0;
}
bool notbalance(int u){
	if((double)t[u].sz*alpha<=(double)max(t[t[u].ls].sz,t[t[u].rs].sz))
	return true;
	return false;
}
void Insert(int &u,int x){
	if(!u){
		u=tree_stack[top--];
		t[u].val=x;
		InitNode(u);
		return;
	}
	t[u].sz++;
	t[u].tot++;
	if(t[u].val>=x)Insert(t[u].ls,x);
	else Insert(t[u].rs,x);
	if(notbalance(u))rebuild(u);
}
int ranks(int u,int x){
	if(!u)return 0;
	if(x>t[u].val)return t[t[u].ls].sz+t[u].del+ranks(t[u].rs,x);
	return ranks(t[u].ls,x);
}
int kth(int k){
	int u=root;
	while(u){
		if(t[u].del&&t[t[u].ls].sz+1==k)return t[u].val;
		else if(t[t[u].ls].sz>=k)u=t[u].ls;
		else{
			k=k-t[t[u].ls].sz-t[u].del;
			u=t[u].rs;
		}
	}
	return t[u].val;
}
void Del_k(int &u,int k){
	t[u].sz--;
	if(t[u].del&&t[t[u].ls].sz+1==k){
		t[u].del=0;
		return;
	}
	if(t[t[u].ls].sz+t[u].del>=k)Del_k(t[u].ls,k);
	else Del_k(t[u].rs,k-t[t[u].ls].sz-t[u].del);
}
void Del(int x){
	Del_k(root,ranks(root,x)+1);
	if(t[root].tot*alpha>t[root].sz)rebuild(root);
}
int main(){
	for(int i=N-1;i>=1;i--) tree_stack[++top]=i;
	int q;cin>>q;
	while(q--){
		int opt,x;
		cin>>opt>>x;
		if(opt==1) Insert(root,x);
		if(opt==2) Del(x);
		if(opt==3) printf("%d\n",ranks(root,x)+1);
		if(opt==4) printf("%d\n",kth(x));
		if(opt==5){
			printf("%d\n",kth(ranks(root,x)));
		}
		if(opt==6){
			printf("%d\n",kth(ranks(root,x+1)+1));
		}
	}
	return 0;
}

B - 甲苯先生的滚榜

思路:用了fhq treap,每次读入后判断这个人有没有答对过题,没有的话直接存入罚时和题数,有的话删除原点(如果一开始存入m个罚时题数都为0的容易RE),然后求出此节点的排名。

代码

C++
#include<bits/stdc++.h>
typedef unsigned int ui;
struct sr{
	int f,s;
};
struct Node{
    int l,r;
    sr val;
	int key;
    int size;
}fhq[1000005];
int cnt,root,yz;
std::mt19937 rnd(233);
inline int newnode(sr val){
    fhq[++cnt].val=val;
    fhq[cnt].key=rnd();
    fhq[cnt].size=1;
    return cnt;
}
void ldr(int now)
{
    if(!now) return;
//    if(fhq[now].reverse) pushdown(now);
    ldr(fhq[now].l);
    printf("%d ", fhq[now].val.s);
    ldr(fhq[now].r);
}
inline void update(int now){
    fhq[now].size=fhq[fhq[now].l].size+fhq[fhq[now].r].size+1;
}
void split(int now,sr val,int &x,int &y){
    if(!now) x=y=0;
    else{
//    	printf("%d %d\n",fhq[now].val.f,fhq[now].val.s);
        if(fhq[now].val.f<val.f||(fhq[now].val.f==val.f&&fhq[now].val.s>=val.s)){
            x=now;
            split(fhq[now].r,val,fhq[now].r,y);
        }
        else{
            y=now;
            split(fhq[now].l,val,x,fhq[now].l);
        }
        update(now);
    }
}
void ssplit(int now,sr val,int &x,int &y){
//	printf(")");
    if(!now) x=y=0;
    else{
//    	printf("awa");
        if(fhq[now].val.f<val.f||(fhq[now].val.f==val.f&&fhq[now].val.s>val.s)){
            x=now;
//            printf("~%d\n\n\n",fhq[now].r);
            ssplit(fhq[now].r,val,fhq[now].r,y);
        }
        else{
//        	printf("///");
            y=now;
            ssplit(fhq[now].l,val,x,fhq[now].l);
        }
        update(now);
    }
}
int merge(int x,int y){
    if(!x||!y) return x+y;
    if(fhq[x].key>fhq[y].key){           // > >= < <=
        fhq[x].r=merge(fhq[x].r,y);
        update(x);
        return x;
    }
    else{
        fhq[y].l=merge(x,fhq[y].l);
        update(y);
        return y;
    }
}
int x,y,z;
inline void ins(sr val){
    split(root,val,x,y);
    root=merge(merge(x,newnode(val)),y);
}
inline void del(sr val){
    split(root,val,x,z);
    ssplit(x,val,x,y);
    y=merge(fhq[y].l,fhq[y].r);
    root=merge(merge(x,y),z);
}
ui m,seed,last=7;
inline void getrank(sr val){
    split(root,val,x,y);
//    ldr(x);
//    printf("\n");
    printf("%d\n",yz-fhq[x].size);
    last=yz-fhq[x].size;
    root=merge(x,y);
}
ui randNum(ui& seed,ui last,const ui m){
    seed=seed*17+last;
	return seed%m+1;
}
int t,n,a,b;
sr c[100005];
signed main(){
	scanf("%d",&t);
	for(int i=1;i<=t;i++){
		scanf("%u%d%u",&m,&n,&seed);
		for(int j=1;j<=cnt;j++) fhq[j].size=fhq[j].l=fhq[j].r=fhq[j].val.f=fhq[j].val.s=0;
		for(int j=1;j<=100000;j++) c[j].f=c[j].s=0;
		root=cnt=yz=0;
		for(int j=1;j<=n;j++){
			a=randNum(seed,last,m);
			b=randNum(seed,last,m);
//			printf("	%d %d\n",a,b);
			if(c[a].f) del(c[a]);
			else yz++;
			c[a].f++;
			c[a].s+=b;
			ins(c[a]);
			getrank(c[a]);
		}
	}
    return 0;
}

C - 书架

思路:选用按大小拆分的fhq_treap,前四个操作调用split把s与前后拆分出来,再用merge按需要顺序组合。第五个操作用split按s分成两块,求出答案后再用merge合并。

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5+5;
struct Node{
    int l,r;
    int val,key;
    int size;
    int fa;
};
Node fhq[maxn];
int cnt,root;
int id[maxn];
int x,y,z,q,w,ls,xy;
mt19937 rnd(233);
inline int newnode(int val){
    fhq[++cnt].val=val;
    fhq[cnt].key=rnd();
    fhq[cnt].size=1;
    id[val]=cnt;
    return cnt;
}
inline void update(int now){
    fhq[now].size=fhq[fhq[now].l].size+fhq[fhq[now].r].size+1;
}
void split(int now,int siz,int &x,int &y,int fax=0,int fay=0){
    if(!now) x=y=0;
    else{
        if(fhq[fhq[now].l].size<siz){
            fhq[now].fa=fax;
            x=now;
            split(fhq[now].r,siz-fhq[fhq[now].l].size-1,fhq[now].r,y,now,fay);
        }
        else{
            fhq[now].fa=fay;
            y=now;
            split(fhq[now].l,siz,x,fhq[now].l,fax,now);
        }
        update(now);
    }
}
int merge(int x,int y){
    if(!x||!y) return x+y;
    if(fhq[x].key>fhq[y].key){
        fhq[x].r=merge(fhq[x].r,y);
        fhq[fhq[x].r].fa=x;
        update(x);
        return x;
    }
    else{
        fhq[y].l=merge(x,fhq[y].l);
        fhq[fhq[y].l].fa=y;
        update(y);
        return y;
    }
}
inline int gp(int now){
    int res=fhq[fhq[now].l].size+1;
    while(fhq[now].fa){
        if(fhq[fhq[now].fa].r==now) res+=fhq[fhq[fhq[now].fa].l].size+1;
        now=fhq[now].fa;
    }
    return res;
}
int n,m;
int p[maxn];
char op[7];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&p[i]);
        split(root,i-1,x,y);
        root=merge(merge(x,newnode(p[i])),y);
    }
    while(m--){
        scanf("\n%s ",&op);
        int s,t;
        scanf("%lld",&s);
        int w=gp(id[s]);
        if(op[0]=='T'){
            split(root,w,x,z);
            split(x,w-1,x,y);
            root=merge(y,merge(x,z));
        }
        else if(op[0]=='B'){
            split(root,w,x,z,0);
            split(x,w-1,x,y,0);
            root=merge(x,merge(z,y));
        }
        else if(op[0]=='I'){
            scanf("%lld",&t);
            if(t){
                if(t>0){
                    split(root,w+1,z,q);
                    split(z,w,y,z);
                    split(y,w-1,x,y);
                    root=merge(x,merge(z,merge(y,q)));
                }
                else{
                    split(root,w,z,q);
                    split(z,w-1,y,z);
                    split(y,w-2,x,y);
                    root=merge(x,merge(z,merge(y,q)));
                }
            }
        }
        else if(op[0]=='A') printf("%lld\n",w-1);
        else{
            split(root,s,x,y);
            int ls=x;
            while(fhq[ls].r) ls=fhq[ls].r;
            printf("%lld\n",fhq[ls].val);
            root=merge(x,y);
        }
    }
    return 0;
}

D - 最长上升子序列

思路:这题没用treap因为真的想不到(,用了树状数组求最长子序列的办法

代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,t,x,ans[1000005],tr[1000005];
vector<int> a;
void awa(int x,int val){
	while(x<=n){
		tr[x]=max(tr[x],val);
		x+=x&-x;
	}
}
int sr(int x){
	t=0;
    while(x) t=max(tr[x],t),x-=x&-x;
    return t;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
		scanf("%d",&x);
		a.insert(x+a.begin(),i);
    }
    for(int i=0;i<n;i++){
    	x=a[i];
		ans[x]=sr(x)+1;
		awa(x,ans[x]);
    }
    for(int i=1;i<=n;i++){
		ans[i]=max(ans[i],ans[i-1]);
    	printf("%d\n",ans[i]);
    }
    return 0;
}

E - 文艺平衡树

思路:这题是板子,用按大小分裂的办法拆出要反转的区间,并给区间打上懒标记后合并,需要调用时再下传懒标记

代码:

C++
#include<bits/stdc++.h>
#include<random>
const int maxn=1e5+5;
struct Node{
    int l,r;
    int val,key;
    int size;
    bool reverse;
}fhq[maxn];
int cnt,root;
std::mt19937 rnd(233);
inline int newnode(int val){
    fhq[++cnt].val=val;
    fhq[cnt].key=rnd();
    fhq[cnt].size=1;
    return cnt;
}
inline void update(int now){
    fhq[now].size=fhq[fhq[now].l].size+fhq[fhq[now].r].size+1;
}
inline void pushdown(int now){
    std::swap(fhq[now].l,fhq[now].r);
    fhq[fhq[now].l].reverse^=1;
    fhq[fhq[now].r].reverse^=1;
    fhq[now].reverse=false;
}
void split(int now,int siz,int &x,int &y){
    if(!now) x=y=0;
    else{
        if(fhq[now].reverse) pushdown(now);
        if(fhq[fhq[now].l].size<siz){
            x=now;
            split(fhq[now].r,siz-fhq[fhq[now].l].size-1,fhq[now].r,y);
        }
        else{
            y=now;
            split(fhq[now].l,siz,x,fhq[now].l);
        }
        update(now);
    }
}
int merge(int x,int y){
    if(!x||!y) return x+y;
    if(fhq[x].key<fhq[y].key){
        if(fhq[x].reverse) pushdown(x);
        fhq[x].r=merge(fhq[x].r,y);
        update(x);
        return x;
    }
    else{
        if(fhq[y].reverse) pushdown(y);
        fhq[y].l=merge(x,fhq[y].l);
        update(y);
        return y;
    }
}
void reverse(int l,int r){
    int x,y,z;
    split(root,l-1,x,y);
    split(y,r-l+1,y,z);
    fhq[y].reverse^=1;
    root=merge(merge(x,y),z);
}
void ldr(int now){
    if(!now) return;
    if(fhq[now].reverse) pushdown(now);
    ldr(fhq[now].l);
    printf("%d ", fhq[now].val);
    ldr(fhq[now].r);
}
int n,m,l,r;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) root=merge(root,newnode(i));
    while(m--){
        scanf("%d%d",&l,&r);
        reverse(l,r);
    }
    ldr(root);
    return 0;
}

F - 郁闷的出纳员

思路:用的普通treap,工资低于最低工资标准的踢出平衡树。修改工资的操作用修改m的值来代替。用一个变量ta表示已经修改的工资的总量,每次加入员工时减去ta

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+10;
const double alpha=0.75;
struct Node{
	int ls,rs;
	int val;
	int tot;
	int sz;
	int del;
}t[N];
int order[N],cnt;
int tree_stack[N],top=0;
int root=0;
void inorder(int u){
	if(!u)return;
	inorder(t[u].ls);
	if(t[u].del)order[++cnt]=u;
	else tree_stack[++top]=u;
	inorder(t[u].rs);
}
void InitNode(int u){
	t[u].ls=t[u].rs=0;
	t[u].sz=t[u].tot=t[u].del=1;
}
void pushup(int u){
	t[u].sz=t[t[u].ls].sz+t[t[u].rs].sz+t[u].del;
	t[u].tot=t[t[u].ls].tot+t[t[u].rs].tot+1;
}
void build(int l,int r,int &u){
	int mid=(l+r)>>1;
	u=order[mid];
	if(l==r){InitNode(u);return;}
	if(l<mid)build(l,mid-1,t[u].ls);
	if(l==mid)t[u].ls=0;
	build(mid+1,r,t[u].rs);
	pushup(u);
}
void rebuild(int &u){
	cnt=0;
	inorder(u);
	if(cnt)build(1,cnt,u);
	else u=0;
}
bool notbalance(int u){
	if((double)t[u].sz*alpha<=(double)max(t[t[u].ls].sz,t[t[u].rs].sz))
	return true;
	return false;
}
void Insert(int &u,int x){
	if(!u){
		u=tree_stack[top--];
		t[u].val=x;
		InitNode(u);
		return;
	}
	t[u].sz++;
	t[u].tot++;
	if(t[u].val>=x)Insert(t[u].ls,x);
	else Insert(t[u].rs,x);
	if(notbalance(u))rebuild(u);
}
int ranks(int u,int x){
	if(!u)return 0;
	if(x>t[u].val)return t[t[u].ls].sz+t[u].del+ranks(t[u].rs,x);
	return ranks(t[u].ls,x);
}
int kth(int k){
	int u=root;
	while(u){
		if(t[u].del&&t[t[u].ls].sz+1==k)return t[u].val;
		else if(t[t[u].ls].sz>=k)u=t[u].ls;
		else{
			k=k-t[t[u].ls].sz-t[u].del;
			u=t[u].rs;
		}
	}
	return t[u].val;
}
void Del_k(int &u,int k){
	t[u].sz--;
	if(t[u].del&&t[t[u].ls].sz+1==k){
		t[u].del=0;
		return;
	}
	if(t[t[u].ls].sz+t[u].del>=k)Del_k(t[u].ls,k);
	else Del_k(t[u].rs,k-t[t[u].ls].sz-t[u].del);
}
void Del(int x){
	Del_k(root,ranks(root,x)+1);
	if(t[root].tot*alpha>t[root].sz)rebuild(root);
}
int n,m,x,lq,rq,ta,tb,ls,peo;
char opt;
signed main(){
	for(int i=N-1;i>=1;i--) tree_stack[++top]=i;
	cin>>n>>m;
	Insert(root,-114514114514);
	while(n--){
		cin>>opt>>x;
		if(opt=='I'){
			if(x>=ta+m){
	//			cout<<x<<" "<<ta<<"		awa\n";
				Insert(root,x-ta);
				peo++;
			}
		}
		if(opt=='A') m-=x,ta+=x;
		if(opt=='S'){
			m+=x,ta-=x;
			ls=kth(ranks(root,m));
			while(ls!=-114514114514){
				Del(ls);
				peo--;
				tb++;
				ls=kth(ranks(root,m));
			}
		}
		if(opt=='F'){
			if(peo<x) cout<<"-1\n";
			else cout<<kth(peo+2-x)+ta<<"\n";
		}
	}
	cout<<tb;
	return 0;
}
原文