[BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转
传送门:BZOJ 3223 和 CodeVS 3243
第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值
实际上两道题的思路是一样的,第二题把值对应到位置就行了
这里我们用伸展树来解决,但其中用到了线段树中的标记思想
对于一个长度的为n的序列,我们把它的每一位向后移动一位,即1~n → 2~n+1,然后再在序列前后分别补上一位:1和n+2。所以我们需要建立一颗节点数为n+2的伸展树,而原序列中的1~n位分别对应新序列中的2~n+1位。
对于每次询问的l和r,实际上就是l+1和r+1,首先找到l+1前一位对应的元素和r+1后一位对应的元素,即l和r+2分别对应的元素的序号。然后将l对应元素通过splay操作旋转到根节点,将r+2对应元素旋转到根节点的右儿子,由排序二叉树的性质可知,r+2的左儿子就是序列l+1~r+1,也就是询问中需要翻转的区间。对r+2的左儿子直接打上标记,在之后找元素序号的时候要记得把标记下传,同时用swap操作来达到翻转的目的。
splay操作和rotate操作在此不赘述。
最后输出的时候,同样查找2~n+1对应的元素序号,将得到的值减1还原,对于CodeVS 3243就再套一个值输出即可。
这里给出BZOJ 3223的代码。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <ctime>
#include <queue>
#include <string>
#include <map>
typedef long long ll;
using namespace std;
const int MAXN = ;
int n, m;
int root;
int son[MAXN][], fa[MAXN], lazy[MAXN], siz[MAXN]; inline int gi() {
char c;
int sum = , f = ;
c = getchar();
while (c < '' || c > '') {
if (c == '-')
f = -;
c = getchar();
}
while (c >= '' && c <= '') {
sum = sum * + c - '';
c = getchar();
}
return sum * f;
} void build(int l, int r, int f) {
if (l > r)
return;
if (l == r) {
siz[l] = ;
fa[l] = f;
son[f][l > f] = l;
return;
}
int mid = (l + r) >> ;
build(l, mid - , mid);
build(mid + , r, mid);
siz[mid] = siz[son[mid][]] + siz[son[mid][]] + ;
fa[mid] = f;
son[f][mid > f] = mid;
} void pushdown(int o) {
swap(son[o][], son[o][]);
lazy[son[o][]] ^= ;
lazy[son[o][]] ^= ;
lazy[o] = ;
} int find(int o, int p) {
if (lazy[o])
pushdown(o);
int l = son[o][];
int r = son[o][];
if (siz[l] + == p)
return o;
if (siz[l] >= p)
return find(l, p);
else
return find(r, p - siz[l] - );
} void rotate(int x, int &to) {
int l, r;
int f = fa[x];
int ff = fa[f];
l = son[f][] == x ? : ;
r = l ^ ;
if (f == to)
to = x;
else
son[ff][son[ff][] == f] = x;
fa[x] = fa[f];
fa[f] = x;
fa[son[x][r]] = f;
son[f][l] = son[x][r];
son[x][r] = f;
siz[f] = siz[son[f][]] + siz[son[f][]] + ;
siz[x] = siz[son[x][]] + siz[son[x][]] + ;
} void splay(int x, int &to) {
while (x != to) {
int f = fa[x];
int ff = fa[f];
if (f != to) {
if ((son[f][] == x) ^ (son[ff][] == f))
rotate(x, to);
else
rotate(f, to);
}
rotate(x, to);
}
} void reserve(int l, int r) {
int x = find(root, l);
int y = find(root, r + );
splay(x, root);
splay(y, son[x][]);
lazy[son[y][]] ^= ;
} int main() {
n = gi();
m = gi();
build(, n + , );
root = (n + ) >> ;
for (int i = ; i <= m; i++) {
int l = gi();
int r = gi();
reserve(l, r);
}
for (int i = ; i <= n + ; i++)
printf("%d ", find(root, i) - );
return ;
}
[BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转的更多相关文章
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- 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标记表示是否翻转 ------------------------------------------------- ...
- bzoj 3223: Tyvj 1729 文艺平衡树 (splay)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3223 [题目大意] 给出一数列,问m次区间翻转后的结果. [题解] Splay 区间翻 ...
- BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)
题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...
- BZOJ 3223 Tyvj 1729 文艺平衡树 | Splay 维护序列关系
题解: 每次reverse(l,r) 把l-1转到根,r+1变成他的右儿子,给r+1的左儿子打个标记就是一次反转操作了 每次find和dfs输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...
随机推荐
- c#解压文件
今天做了一个异步上传文件后再直接解压的一个东西.到解压这找了好多资料,做了1个多小时,贴出来,给自己张张记性. HttpPostedFileBase imgFile = Request.Files[0 ...
- iOS开发:自定义控件实现手势解锁
自定义控件 1.提供initWithFrame:及initWithCoder:方法来初始化: 2.解锁控件只负责展示.触摸.绘图等,存储轨迹.判断轨迹等操作不是解锁控件要做的.因此要定义一个代理,将轨 ...
- 最近在做外贸网站的时候,需要大量的字体来充实页面,就学习了怎么引用Google Fonts
第一步,FQ进入谷歌官方字体网站:https://fonts.google.com 妥妥的. 第二步,点击你所选择字体演示块的右上角的加号,然后你所选择的字体会形成引用链接以及你所要写的css样式. ...
- ASP.NET MVC为字段设置多语言显示 [转]
这段时间一直在忙.NET的项目,今天就写一下关于.NET的文章吧,也很长时间没写过.NET的啦 在使用ASP.NET MVC3 的时候,使用元数据模式可以非常方便地设置每个 字段(或者说属性)以减少 ...
- 多线程完成socket
//服务器端代码 public class Service { //服务器 public static void main(String[] args) { ServerSocket serverSo ...
- 【Python③】python基本数据类型,变量和常量
基本数据类型 Python中,能直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,包括负整数,程序中的写法和数学上的一样,例如:6,-666,8888…… 计算机使用二进制,所 ...
- Visual Basic 2012 借助DataGridView控件将Excel 2010数据导入到SQL server 2012
(注:注释的颜色原本为绿色,在这里变为黑色,有点不便,但不会造成阅读影响.放入Visual Basic2012代码编辑器后会还原成绿色.) 摘 要:DataGridView控件作为数据传输的中介,只 ...
- SQLserver 备份和还原 失败
错误一: 备份对于服务器“xxxxxx”失败. System.Data.SqlClient.SqlError: 无法使用备份文件 'C:\Program Files\Microsoft SQL Ser ...
- 转 从腾讯那“偷 了”3000万QQ用户数据
http://www.icaijing.com/hot/article4899809/ http://news.cnblogs.com/n/533061/
- 场景5 Performance Management
场景5 Performance Management 性能调优(不能重启数据库) 索引 资源管理器 性能优化 统计分析 SQL性能分析 SPM (SQL执行计划管理) 堆表 :数据存储无序 位图索引 ...