题目大意:
就是每次把牌堆中若干个连续的牌放到堆顶,问你最后牌的序列。
解题思路:
Splay 区间翻转的模板题:
对于一个区间
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
]
[1,2,3,4,5,6,7,8]
[1,2,3,4,5,6,7,8]
你要把
[
3
,
4
,
5
]
[3,4,5]
[3,4,5]拿到前面去
- 我们可以这么搞先把
[
1
−
5
]
[1-5]
[1−5]翻转·一下
[ 5 , 4 , 3 , 2 , 1 , 6 , 7 , 8 ] [5,4,3,2,1,6,7,8] [5,4,3,2,1,6,7,8] - 然后我们发现只要把
[
5
,
4
,
3
]
[5,4,3]
[5,4,3]和
[
2
,
1
]
[2,1]
[2,1]再翻转一遍就好了
[ 3 , 4 , 5 , 1 , 2 , 6 , 7 , 8 ] [3,4,5,1,2,6,7,8] [3,4,5,1,2,6,7,8]
AC code
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x7fffffff
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 1e9 + 9;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x) {
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
read(first);
read(args...);
}
int n, m, arr[maxn];
struct node {
#define ls(x) T[x].ch[0]
#define rs(x) T[x].ch[1]
#define fa(x) T[x].fa
#define root T[0].ch[1]
/**
* 树形结构
*/
int fa;//父亲
int ch[2];// 存储左右儿子的位置
int sum;// 子树大小
int val;
/**
*
* lazy 标记两个
*
*/
int rac;
/**
* 权值 最大值
*/
}T[N];
void downdate(int x) {
if(T[x].rac) {
int &lc = T[x].ch[0], &rc = T[x].ch[1];
swap(lc,rc);
T[lc].rac ^= 1, T[rc].rac ^= 1;
T[x].rac = 0;
}
}
void update(int x) {
T[x].sum = T[ls(x)].sum + T[rs(x)].sum + 1;
}
int ident(int x) {return T[fa(x)].ch[0] == x ? 0 : 1;}
void connect(int x, int fa, int how) {// 把x变成fa的how儿子
T[fa].ch[how] = x;
T[x].fa = fa;
}
void rotate(int x) { // 把x转到fa位置
int Y = fa(x), R = fa(Y);
int YSON = ident(x), RSON = ident(Y);
downdate(Y);
downdate(x);
connect(T[x].ch[YSON^1],Y,YSON), connect(Y,x,YSON^1), connect(x,R,RSON);
update(Y);
update(x);
}
void splay(int x, int to) { // 把x转到to的位置
to = fa(to);
while(fa(x) != to) {
int y = fa(x);
if(T[y].fa == to) rotate(x);
else if(ident(x) == ident(y)) rotate(y), rotate(x);
else rotate(x), rotate(x);
}
}
//.....................................................................
int find(int x) {
int now = root;
while (now) {
downdate(now);
if (x <= T[T[now].ch[0]].sum) now = T[now].ch[0];
else {
x -= T[T[now].ch[0]].sum + 1;
if (!x) return now;
now = T[now].ch[1];
}
}
return 0;
}
inline int BuildTree(int l,int r) {
if(l > r) return 0;
connect(BuildTree(l,mid-1),mid,0);
connect(BuildTree(mid+1,r),mid,1);
T[mid].val = mid;
update(mid);
return mid;
}
void print(int now) {
if(!now) return;
downdate(now);
if(T[now].ch[0]) print(T[now].ch[0]);
if(now != 1 && now != n + 2) cout << now - 1 << " ";
// cout << now << " ";
if(T[now].ch[1]) print(T[now].ch[1]);
}
inline void work(int l, int r) {
int fl = find(l);
int fr = find(r+2);
splay(fl,root);
splay(fr,T[root].ch[1]);
T[T[fr].ch[0]].rac ^= 1;
}
int main() {
read(n,m);
root = BuildTree(1,n+2);
while(m --) {
int l, r;
cin >> l >> r;
work(1,l+r-1);
work(1,r);
work(r+1,r+l-1);
}
print(root);
return 0;
}
rope
Rope其主要是结合了链表和数组各自的优点,链表中的节点指向每个数据
块,即数组,并且记录数据的个数,然后分块查找和插入。在g++头文件中,< ext / rope >中有成型的块状链表,在using namespace
__gnu_cxx;空间中,其操作十分方便。
基本操作:
rope test;
test.push_back(x);//在末尾添加x
test.insert(pos,x);//在pos插入x
test.erase(pos,x);//从pos开始删除x个
test.copy(pos,len,x);//从pos开始到pos+len为止用x代替
test.replace(pos,x);//从pos开始换成x
test.substr(pos,x);//提取pos开始x个 x的默认值是1
test.at(x)/[x];//访问第x个元素
#include <bits/stdc++.h>
#include <ext/rope>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
using namespace __gnu_cxx;
const int N = 2e6 + 10, mod = 1e9 + 9;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x) {
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
read(first);
read(args...);
}
int main() {
IOS;
int n, m;
rope<int> q;
cin >> n >> m;
for(int i = 1; i <= n; ++ i) {
q.push_back(i);
}
for(int i = 1; i <= m; ++ i) {
int u, v;
cin >> u >> v;
q = q.substr(u-1,v) + q.substr(0,u-1) + q.substr(u+v-1,n-(u+v-1));
}
for(int i = 0; i < n; ++ i)
cout << q[i] << " ";
}