SGU 187 Twist and whirl - want to cheat(splay)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=187
题意:给出一个长度为n的数列,每次将某个区间的数字翻转。求最后的序列。
思路:将数列两端增加两个哨兵,也就是现在有n+2个数,其中1到n映射到现在的2到n+1。对于每个区间[A,B],将A-1调整到根节点,将B+1调整到根节点的右子树,那么整个区间就到了根节点右子树的左子树(设该节点为P)。将P标记为翻转。每次查找时翻转标记向下传递,类似于线段树的那种操作。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std;const int N=130005; int n,m; int c[N][2],p[N],s[N],sign[N]; int root;void update(int f,int x,int flag) {p[x]=f;if(!f) return;c[f][flag]=x;s[f]=1+s[c[f][0]]+s[c[f][1]]; }void down(int x) {if(!sign[x]) return;sign[x]=0;swap(c[x][0],c[x][1]);sign[c[x][0]]^=1;sign[c[x][1]]^=1; }void rotate(int x) {int P=p[x],G=p[P];if(c[P][0]==x){update(P,c[x][1],0);update(x,P,1);update(G,x,!(c[G][0]==P));}else{update(P,c[x][0],1);update(x,P,0);update(G,x,!(c[G][0]==P));} }void splay(int x,int &goal) {int P=p[x],G=p[P],limit=p[goal];while(P!=limit){if(G!=limit&&(c[G][0]==P)==(c[P][0]==x)) rotate(P);rotate(x);P=p[x];G=p[P];}goal=x;down(goal); }int select(int x) {int t=root;while(1){down(t);if(s[c[t][0]]+1==x) break;if(s[c[t][0]]+1>x) t=c[t][0];else x-=s[c[t][0]]+1,t=c[t][1];}return t; }int build(int L,int R) {if(L>R) return 0;int m=(L+R)>>1;update(m,build(L,m-1),0);update(m,build(m+1,R),1);return m; }int main() {scanf("%d%d",&n,&m);root=build(1,n+2);int A,B,i,r1,r2;while(m--){scanf("%d%d",&A,&B);r1=select(A);r2=select(B+2);splay(r1,root);splay(r2,c[r1][1]);i=c[root][1];i=c[i][0];sign[i]^=1;}for(i=2;i<=n+1;i++) printf("%d ",select(i)-1);puts("");return 0; }
SGU 187 Twist and whirl - want to cheat(splay)相关推荐
- SGU 187.Twist and whirl - want to cheat( splay )
维护一个支持翻转次数M的长度N的序列..最后输出序列.1<=N<=130000, 1<=M<=2000 splay裸题... ------------------------- ...
- SGU 187 - Twist and whirl -- want to cheat
原题地址:http://acm.sgu.ru/problem.php?contest=0&problem=187 太开心啦!!!!这道题从2013年开始困扰我!!今天晚上第四次下定决心把它写一 ...
- 【Splay】[SGU 187]Twist and whirl - want to cheat
这道题真是我见过的最坑的平衡树的题目.... time limit per test: 0.25 sec. memory limit per test: 4096 KB 这个也太小了吧! 题目大意就是 ...
- sgu 187 Twist and whirl - want to cheat 伸展树(splay)
裸的区间翻转..直接写个splay就行... #include <iostream> #include <cstdio> #include <algorithm> ...
- SGU 187.Twist and whirl - want to cheat
splay 不过竟然用reverse一发水过了... 调用STL的代码:(156ms) #include <iostream> #include <cstdio> #inclu ...
- SGU 187 Twist and whirl - want to cheat
伸展树... 赤果果地抄袭雷哥的代码:http://www.cnblogs.com/jianglangcaijin/archive/2013/01/21/2869148.html 题意:输入n,m.给 ...
- ros发布节点信息python_ROS Twist和Odometry消息类型使用(Python)
消息类型: 1. Twist - 线速度角速度 通常被用于发送到/cmd_vel话题,被base controller节点监听,控制机器人运动 geometry_msgs/Twist geometry ...
- SGU 252 Railway Communication(KM)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=252 题意:一个有向图.选择最少的路径覆盖所有点.在路径最少的情况下,使得所有路径经 ...
- SGU 201 Non Absorbing DFA (DP)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个自动机,给出所有的转移,其中还有一个 ...
最新文章
- .NET 线程问题汇总
- PHP后期静态绑定概念和用法
- 【转】VS.NET2003 调试无法启动
- LSGO软件技术团队2015~2016学年第七周(1012~1018)总结
- Huffman树压缩和解压文件
- mycat 从入门到放弃 (转)
- 【报告分享】中国年轻用户电商消费洞察报告:寻找电商换道增长机遇.pdf(附下载链接)...
- 第二十:如何把Jenkins+Allure2生成的报告发送邮件(重点超详细)
- 理解vue中的组件(二)
- php 之 json格式
- 使用IDEA,利用SpringMVC框架建立HelloWorld项目
- springboot项目license_license · 开源的SpringBoot前后端分离项目/framework - Gitee.com
- 商业虚拟专用网络技术二通用路由封装
- 基于jQuery发射弹幕的一个小案例
- c mysql加密解密_mysql内置加密函数对数据加密
- Python float基本用法
- python运行调出控制台_python控制台怎么打开
- java 导出word试题
- STM32中断优先级管理
- Vue3项目基本知识点