[置顶] hdu 1890 伸展树区间翻转
题意: 给你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 伸展树区间翻转的更多相关文章
- hdu 5919 主席树(区间不同数的个数 + 区间第k大)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- [置顶] hdu 4699 2个栈维护 or 伸展树
hdu 4699 Editor 题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求到k位置的最大的前缀和.. 注意这里的k是在光标之前的, ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- hdu 1890 splay树
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- HDU 4348 主席树区间更新
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 3308(线段树区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- Google2016开发者大会
Android主讲: 一.吴晶:android笔记博主(博客:http://www.race604.com/) 主题:Android低功耗蓝牙(BLE)实践 低功耗蓝牙在可穿戴和智能家居里边用的比较多 ...
- ANSI escape code
最近在做iOS上的SSH终端项目,主要是在手机上远程连接Unix系统,并进行一些简单的指令操作,类似于SecureCRT:今天想总结一下这个项目中遇到的新东西----ANSI escape code. ...
- UIGestureRecognizer手势识别
UIGestureRecognizer 1.#import "ViewController.h"2.3.@interface ViewController ()<UIGest ...
- centos 用户切换
在系统的/etc/.bash_profile中已经配置了各种环境变量. 用账户a登陆,ldd xxx.so查看一切链接正常. 用账户root登陆,ldd xxx.so查看一切链接正常. 用账户a登陆, ...
- 【USACO 2.4.3】牛的旅行
[描述] 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
- hdu1142 A Walk Through the Forest( Dijkstra算法+搜索)
看到这道题,想起了我家旁边的山! 那是一座叫做洪山寨的山,据说由当年洪秀全的小妾居住于此而得名! 山上盛产野果(很美味)! 好久没有爬上去了! #include<stdio.h> #inc ...
- oracle操作字符串:拼接、替换、截取、查找
1.拼接字符串 1)可以使用“||”来拼接字符串 select '拼接'||'字符串' as str from dual 2)通过concat()函数实现 select concat('拼接', '字 ...
- ch01.深入理解C#委托及原理(转)
ch01..深入理解C#委托及原理_<没有控件的ASPDONET> 一.委托 设想,如果我们写了一个厨师做菜方法用来做菜,里面有 拿菜.切菜.配菜.炒菜 四个环节,但编写此方法代码的人想让 ...
- 修改本地数据库root权限密码
方法1: 用SET PASSWORD命令 测试成功 首先登录MySQL @1——mysql DOS 窗口中. 格式:mysql> set password for 用户名@localhost = ...
- 新建PCH文件以及常用宏定义
$(SRCROOT)/项目名/pch文件名.pch //0-255的随机数 #define randint arc4random() % 256 //随机色 #define randColor [UI ...