链接:https://www.nowcoder.com/acm/contest/141/C
来源:牛客网
题意:
一副有N(N<=1e5)张卡片的牌,初始从顶部到底部的牌号分别是1到N,给出M(M<=1e5)次洗牌操作,每次洗牌操作给出两个整数p和s,表示从顶部往下数第p个位置开始的s张牌从牌堆中取出,放到顶部。
样例输入:
5 1
2 3
样例输出:
2 3 4 1 5
样例输入:
5 2
2 3
2 3
样例输出:
3 4 1 2 5
题目分析:
好吧,赛后师兄们说就是一个平衡树的模板题,但是强大的chen_jr_他们队能用一个强大的stl代替实现,虽然时间多了点但是这题并不卡这点时间,这个STL就是rope啦,先学一下rope的基本用法,但是treap还是需要学的!!
rope模板:
#include <ext/rope>
using namespace __gnu_cxx;
//以上两个东西是使用rope需要用的
//1.声明
rope<int>t;
//2.初始化
t.clear();
//3.操作
t.push_back(x);//在末尾添加x(元素)
t.insert(pos, x);//在pos位置(pos=0...t.size()-1)插入x(元素/rope)
t.copy(pos, x, to);//把rope从pos开始的x个元素,覆盖到指针node* to中
t.replace(pos, x, y);//从pos开始的x个换成y(元素/rope)
t.replace(pos, x);//第pos个换成x(元素/rope)
t.erase(pos, x);//从pos开始删除x个
t.substr(pos, x);//提取pos开始x个
t.at(pos) / t[pos];//访问pos位置元素
AC代码——rope:
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
rope<int>s,tmp;
int n,m,l,r;
int main() {
while (~scanf("%d %d",&n,&m)){
s.clear();
for(int i=1;i<=n;i++)s.push_back(i);
for(int i=1;i<=m;i++){
scanf("%d %d",&l,&r);
tmp=s.substr(l-1,r);
s.erase(l-1,r);
s=tmp+s;
}
for(int i=0;i<n;i++)printf("%d%c",s[i],i==n-1?'\n':' ');
}
return 0;
}
PS:各种树,太强辣!!一定要学
AC代码——treap:
#include <bits/stdc++.h>
using namespace std;
const int T = 1e6 + 6; // 节点数量, 同时也是所维护的序列的长度
int lch[T], rch[T], siz[T], fix[T], n; int key[T];
// lch[t]: left child, t 的左孩子
// rch[t]: right child, t 的右孩子
// siz: t 子树的规模
// fix: t 节点的随机权值,越大的位置越高
// key: t 节点的关键字值
int tot, root;
// tot: 已使用节点数量,包括空节点 (0 号元素) 在内
// root: 如等于 0,表示空树,否则表示一棵 treap 的根节点
inline void reset() { root = 0; tot = 1; }
// 新建一个节点,生成随机权值,保证高度为期望 O(log T), 并令它的关键字为 x, 返回节点的编号
inline int newnode(int x){
int t = tot; ++tot; /* assert(tot<=T); */
lch[t] = rch[t] = 0, siz[t] = 1; // siz 域是要把自己也算进去的
fix[t] = rand() << 15 | rand(); // 普通的 rand() 只能产生 (1<<15) 以内的整数,需要做两次之后拼接起来
key[t] = x;
return t;
}
// 如果你需要懒标记,可以把维护过程补在这里,比如说区间翻转,区间求最值等操作(本程序没有体现,故为空白,连形参都省略)
inline void pushdown(int){}
// 如无上述特殊询问,update 负责维护 siz 域
inline void update(int t){
siz[t] = siz[lch[t]] + 1 + siz[rch[t]];
}
// 把 t 以第 k 名为界分割成 l 和 r 两半,k 按人类的思维从 1 开始编号,其中第 k 名将落入 r 中。
void split(int t, int k, int &l, int &r){
if (!t){ l = r = 0; /*assert(k==1);*/ return; }
pushdown(t);
int tk = siz[lch[t]] + 1;
if (k <= tk){
split(lch[t], k, l, lch[t]); r = t;
}
else{
split(rch[t], k - tk, rch[t], r); l = t;
}
update(t);
}
// 将 l 和 r 合并成 t,如合并后以中序遍历查看 t 的内容,则有 l 的内容排在 r 的前面。
void join(int &t, int l, int r){
if (!l){ t = r; return; }
if (!r){ t = l; return; }
if (fix[l]>fix[r]){
pushdown(t = l); join(rch[l], rch[l], r);
}
else{
pushdown(t = r); join(lch[r], l, lch[r]);
}
update(t);
}
// 在 t 中寻找第 k 名元素,并返回其编号(注意是编号,不是其关键字)(这在设计上和 STL set 的 find 函数如出一辙)
int find(int t, int k){
pushdown(t);
int tk = siz[lch[t]] + 1;
if (k == tk)return t;
if (k<tk) return find(lch[t], k);
return find(rch[t], k - tk);
}
// 查询 t 中第一个大于等于 x 的关键字(这在设计上和 STL set 的 lower_bound 函数也是如出一辙)
int order(int t, int x){
if (!t)return 1;
pushdown(t);
if (x <= key[t])return order(lch[t], x);
return siz[lch[t]] + 1 + order(rch[t], x);
}
inline void insert(int x){
int l, r, k = order(root, x), t = newnode(x);
split(root, k, l, r);
join(r, t, r);
join(root, l, r);
}
void work(int p, int len){
int l, r, m;
split(root, p, l, r);
split(r, len + 1, m, r);
join(root, l, r);
join(root, m, root);
}
vector<int> ans;
void display(int u){
if (u == 0)return;
display(lch[u]);
ans.push_back(u);
display(rch[u]);
}
int main(){
int n, m;
while (scanf("%d %d", &n, &m) != -1){//n张牌1到n,m次洗牌
reset();//初始化,空树,只有空节点root
for (int i = 1; i <= n; i++){
insert(i); //插入值
}
for (int i = 1; i <= m; i++){
int p, s, L, MID, R;
scanf("%d %d", &p, &s);
split(root, p, L, MID);//把root树拆成L(1到p-1)和MID(p到n)
split(MID, s + 1, MID, R);//把MID树拆成MID(p到p+s)和R(p+s+1到n)
join(L, MID, L);//将MID和L合并成L,MID作为左边,L作为右边
join(root, L, R);//将L和R合并成root,MID作为左边,L作为右边
}
ans.clear();
display(root);//遍历答案
for (int i = 0; i<ans.size(); i++){
printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' ');
}
}
}