[置顶] hdu 1890 伸展树区间翻转

时间:2023-01-19 07:39:53

题意: 给你n个数,每次先输出第i大的数的位置(如果有多个,选下标小的那个),然后每次将第i个位置到第i大的数所在位置之间的数进行翻转。

思路:输入的数组可能有多个相同的值,我们可以进行两次排序把数组的值变为1---n(表示第几大)。

在建伸展树的时候我们可以顺便用pos[i]记录第i大的数的节点标号。

对于第i次操作,我们用col[]数组记录翻转标记,每次先把第i大的节点pos[i]旋转到根,那么它的位置为i+左儿子的个数。然后左儿子打上翻转标记,最后删除根。

注意:下放懒惰标记时只要交换左右儿子的节点标号就可以了,也正因为这个原因,

下放函数的位置记得要放在没有引用任何左右儿子信息之前, 这跟区间其它操作最大的区别。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define L ch[x][0]
#define R ch[x][1]
const int maxn = 100005;
int pos[maxn]; //pos[i]表示第i大的数的节点的标号
int n;
struct node {
int a, id;
bool operator <(const node &t) const {
return id < t.id;
}
}p[maxn];
bool cmp(const node &a, const node &b) {
return a.a < b.a || (a.a == b.a && a.id < b.id);
}
struct splayTree {
int sz[maxn], ch[maxn][2], pre[maxn];
bool col[maxn];
int root, tot;
void down(int x) {
if(col[x]) {
col[L] ^= 1;
col[R] ^= 1;
swap(L, R);
col[x] = 0;
}
}
void up(int x) {
sz[x] = sz[L] + sz[R] + 1;
}
void rotate(int &x, int f) {
int y = pre[x], z = pre[y];
down(y); down(x);
ch[y][!f] = ch[x][f];
pre[ch[x][f]] = y;
pre[x] = pre[y];
if(pre[x]) ch[z][ch[z][1] == y] = x;
ch[x][f] = y;
pre[y] = x;
up(y);
}
void splay(int &x, int g) {
while(pre[x] != g) {
int y = pre[x], z = pre[y];
down(z); down(y); down(x);
//不是区间翻转的题,这里的down可以不写,因为rotate里面有down, 但区间翻转要先down在去旋转,因为左右儿子会改变
if(z == g) rotate(x, ch[y][0] == x);
else {
int f = (ch[z][0] == y);
ch[y][!f] == x ? rotate(y, f) : rotate(x, !f);
rotate(x, f);
}
} up(x);
if(!g) root = x;
}
int find(int k) {
int x = root;
while(sz[L]+1 != k) {
down(x);
if(sz[L]>= k) x = L;
else {
k -= sz[L]+1;
x = R;
}
}
return x;
}
void rto(int k, int g) {
int x = root;
while(1) {
down(x);
if(sz[L]+1 == k) break;
if(sz[L]>= k) x = L;
else {
k -= sz[L]+1;
x = R;
}
}
splay(x, g);
}
void newNode(int &x, int m, int fa) {
x = ++tot;
pos[p[m].a] = x;
pre[x] = fa;
sz[x] = 1;
L = R = 0;
col[x] = 0;
}
void build(int &x, int l, int r, int fa) {
if(l > r) return;
int m = (l + r) >> 1;
newNode(x, m, fa);
build(L, l, m-1, x);
build(R, m+1, r, x);
up(x);
}
void init(int n) {
tot = 0;
int i;
//数字可能相等,可以把数字预处理成1--n
for(i = 1; i <= n; i++) {
scanf("%d", &p[i].a);
p[i].id = i;
}
sort(p+1, p+n+1, cmp);
for(i = 1; i <= n; i++)
p[i].a = i;
sort(p+1, p+n+1); build(root, 1, n, 0);
}
void print(int x) {
down(x);
printf("x: %d lson: %d rson: %d fa: %d lsz: %d rsz: %d\n", x, L, R, pre[x], sz[L], sz[R]);
if(L)print(L);
if(R)print(R);
}
void debug() {
printf("root = %d\n", root);
print(root);
}
void solve() {
for(int i = 1; i < n; i++) {
splay(pos[i], 0); //把值为i的节点旋到根
int x = root;
printf("%d ", sz[L]+i);
down(x); col[L] ^= 1; down(L); //根down,根的左儿子打翻转标记
if(sz[L]) { //有左儿子
rto(sz[L], root); //把左儿子的最右边的点旋到根
//删除根,根的左儿子代替根,新根的右儿子还是原根的右儿子,但父亲要修改
root = L;
ch[root][1] = R;
pre[root] = 0;
pre[R] = root;
}
else { //没有左儿子,直接把右儿子拉到根上来
root = ch[root][1];
pre[root] = 0;
}
up(root);
}
printf("%d\n", n);//最后只剩一个节点时一定是最后一个, 特判一下。
}
}spt;
int main() {
int i;
while( ~scanf("%d", &n) && n) {
spt.init(n);
spt.solve();
}
return 0;
}

[置顶] hdu 1890 伸展树区间翻转的更多相关文章

  1. hdu 5919 主席树&lpar;区间不同数的个数 &plus; 区间第k大&rpar;

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  2. &lbrack;置顶&rsqb; hdu 4699 2个栈维护 or 伸展树

    hdu 4699  Editor 题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求到k位置的最大的前缀和.. 注意这里的k是在光标之前的, ...

  3. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  4. hdu 1890 splay树

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  6. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  7. HDU 4348 主席树区间更新

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. hdu 1698 线段树 区间更新 区间求和

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu 3308&lpar;线段树区间合并&rpar;

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. Python 2&period;7下载地址

    网址:https://www.python.org/downloads/release/python-2711/ Python 2.7.11 Release Date: 2015-12-05 Pyth ...

  2. NGINX怎样处理惊群的

    写在前面 写NGINX系列的随笔,一来总结学到的东西,二来记录下疑惑的地方,在接下来的学习过程中去解决疑惑. 也希望同样对NGINX感兴趣的朋友能够解答我的疑惑,或者共同探讨研究. 整个NGINX系列 ...

  3. Random类&lpar;java&period;util&rpar;

    转自 Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要的随机数字. 相同种子数的Rand ...

  4. 【转】RDO、SAD、SATD、λ

    SAD(Sum of Absolute Difference)=SAE(Sum of Absolute Error)即绝对误差和 SATD(Sum of Absolute Transformed Di ...

  5. LAN路由

    一.实验的目的:   实现不同子网之前的信息交流      二.如果 1.虚拟子网 VMnet8:192.168.233.0/24 VMnet1:172.16.1.0/24 2.虚拟机vm1 ip:1 ...

  6. hdu&lowbar;5720&lowbar;Wool&lpar;脑洞&rpar;

    题目连接:hdu_5720_Wool 题意: 给你N个数,然后给你一个区间,问你在这个区间内有多少数可以不与这N个数任选两个出来组成三角形 题解: 这里我还是贴官方的题解算了 考虑三角形三条边a,b, ...

  7. Visual Studio 2017 社区版的安装与组件修改(C&plus;&plus;)

    0. 环境描述 需求:用VS2017做C++简易开发. 操作系统:Windows 8.1. 1. 下载 MSDN下载VS2017社区版. https://msdn.itellyou.cn/ 下载后: ...

  8. jquery 选择对象随心所欲,遍历数组更是易如反掌

    jquery只要研究总结透彻了,那选择对象就会随心所欲,遍历数组更是易如反掌.选对对象,才能“娶妻生子”,才能有后续的数据处理.呵呵遍历对很关键. 怕只怕,学东西浅尝辄止一知半解.本篇特别研究总结jq ...

  9. Scrapy可视化管理软件SpiderKeeper

    通常开发好的Scrapy爬虫部署到服务器上,要不使用nohup命令,要不使用scrapyd.如果使用nohup命令的话,爬虫挂掉了,你可能还不知道,你还得上服务器上查或者做额外的邮件通知操作.如果使用 ...

  10. VS中让用户选择路径

    //选择文件 OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); MessageBox.Show(ofd.FileName); / ...