csu1365 Play with Chain
很显然的splay,第一次用splay操作区间...我实在佩服这个targan大佬,居然搞出这么牛逼的平衡树,调了大概5个小时终于搞定了。。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 300005
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 9997
struct tree{//splay树
int sz[];//以当前结点为根的左右子树大小,不包括当前结点
int son[];//当前结点的左右儿子
int fa;//当前结点的父亲
int reverse;//翻转标记
int v;//当前结点表示的权值
}a[maxn];
int root;//根节点
int len;
int n,m,x,y,z;
bool f;
int isrson(int p,int f){
return a[f].son[] == p;
}
void update(int p){//更新p结点的size
if(a[p].son[] != -) a[p].sz[] = a[a[p].son[]].sz[] + a[a[p].son[]].sz[] + ;
else a[p].sz[] = ;
if(a[p].son[] != -) a[p].sz[] = a[a[p].son[]].sz[] + a[a[p].son[]].sz[] + ;
else a[p].sz[] = ;
}
void pushdown(int p){
swap(a[p].son[],a[p].son[]);
update(p);
a[p].reverse ^= ;
int ls = a[p].son[],rs = a[p].son[];
if(ls != -) a[ls].reverse ^= ;
if(rs != -) a[rs].reverse ^= ;
}
int find(int x){//找到序列第x个数对应的结点
int p = root;
for(;;){
if(a[p].reverse) pushdown(p);
if(a[p].sz[] >= x) p = a[p].son[];
else if(a[p].sz[] + == x) return p;
else x = x - a[p].sz[] - ,p = a[p].son[];
}
}
void changeson(int p,int f,int v){//把p作为f的v儿子,p,f不一定存在
if(f != -) a[f].son[v] = p;
if(p != -) a[p].fa = f;
if(f == -) root = p;
}
void rotate(int p){
int f = a[p].fa,g = a[f].fa;
int u = isrson(p,f),v = isrson(f,g);
changeson(a[p].son[u^],f,u),changeson(f,p,u^),changeson(p,g,v);//改变p,f,g的父子关系
update(f),update(p);
}
void splay(int p,int tar){
if(a[p].fa == tar) return;
while(a[a[p].fa].fa != tar && a[p].fa != tar){
int f = a[p].fa,g = a[f].fa;
int u = isrson(p,f),v = isrson(f,g);
if(u ^ v) rotate(p),rotate(p);
else rotate(f),rotate(p);
}
if(a[p].fa != tar) rotate(p);
}
void getsection(int x,int y){//得到[x,y]区间
splay(find(x - ),-),splay(find(y + ),root);
}
void build(int l,int r){
len++;
int p = len;
int mid = (l + r) >> ;
a[p].v = mid,a[p].sz[] = a[p].sz[] = ;
a[p].son[] = a[p].son[] = -;
if(l == r) return;
if(l <= mid - ) changeson(len + ,p,),build(l,mid-);
if(mid + <= r) changeson(len + ,p,),build(mid+,r);
update(p);
}
void flip(int x,int y){
getsection(x,y);//得到区间[x,y]后打标记
int p = a[a[root].son[]].son[];
a[p].reverse ^= ;
}
void cut(int x,int y,int z){//cut [x,y] z 把区间x,y剪切到z位置后
//先得到区间[x,y],将其x-1rotate到根,将y+1rotate到根的儿子,则y+1结点的左儿子为根的树即为该区间
getsection(x,y);
int p = a[a[root].son[]].son[];
a[a[p].fa].son[] = -;
update(a[p].fa),update(root);
int t = p;
p = find(z);
splay(p,-),p = find(z + ),splay(p,root);
p = a[root].son[],a[p].son[] = t,a[t].fa = p;
update(p),update(root);
}
void print(int p){
if(p == -) return;
if(a[p].reverse) pushdown(p),update(p);
print(a[p].son[]);
if(a[p].v != && a[p].v != n + )
if(!f) printf("%d",a[p].v),f = true;
else printf(" %d",a[p].v);
print(a[p].son[]);
}
int main(){
freopen("a.in","r",stdin);
freopen("b.out","w",stdout);
char op[];
while(~scanf("%d%d",&n,&m)){
if(n < && m < ) break;
len = ,root = ,f = false;
build(,n+);
a[root].fa = -;
for(int i = ;i <= m;++i){
scanf("%s",op);
if(op[] == 'C'){
scanf("%d%d%d",&x,&y,&z);
++x,++y,++z;
cut(x,y,z);
continue;
}
scanf("%d%d",&x,&y);
++x,++y;
flip(x,y);
}
print(root),putchar('\n');
}
return ;
}
csu1365 Play with Chain的更多相关文章
- STM32用JLINK 烧写程序时出现NO Cortex-m device found in JTAG chain现象和解决方案
现象 CPU: STM32107VC 用JLINK 烧写程序时出现NO Cortex-m device found in JTAG chain 如图无法查找到硬件就是CPU 提示1:NO Cortex ...
- 责任链模式/chain of responsibility/行为型模式
职责链模式 chain of responsibility 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处 ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- arm,iptables: No chain/target/match by that name.
最近由于项目需要,需要打开防火墙功能. 公司有 arm linux 3.0x86 linux 3.2x86 linux 2.4 的三个嵌入式.都需要打开防火墙功能. 执行“whereis iptabl ...
- C#设计模式系列:职责链模式(Chain of Responsibility)
1.职责链模式简介 1.1>.定义 职责链模式是一种行为模式,为解除请求的发送者和接收者之间的耦合,而使多个对象都有机会处理这个请求.将这些对象连接成一条链,并沿着这条链传递该请求,直到有一个对 ...
- [工作中的设计模式]责任链模式chain
一.模式解析 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知 ...
- track message forwards, avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain
https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html The TRACE method is used to invoke a remote, ...
- JAX-WS使用Handler Chain加工消息
承前 本文的示例,是基于前一篇文章中的实例而改进的,如果想要运行本文的代码例子,需要先实现前一篇的代码. 前一篇文章JAX-WS开发WebService初级 Handler概念 在WebService ...
- 配置DelegatingFilterProxy使用Spring管理filter chain
项目环境:JDK7 + Maven3.04 0. 项目使用springmvc作为controller层 1. 引入spring-security <dependency> <grou ...
随机推荐
- 记录一下java在后端用request来判断请求类型
这几天看代码,看到这么一个操作. String requestType = request.getHeader("X-Requested-With"); 于是各种查找 这里记 ...
- 微信小程序组件解读和分析:六、progress进度条
progress进度条组件说明: 进度条,就是表示事情当前完成到什么地步了,可以让用户视觉上感知事情的执行.progress进度条是微信小程序的组件,和HTML5的进度条progress类似. pro ...
- mysql 的 case when then 用法 和null 的判断
表:一个表 aa 有两个字段 id 和 sex ,第1条记录的sex 为空串 ('') 第二条记录的sex 为空 (null) 1. 用法: 第一种: select (case 字段名 whe ...
- 开源一个一个NodeJS 代理服务器扫描工具,可以用来***
鉴于我朝很多网站访问不了,google等就是大悲剧,之前一直在用VPN,但是公司内网VPN被封,诸多工具也惨遭毒手..我辈怎能容忍. 目前只有代理没有被封,于是搞了个代理扫描工具并开源: https: ...
- 【转】Nutz | Nutz项目整合Spring实战
http://blog.csdn.net/evan_leung/article/details/54767143 Nutz项目整合Spring实战 前言 Github地址 背景 实现步骤 加入spri ...
- H5 canvas pc 端米字格 写字板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 给SVN控制的项目添加忽略文件/文件夹
忽略目录其实有些像建立一个文件夹,但却不放入版本控制.如果不加入版本控制又会在svn status命令中显示出来,很不方便,所以可以设置本文件夹属性,让它既加入版本控制,又忽略其变化 未加入控制的文件 ...
- [驱动] 一个简单内核驱动,通过qemu调试(1)
模块 通过在HOST上修改linux kernel源代码,重新编译一个vmlinux,然后,通过qemu根据这个bzImage 启动一个vm,进行调试 #cat drivers/char/test.c ...
- MongoDB安装与配置启动
1.下载安装包.mongodb-linux-x86_64-rhel62-3.6.3.tgz 2.解压.修改名字. 3.修改配置文件: # mongodb.conf #where to loglogpa ...
- 根据Dockerfile创建hello docker镜像
一.编写hello可执行c文件: 1.安装:gcc glibc glibc-static yum install -y gcc glibc glibc-static 2.编写hello.c:vim h ...