牛客网暑期ACM多校训练营(第三场)C.Shuffle Cards 平衡二叉树(无旋treap)


链接:https://www.nowcoder.com/acm/contest/141/C
来源:牛客网
 

Shuffle Cards

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

Eddy likes to play cards game since there are always lots of randomness in the game. For most of the cards game, the very first step in the game is shuffling the cards. And, mostly the randomness in the game is from this step. However, Eddy doubts that if the shuffling is not done well, the order of the cards is predictable!

To prove that, Eddy wants to shuffle cards and tries to predict the final order of the cards. Actually, Eddy knows only one way to shuffle cards that is taking some middle consecutive cards and put them on the top of rest. When shuffling cards, Eddy just keeps repeating this procedure. After several rounds, Eddy has lost the track of the order of cards and believes that the assumption he made is wrong. As Eddy's friend, you are watching him doing such foolish thing and easily memorizes all the moves he done. Now, you are going to tell Eddy the final order of cards as a magic to surprise him.

Eddy has showed you at first that the cards are number from 1 to N from top to bottom.

For example, there are 5 cards and Eddy has done 1 shuffling. He takes out 2-nd card from top to 4-th card from top(indexed from 1) and put them on the top of rest cards. Then, the final order of cards from top will be [2,3,4,1,5].

输入描述:

The first line contains two space-separated integer N, M indicating the number of cards and the number of shuffling Eddy has done.
Each of following M lines contains two space-separated integer pi, si indicating that Eddy takes pi-th card from top to (pi+si-1)-th card from top(indexed from 1) and put them on the top of rest cards.


1 ≤ N, M ≤ 105
1 ≤ pi ≤ N
1 ≤ si ≤ N-pi+1

输出描述:

Output one line contains N space-separated integers indicating the final order of the cards from top to bottom.

 

输入

5 1
2 3

输出

2 3 4 1 5

示例2

输入

5 2
2 3
2 3

输出

3 4 1 2 5

示例3

输入

5 3
2 3
1 4
2 4

输出

3 4 1 5 2

 题目大意:

给出n张卡片,编号从1~n。

现在有q次操作,给出一个p,s,使区间[p,p+s-1]的数整体移到最前面。

问q次操作之后卡片的状况

题目分析:

利用平衡二叉树实现区间操作,这里笔者用的是无旋treap。

具体操作时:

移动区间可以利用无旋treap的split操作使按区间分割成三颗子树,然后按照题目意思合并。

回答询问时只需按照是第K个元素把树分割成 K-1个元素的树和n-K+1个元素的树,显然询问的树就是n-K+1个元素的树再分割一个元素的树根。

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<cmath>
#include<stack>
#include<iomanip>
#include<functional>
#include<iomanip>
#include<bitset>
#define lson l,m
#define rson m+1,r
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int maxn = 2 * int(1e5) + 100;
const int BN = 30;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = (int)1e9 + 7;
const double eps = 1e-12;
unsigned int SEEDS = 19260817;
struct Treap {
#define ls t[x].ch[0]
#define rs t[x].ch[1]
	struct treap {
		int ch[2], size, val, rd;
		//ch[0]表示左儿子,ch[1]表示右儿子
		//记录同样权值的元素个数
		//记录节点子数大小(包括该节点)
		//记录节点权值
		//rd表示节点的随机值
	}t[maxn];
	int root, tot;
	int flip[maxn];//翻转用记录函数
	inline int Rand() {
		SEEDS = SEEDS * 1103515245 + 12345;
		return SEEDS / 65536;
	}
	//初始化
	void init() {
		root = 0;
		t[root].ch[0] = t[root].ch[1] = 0;
		t[root].val = t[root].rd = t[root].size = 0;
		tot = 0;
		srand(time(NULL));
	}
	//建立新节点
	int newnode(int val) {
		t[++tot].val = val;
		t[tot].ch[0] = t[tot].ch[1] = 0;
		t[tot].rd = Rand(), t[tot].size = 1;
		return tot;
	}
	//翻转一个区间的两个孩子
	void rev(int x) {
		swap(ls, rs);
		flip[x] ^= 1;
	}
	//统计子树大小
	void pushup(int x) {
		t[x].size = t[ls].size + t[rs].size + 1;
	}
	//Pushdown来下传标记(用区间反转标记和区间赋值标记来举例)
	void pushdown(int x) {
		if (!x) return;
		if (flip[x]) {
			if (ls) rev(ls);
			if (rs) rev(rs);
			flip[x] = 0;
		}
	}
	//分离k个节点
	pii split(int x, int k) {
		if (!x) return pii(0, 0);
		pii tmp;
		if (k <= t[ls].size) {
			tmp = split(ls, k);
			ls = tmp.second;
			pushup(x);
			tmp = pii(tmp.first, x);
		}
		else {
			tmp = split(rs, k - t[ls].size - 1);
			rs = tmp.first;
			pushup(x);;
			tmp = pii(x, tmp.second);
		}
		return tmp;
	}
	//合并两棵树
	int merge(int x, int y) {
		if (!x || !y) return x + y;
		if (t[x].rd<t[y].rd) {
			t[x].ch[1] = merge(t[x].ch[1], y);
			pushup(x);
			return x;
		}
		else {
			t[y].ch[0] = merge(x, t[y].ch[0]);
			pushup(y);
			return y;
		}
	}
	// 查询val数的排名辅助函数
	int ranks(int x, int val) {
		if (!x) return 0;
		if (t[x].val >= val) return ranks(ls, val);
		else return ranks(rs, val) + t[ls].size + 1;
	}
	//查询排名为val辅助函数
	int finds(int x, int val) {
		if (!x) return 0;
		if (val == t[ls].size + 1) return x;
		if (val <= t[ls].size) return finds(ls, val);
		else return finds(rs, val - t[ls].size - 1);
	}
	//插入val数
	void insert(int val) {
		int v = ranks(root, val);
		pii x = split(root, v);
		root = merge(merge(x.first, newnode(val)), x.second);
	}
	//删除x数
	void deletes(int val) {
		int v = ranks(root, val);
		pii x = split(root, v),
			y = split(x.second, 1);
		root = merge(x.first, y.second);
	}
	//查询排名为x的数
	int kth(int x) {
		return t[finds(root, x)].val;
	}
	//求x的前驱(前驱定义为小于x,且最大的数)
	int pre(int val) {
		return t[finds(root, ranks(root, val))].val;
	}
	//求x的后继(后继定义为大于x,且最小的数)
	int nex(int val) {
		return t[finds(root, ranks(root, val + 1) + 1)].val;
	}
	//查询x数的排名
	int Rank(int val) {
		return ranks(root, val) + 1;
	}
	//分段合并
	void solve(int x, int len) {
		pii tmp1 = split(root, x - 1), tmp2 = split(tmp1.second, len);
		root = merge(tmp2.first, merge(tmp1.first, tmp2.second));
	}
	//获得下标
	int getpos(int pos) {
		pii tmp1 = split(root, pos - 1), tmp2 = split(tmp1.second, 1);
		int ans = tmp2.first;
		merge(tmp1.first, merge(tmp2.first, tmp2.second));
		return ans;
	}
}tr;
int main() {
	//freopen("test.in","r",stdin);
	//ios::sync_with_stdio(false);
	//cin.tie(0);
	//freopen("D:\\cpp\\comper\\3224\\8.in", "r", stdin);
	//freopen("D:\\cpp\\\comper\\8.in", "w", stdout);
	int n, q;
	while (scanf("%d%d", &n, &q)) {
		tr.init();
		for (int i = 1; i <= n; i++)
			tr.insert(i);
		int p, s;
		while (q--) {
			scanf("%d%d", &p, &s);
			tr.solve(p, s);
		}
		for (int i = 1; i <= n; i++) {
			int pos = tr.getpos(i);
			printf("%d", tr.t[pos].val);
			printf("%s", i < n ? " " : "\n");
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值