BZOJ 3223 文艺平衡树
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
1 3
1 3
1 4
Sample Output
HINT
N,M<=100000
Source
splay区间操作大裸题不解释。
#include<queue>
#include<cstdio>
#include<cstdlib>
using namespace std; #define maxn 100010
const int inf = << ;
queue <int> team;
struct TREE
{
int ch[maxn][],fa[maxn],cnt,root;
int key[maxn],size[maxn],t[maxn]; inline int newnode()
{
if (team.empty()) return ++cnt;
int ret = team.front(); team.pop();
return ret;
} inline TREE()
{
root = newnode();
key[root] = -inf; ++t[root];
updata(root);
add(inf);
} inline int find(int rank,int have,int now)
{
if (have+size[ch[now][]]<rank&&have+size[ch[now][]]+t[now]>=rank) return now;
if (have+size[ch[now][]]+t[now]<rank) return find(rank,have+size[ch[now][]]+t[now],ch[now][]);
else return find(rank,have,ch[now][]);
} inline void updata(int now) { size[now] = size[ch[now][]] + size[ch[now][]] + t[now]; } inline void rotate(int x)
{
int y = fa[x],z = fa[y],l = ch[y][] != x,r = l ^ ;
if (z != ) ch[z][ch[z][] != y] = x;
fa[x] = z; fa[y] = x; fa[ch[x][r]] = y;
ch[y][l] = ch[x][r]; ch[x][r] = y;
updata(y); updata(x);
} inline void splay(int x,int aim)
{
int p = fa[aim];
while (fa[x] != p)
{
int y = fa[x],z = fa[y];
if (z != p)
{
if ((ch[z][] == y)^(ch[y][] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
if (aim == root) root = x;
} inline int search(int x)
{
int now = root;
while (now)
{
if (key[now] == x) break;
now = ch[now][x > key[now]];
}
return now;
} inline void add(int x)
{
int now = root,pre = ;
while (now)
{
pre = now;
now = ch[now][x > key[now]];
}
now = newnode();
fa[now] = pre; ch[pre][x > key[pre]] = now;
key[now] = x; ++t[now];
pre = now;
while (now)
{
updata(now);
now = fa[now];
}
splay(pre,root);
} inline int qrank(int x)
{
int now = root,ret = ;
while (key[now] != x)
{
if (x < key[now]) now = ch[now][];
else ret += size[ch[now][]] + t[now],now = ch[now][];
}
return ret + size[ch[now][]] + ;
} inline void insert(int x)
{
int p = search(x);
if (p)
{
splay(p,root);
++t[p];
updata(p);
}
else add(x);
} inline void del(int x)
{
int now = search(x),k = qrank(x);
int p = find(k-,,root),q = find(k + t[now],,root);
splay(p,root);
splay(q,ch[p][]);
if (--t[now])
{
updata(now);
updata(q);
updata(p);
}
else
{
ch[q][] = ; fa[now] = ;
updata(q);
updata(p);
team.push(now);
}
} inline int ask(int x,int sign)
{
int now = root,ret;
while (now)
{
if (sign)
{
if (key[now] > x)
ret = now,now = ch[now][];
else now = ch[now][];
}
else
{
if (key[now] < x)
ret = now,now = ch[now][];
else now = ch[now][];
}
}
return key[ret];
}
}tree; inline int read()
{
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int main()
{
freopen("3224.in","r",stdin);
freopen("3224.out","w",stdout);
int T = read();
while (T--)
{
int opt = read();
if (opt == ) tree.insert(read());
else if (opt == ) tree.del(read());
else if (opt == ) printf("%d\n",tree.qrank(read())-);
else if (opt == ) printf("%d\n",tree.key[tree.find(read()+,,tree.root)]);
else if (opt == ) printf("%d\n",tree.ask(read(),));
else printf("%d\n",tree.ask(read(),));
}
fclose(stdin); fclose(stdout);
return ;
}
BZOJ 3223 文艺平衡树的更多相关文章
- [题解]bzoj 3223 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- BZOJ 3223 文艺平衡树 [codevs3303翻转区间]
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...
- bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17715 Solved: 7769[Submit][Status][ ...
- bzoj 3223 文艺平衡树 Splay 打标志
是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...
- [bzoj3224]普通平衡树/3223文艺平衡树
这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...
- 3223. 文艺平衡树【平衡树-splay】
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
随机推荐
- php动态网页实现页面静态化 通过在初次被访问时生成html文件保存起来,下次该PHP程序被访问时就直接找到以前被访问过的html页面
一.什么是静态页面?什么是动态页面 静态页面是网页的代码都在页面中,不需要执行asp,php,jsp,.net等程序生成客户端网页代码的网页.不能 静态页面 动态页面 区别: ...
- AlertDialog.Builder setCancelable用法
AlertDialog.Builder的setCancelable public AlertDialog.Builder setCancelable (boolean cancelable) Sinc ...
- OC的单例模式的实现
下面是在ARC,GCD下的单例模式实现: 头文件里申明类方法getInstance: #import <Foundation/Foundation.h> @interface Single ...
- 将.lib库文件转换成.a库文件的工具
分享如下两个链接: 微盘: http://vdisk.weibo.com/s/ztzPTJIC52mz2 百度云盘: http://pan.baidu.com/s/11gTOc 使用方法,解压文件mi ...
- DM8168 GPIO驱动与測试程序
本次測试针对GPIO1进行,挑选了GP1[31],引脚的复用默认的就是GPIO 还是老规矩,贴上driver.c,Makefile,test.c: dm8168_gpio.c: #include &l ...
- CSU1312:榜单(模拟)
Description ZZY很喜欢流行音乐,每周都要跟踪世界各地各种榜单,例如Oricon和Billboard,现在给出每周各个单曲的销量请给出每周的TOP5以及TOP5中各个单曲的浮动情况. 量的 ...
- Marketplace Client- Download
Marketplace是Java平台被广泛使用的IDE(集成开发环境)Eclipse的软件商店.上面有个有种牛X的插件,可根据自己需要下载. Eclipse Marketplace官网地址:http: ...
- hadoop2集群中的datanode启动以后自动关闭的问题
今天在启动前几天搭建成功的hadoop集群的时候,出现了datanode启动之后自动关闭的情况,经过查询之后发现问题产生的原因是:在第一次格式化dfs后,启动并使用了hadoop,后来又重新执行了格式 ...
- Sql2008中使用DataTable作为存储过程的参数
使用DataTable作为存储过程的参数 最近工作中写了几个存储过 程,需要向存储过程中传递字符串,因为SQL Server 2000中没有内置类似于 split 的函数,只好自己处理,将前台数据 ...
- 利用SQLiteOpenHelper创建数据库,进行增删改查操作
Android中提供SQLiteOpenHelper类,在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象.继承和扩展SQLiteOpenHelper类主要做的工作就是重写 ...