3223: Tyvj 1729 文艺平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 6708 Solved: 4084
[Submit][Status][Discuss]
Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
Input
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
Output
输出一行n个数字,表示原始序列经过m次变换后的结果
Sample Input
5 3
1 3
1 3
1 4
Sample Output
4 3 2 1 5
HINT
N,M<=100000
用平衡树轻松搞定,加个lazy标记就行,代码有详细注释
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n;
const int inf = 0x7fffffff;
struct tree
{
int father,lch,rch,val,size; //父节点、左儿子、右儿子、值、子树大小
}stree[100005];
int lazy[100005]; // lazy标记
int root; // 根节点,树的大小
int a[100005];
inline bool get(int x) // 判断是父亲的左儿子还是右儿子
{
return stree[ stree[x].father ].rch == x;
}
inline void update(int x) // 更新子树大小
{
stree[x].size = stree[ stree[x].lch ].size + stree[ stree[x].rch ].size + 1;
}
void pushdown(int x)
{
if (x && lazy[x]) // 如果当前节点被反转过
{
lazy[x] = 0; //反转当前节点
lazy[ stree[x].lch ] ^= 1;
lazy[ stree[x].rch ] ^= 1; //标记左右儿子节点
swap(stree[x].lch, stree[x].rch); // 左右儿子对换
}
}
int build(int l, int r, int rt)
{
if (l > r)
return 0;
int mid = (l + r) / 2;
int lch = build(l, mid - 1, mid); //创建左子树
int rch = build(mid + 1, r, mid); //创建右子树
stree[mid].father = rt;
stree[mid].val = a[mid];
stree[mid].size = 1;
stree[mid].lch = lch;
stree[mid].rch = rch; // 创建该节点
update(mid); // 更新mid节点
return mid;
}
inline void rotate(int x)//旋转
{
pushdown( stree[x].father ); // 更新x的父节点的lazy标记
pushdown(x); // 更新x的lazy标记
int fa,oldf,whichx;
fa = stree[x].father; // x的父节点
oldf = stree[fa].father; // x父节点的父节点
whichx = get(x); // 判断x是父节点左儿子还是右儿子
if (whichx) //x是父节点的右儿子
{
stree[fa].rch = stree[x].lch;
stree[ stree[fa].rch ].father = fa; // 将x的左儿子变为x父节点的右儿子
stree[x].lch = fa;
stree[fa].father = x;
stree[x].father = oldf; // 将x父节点变为x左儿子
}
else // 同上
{
stree[fa].lch = stree[x].rch;
stree[ stree[fa].lch ].father = fa;
stree[x].rch = fa;
stree[fa].father = x;
stree[x].father = oldf;
}
if (oldf) // 如果有x的父节点有父节点,则更新祖父节点的(左/右)儿子为x
{
if (stree[oldf].rch == fa)
stree[oldf].rch = x;
else
stree[oldf].lch = x;
}
update(fa);
update(x); // 更新x与x父节点的大小
}
inline void splay(int x, int t)//将x伸展到t的子节点
{
int fa = stree[x].father; //将x伸展至父节点
while (fa != t)
{
if (stree[fa].father != t) // 如果父节点不是目的节点
{
if (get(x) == get(fa)) // 一字型(需旋转父节点再旋转x)
rotate(fa);
else
rotate(x); // 之字形(需旋转x两次)
}
rotate(x); // 旋转x
fa = stree[x].father; //更新父节点
}
if (!t)
root = x; // 更新根节点
}
int find(int x) //查找x为第几小的值
{
int u = root;
while (1)
{
pushdown(u);
if (x <= stree[stree[u].lch].size) // 如果x在当前节点本身或者当前节点的左子树内
u = stree[u].lch;
else // 如果x在当前节点的右子树内
{
x -= stree[stree[u].lch].size;
if (x == 1) // 恰好是当前节点
return u;
x--; // 减去当前节点的出现次数
u = stree[u].rch; // 更新当前节点
}
}
}
void init()
{
a[1] = a[n + 2] = inf; // 两端的标记
root = build(1, n + 2, 0); //建树
}
int writee(int x)
{
pushdown(x); //判断当前节点是否有lazy标记
if (stree[x].lch)
writee(stree[x].lch);
if (stree[x].val != inf && stree[x].val != -inf)
printf("%d ", stree[x].val);
if (stree[x].rch)
writee(stree[x].rch); // 中序遍历伸展树
}
int main()
{
// freopen("in.txt", "r", stdin);
int m,l,r;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
a[i + 1] = i;
init();
while (m--)
{
scanf("%d%d", &l, &r);
if (l >= r)
continue;
int x,y;
x = find(l);
y = find(r + 2);
splay(x, 0); // 将x伸展到根节点
splay(y, x); // 将y伸展到x的儿子节点,因为r>l,故y是x的右儿子,此时,y的左儿子即是[l,r]区间
lazy[ stree[ stree[root].rch ].lch ] ^= 1; //给该区间打上lazy标记
}
writee(root);
return 0;
}