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]的更多相关文章

  1. bzoj3223: Tyvj 1729 文艺平衡树 splay裸题

    splay区间翻转即可 /************************************************************** Problem: 3223 User: walf ...

  2. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  3. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2202  Solved: 1226[Submit][Sta ...

  4. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

  5. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

  6. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...

  7. 【BZOJ3223】 Tyvj 1729 文艺平衡树 Splay

    Description   您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2  ...

  8. 【Splay】bzoj3223 Tyvj 1729 文艺平衡树

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #i ...

  9. 【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树

    让蒟蒻见识到了常数大+滥用STL的危害. <法一>很久之前的Splay #include<cstdio> #include<algorithm> using nam ...

随机推荐

  1. [WCF编程]4.契约概述

    一.契约的基本概念 契约是消息参与者之间的约定.在SOA架构中,契约提供了服务通信所必需的元数据.契约用来定义数据类型,操作,消息交换模式和消息交换使用的传输协议.契约通常是在标准化平台中使用与编程语 ...

  2. super.getClass()方法调用

    下面程序的输出结果是多少?import java.util.Date;public class Test extends Date{public static void main(String[] a ...

  3. 自己动手写ORM的感受

    之前看到奋斗前辈和时不我待前辈的自己动手写ORM系列博客,感觉讲解的通俗易懂,清晰透彻.作为一个菜鸟,闲来也想着自己写一个ORM,一来加深自己对 ORM的理解,以求对EF,NHibernate等ROM ...

  4. extjs combobox 事件

    change---显示的值改变事件 select---选中选项事件 expand---下拉框展开事件 collapse--下拉框折叠事件 { xtype: 'container', width: 25 ...

  5. [python]初试页面抓取——抓取沪深股市交易龙虎榜数据

    [python]抓取沪深股市交易龙虎榜数据 python 3.5.0下运行 没做自动建立files文件夹,需要手动在py文件目录下建立files文件夹后运行 #coding=utf-8 import ...

  6. MyEclipse 2016 CI 4新增BootStrap模板

    Live Preview with CodeLive 目前CodeLive还只有Live Preview这一个功能,在后续的版本中会陆续添加新功能. 新增Bootstrap模板 在模板面板中选择相应的 ...

  7. For each循环中使用remove方法。

    List<String> list =new ArrayList<String>(); list.add("boss"); list.add("g ...

  8. OC KVC

    OC KVC KVC 全称 key valued coding 键值编码 在说KVC之前应该简单的了解一下反射机制 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法. 对于任意 ...

  9. MPMoviePlayerController属性,方法,通知整理

    属性 说明 @property (nonatomic, copy) NSURL *contentURL 播放媒体URL,这个URL可以是本地路径,也可以是网络路径 @property (nonatom ...

  10. 通过LoadRunner - Analyze详细分析页面元素请求

    众所周知LoadRunner录制某个链接,包括动态请求与js.css.jpg等静态请求. web_custom_request("动态请求", "URL=http://w ...