[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输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...
随机推荐
- 删除Xcode中的 证书文件
Finder->前往 ~/Library/MobileDevice/Provisioning Profiles
- java selenium (一) selenium 介绍
Selenium 是目前用的最广泛的Web UI 自动化测试框架. 本系列文章,将深入简出来讲解selenium 的用法 文章的末尾处, 有整个系列的链接 阅读目录 selenium 的命名 sele ...
- php 数组的常用函数
在php教程中数组是种强大的数据类型,他可以做的事情很多,可以存储不同的数据类型在一个数组中,下面我们列出了数组常用的操作,排序,键名对数组排序等做法. /* 数组的常用函数 * * 数组的排序函 ...
- linux下关于Apache设置二级域名绑定二级目录的方法
背景:对于一些论坛网站,某些目录需要制定二级域名去访问,这时候就要在apache中用二级域名绑定二级目录. 方法: 1.首先你要找到apache安装路径,在apahce安装路径下的conf文件夹中找到 ...
- bzoj 3172: [Tjoi2013]单词
#include<cstdio> #include<cstring> #include<iostream> #define M 1000008 using name ...
- PHP是什么
php 是一种服务器端的,嵌入html的脚本语言.php区别其他像客户端java的地方是它的代码在服务器端执行.php能做什么? 最低水平,php可以做任何其他cgi程序所能做的事,例如收集表格数据, ...
- 如何快速掌握一款新的MCU? (转)
发布时间:2013-12-15 10:27:51 技术类别:单片机 个人分类:话题思考 任何一款MCU,其基本原理和功能都是大同小异,所不同的只是其外围功能模块的配置及数量.指 ...
- code of C/C++(2)
初学者学习构造函数和析构函数,面对如何构造的问题,会头大.这里提供了变量(int,double,string),char *,字符数组三个类型的私有成员初始化的方法 //char * 类型的成员,如何 ...
- JS中函数的调用和this的值
调用每一个函数会暂停当前函数的执行,传递控制权和参数给新函数.除了声明时定义的形式参数,每个函数还接收两个附加的参数:this 和 arguments. 参数this在面向对象编程中非常重要,他的值取 ...
- 设计模式(2)--单例模式(Singleton Pattern)
概述 一个类能返回对象一个引用(永远是同一个)和一个获得该实例的方法(必须是静态方法,通常使用getInstance这个名称):当我们调用这个方法时,如果类持有的引用不为空就返回这个引用,如果类保持的 ...