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标记表示是否翻转 ------------------------------------------------- ...
随机推荐
- C# 保存窗口为图片(保存纵断面图)
源代码例如以下: #region 保存纵断面截图 private void button_save_Click(object sender , EventArgs e) { SaveFileDialo ...
- mysqldump 定时任务 执行后备份的文件为空
#!/bin/bash mysql_host="127.0.0.1" mysql_user="root" mysql_passwd="******** ...
- Fix java version mismatch in windows---stackoverflow
Question: I have the 64bit version of the jdk installed on windows 7. I installed the 32 bit version ...
- java获取计算机硬件参数
public class HardWareUtils { /** * * 获取主板序列号 * * * * @return */ public static String g ...
- Html5新增的语义化标签(部分)
2014年10月29日,万维网联盟宣布,经过接近8年的艰苦努力,html5的标准规范终于制定完成.这是互联网的一次重大变革,这也许是一个时代的来临! 总结一些h5新增的语义化标签,记录下来方便自己学习 ...
- IIS7.5 asp+access数据库连接失败处理 64位系统
IIS7.5 asp+access数据库连接失败处理(SRV 2008R2 x64/win7 x64) IIS7.5不支持oledb4.0驱动?把IIS运行模式设置成32位就可以了,微软没有支持出64 ...
- vim字符串替换
vi/vim 中可以使用 :s 命令来替换字符串.以前只会使用一种格式来全文替换,今天发现该命令有很多种写法(vi 真是强大啊,还有很多需要学习),记录几种在此,方便以后查询. :s/vivian/s ...
- SQL数据库安装
安装过程中经常出现失败或者提示,那么久要清楚干净所有的数据在重新安装,步骤如下. SQL2008卸载 一.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板” 2)点击“卸载程序”. 卸载与 ...
- mysql - 初探
1,查询所有数据库名称: show databases; 2,查询所有表: use database_name; show tables; 3,查询表中的所有字段: desc table_name;
- Bootstrap Table的例子(转载)
转载自:http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#classes-table 使用的API: data1.json da ...