萌新求调Treap28分
  • 板块P1801 黑匣子
  • 楼主_stOrz_
  • 当前回复1
  • 已保存回复1
  • 发布时间2021/10/14 19:51
  • 上次更新2023/11/4 03:49:23
查看原帖
萌新求调Treap28分
236416
_stOrz_楼主2021/10/14 19:51
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[2000005], tot, vis[1000005], root;
struct node{
	int val, ls, rs, pri, siz, num;
}tr[1000005];
int newnode(int val){
	tr[++tot].ls = tr[tot].rs = 0;
	tr[tot].pri = rand();
	tr[tot].val = val;
	tr[tot].num = tr[tot].siz =  1;
	return tot;
}
void update(int p){
	tr[p].siz = tr[tr[p].ls].siz + tr[tr[p].rs].siz + tr[p].num;
}
void zig(int &p){
	int q = tr[p].ls;
	tr[p].ls = tr[q].rs;
	tr[q].rs = p;
	tr[q].siz = tr[p].siz;
	update(p);
	p = q;
}
void zag(int &p){
	int q = tr[p].rs;
	tr[p].rs = tr[q].ls;
	tr[q].ls = p;
	tr[q].siz = tr[p].siz;
	update(p);
	p = q;
}
void Insert(int &p, int val){
	if(!p){
		p = newnode(val);
		return;
	} 
	tr[p].siz++;
	if(val == tr[p].val){
		tr[p].num++;
		return;
	}
	if(val < tr[p].val){
		Insert(tr[p].ls, val);
		if(tr[p].pri < tr[tr[p].ls].pri) zig(p);
	}
	else{
		Insert(tr[p].rs, val);
		if(tr[p].pri < tr[tr[p].rs].pri) zag(p);
	}
}
int GetValByRank(int p, int rank){
	if(!p) return 0;
//	cout << "p" << p << "\n";
	if(tr[tr[p].ls].siz >= rank) return GetValByRank(tr[p].ls, rank);
	else if(tr[tr[p].ls].siz + tr[p].num < rank) return GetValByRank(tr[p].rs, rank - tr[tr[p].ls].siz - tr[p].num);
	else return tr[p].val;
}
signed main(){
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	int x = 1, t = 1;	
	for(int i = 1; i <= m; i++){
		cin >> vis[i];
	}
	while(t <= m){
		while(x <= vis[t]){
			Insert(root, a[x]);
			x++;
		}
		cout << GetValByRank(root, t++) << "\n";
	}
}

调了好久嘤嘤嘤

2021/10/14 19:51
加载中...