BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 3595 Solved: 2029
[Submit][Status][Discuss]
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
splay的序列操作
操作[l,r],l-1splay到根,r+1splay到根的rc,r+1的lc就是我们要操作的序列了
反转操作打一个flp标记
kth和print时(用到孩子时)都要pushDown标记
用到了build函数
注意:
1.1和n+2是虚拟节点,否则[1,n]区间要特判
2.本题节点下标就是v了,一个序列嘛
关于标记
kth中pushDown了,splay操作时已经是pushDown过了的了
[2016-12-24]
注意此处标记的定义是x没有进行标记操作
也可以按照进行标记操作了来写,见下面
//
// main.cpp
// 文艺平衡树
//
// Created by Candy on 28/11/2016.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
struct node{
int ch[],fa,w,size;
int flp;
}t[N];
int cnt,root;
inline int wh(int x){return t[pa].ch[]==x;}
inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;}
inline void pushDown(int x){
if(t[x].flp){
swap(lc,rc);
t[lc].flp^=;t[rc].flp^=;
t[x].flp=;
}
}
int build(int l,int r){
if(l>r) return ;
int x=(l+r)>>;
lc=build(l,x-);rc=build(x+,r);
t[lc].fa=t[rc].fa=x;
t[x].w=;t[x].flp=;
update(x);
return x;
}
void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(g) t[g].ch[wh(f)]=x;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
t[x].fa=g;
update(f);update(x);
}
void splay(int x,int tar){
for(;t[x].fa!=tar;rotate(x))
if(t[pa].fa!=tar) rotate(wh(x)==wh(pa)?pa:x);
if(tar==) root=x;
}
int kth(int k){
int x=root,ls=;
while(x!=){
pushDown(x);
int _=ls+t[lc].size;
if(_<k&&k<=_+t[x].w) return x;
if(k<=_) x=lc;
else ls=_+t[x].w,x=rc;
}
return -;
}
int n,m,l,r;
void print(int x){
if(x==) return;
pushDown(x);
if(lc) print(lc);
if(x!=&&x!=n+) printf("%d ",x-);
if(rc) print(rc);
}
int main(int argc, const char * argv[]) {
n=read();m=read();
root=build(,n+);
while(m--){
l=read();r=read();
splay(kth(l),);
int x=kth(r+);
splay(x,root);
t[lc].flp^=;
}
print(root);
return ;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
struct node{
int ch[],fa,w,size;
int flp;
}t[N];
int cnt,root;
inline int wh(int x){return t[pa].ch[]==x;}
inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;}
inline void paint(int x){
t[x].flp^=;
swap(lc,rc);
}
inline void pushDown(int x){
if(t[x].flp){
paint(lc);
paint(rc);
t[x].flp=;
}
}
int build(int l,int r){
if(l>r) return ;
int x=(l+r)>>;
lc=build(l,x-);rc=build(x+,r);
t[lc].fa=t[rc].fa=x;
t[x].w=;t[x].flp=;
update(x);
return x;
}
void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(g) t[g].ch[wh(f)]=x;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
t[x].fa=g;
update(f);update(x);
}
void splay(int x,int tar){
for(;t[x].fa!=tar;rotate(x))
if(t[pa].fa!=tar) rotate(wh(x)==wh(pa)?pa:x);
if(tar==) root=x;
}
int kth(int k){
int x=root,ls=;
while(x!=){
pushDown(x);
int _=ls+t[lc].size;
if(_<k&&k<=_+t[x].w) return x;
if(k<=_) x=lc;
else ls=_+t[x].w,x=rc;
}
return -;
}
int n,m,l,r;
void print(int x){
if(x==) return;
pushDown(x);
if(lc) print(lc);
if(x!=&&x!=n+) printf("%d ",x-);
if(rc) print(rc);
}
int main(int argc, const char * argv[]) {
n=read();m=read();
root=build(,n+);
while(m--){
l=read();r=read();
splay(kth(l),);
int x=kth(r+);
splay(x,root);
paint(lc);
}
print(root);
return ;
}
BZOJ3223: Tyvj 1729 文艺平衡树 [splay]的更多相关文章
- bzoj3223: Tyvj 1729 文艺平衡树 splay裸题
splay区间翻转即可 /************************************************************** Problem: 3223 User: walf ...
- [BZOJ3223]Tyvj 1729 文艺平衡树
[BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[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树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- 【BZOJ3223】 Tyvj 1729 文艺平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- 【Splay】bzoj3223 Tyvj 1729 文艺平衡树
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #i ...
- 【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树
让蒟蒻见识到了常数大+滥用STL的危害. <法一>很久之前的Splay #include<cstdio> #include<algorithm> using nam ...
随机推荐
- sql 中的Bulk和C# 中的SqlBulkCopy批量插入数据 ( 回顾 and 粗谈 )
通常,我们会对于一个文本文件数据导入到数据库中,不多说,上代码. 首先,表结构如下. 其次,在我当前D盘中有个文本文件名为2.txt的文件. 在数据库中,可以这样通过一句代码插入. Bulk in ...
- 使用js批量选中功能实现更改数据库中的status状态值(批量展示)
我们在开发项目的时候经常会在后台管理时用到批量展示功能来动态的修改数据库的值.下面以修改数据库的status状态值来实现批量展示功能.批量选中功能引用js来实现.前端html代码: <table ...
- js中XMLHttpRequest对象实现GET、POST异步传输
js中XMLHttpRequest对象实现GET.POST异步传输 /* * 统一XHR接口 */ function createXHR() { // IE7+,Firefox, Opera, Chr ...
- python PIL比较图片像素
# -*- coding: utf-8 -*- from PIL import Image from pylab import * def compare_pic_L(pic1,pic2): #打开第 ...
- async.whilst 的一个简化版实现
function whilst(condition, todo, fin){ var cb = function(){ if(condition()){ todo(cb); }else{ fin(); ...
- No.026:Remove Duplicates from Sorted Array
问题: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 关于arcgis engine的工作空间(IWorkspace)和选择集(FeatureSelection)
1.通过某个WorkspaceFactoryClass(例如AccessWorkspaceFactoryClass)拿到工作空间工厂接口,这时的OpenFromFile方法可以直接打开mdb类型文件, ...
- Atitit.报名模块的管理
Atitit.报名模块的管理 1.1. 统计报名数据1 1.2. 存储1 1.3. 报名1 1.4. 查看报名数据3 1.1. 统计报名数据 select count(*) as nums from ...
- Android Studio 快捷键一览
刚从 eclipse 转到 android studio 的同学,编写代码时使用的快捷键不同,一时难以适应,当然可以通过设置,将快捷键模板设置成与 eclipse 相同的,但我个人不赞成,因为 And ...
- IOS开发基础知识--碎片45
1:iOS SEL的简单总结 SEL就是对方法的一种包装.包装的SEL类型数据它对应相应的方法地址,找到方法地址就可以调用方法 a.方法的存储位置 在内存中每个类的方法都存储在类对象中 每个方法都有一 ...