热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU3487PlaywithChain

题意:对序列取出连续的一段接到剩下的第k个值后面,或者把一段序列反转。解题思路:splay区间操作。解题代码:1FileName:hdu3487.cpp2Author:darkdr

题意:对序列取出连续的一段接到剩下的第k个值后面,或者把一段序列反转。

解题思路:splay 区间操作。

解题代码:

技术分享技术分享
  1 // File Name: hdu3487.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年04月09日 星期四 10时16分12秒
  4 
  5 #include
  6 #include
  7 #include
  8 #include<set>
  9 #include
 10 #include
 11 #include
 12 #include
 13 #include
 14 #include
 15 #include
 16 #include
 17 #include
 18 #include
 19 #include
 20 #include
 21 #include
 22 #include
 23 #include
 24 #define LL long long
 25 #define maxn 310000
 26 using namespace std;
 27 
 28 char str[10];
 29 int ta,tb,tc;
 30 int n , m ; 
 31 int lastans[maxn];
 32 struct SplayTree{
 33     int sz[maxn];
 34     int ch[maxn][2];
 35     int pre[maxn];
 36     int root ,top1,top2;
 37     int ss[maxn],que[maxn];
 38     int rev[maxn];
 39     inline void Rotate(int x, int f){
 40         int y = pre[x];
 41         push_down(y);
 42         push_down(x);
 43         ch[y][!f] = ch[x][f];
 44         pre[ch[x][f]] = y ; 
 45         pre[x] = pre[y];
 46         if(pre[x]) ch[pre[y]][ch[pre[y]][1] == y]  = x; 
 47         ch[x][f] = y; 
 48         pre[y] = x; 
 49         push_up(y);
 50     }
 51     inline void Splay(int x, int goal){
 52         push_down(x);
 53         while(pre[x] != goal){
 54             if(pre[pre[x]] == goal){
 55                 Rotate(x,ch[pre[x]][0] == x);
 56             }else{
 57                 int y = pre[x],z = pre[y];
 58                 int f = (ch[z][0] == y);
 59                 if(ch[y][f] == y ){
 60                     Rotate(x,!f),Rotate(x,f);
 61                 }else{
 62                     Rotate(y,f),Rotate(x,f);
 63                 }
 64             }
 65         }
 66         push_up(x);
 67         if(goal == 0 ) root = x;
 68     }
 69     inline void RotateTo(int k ,int goal){
 70         int x = root ; 
 71         push_down(x);
 72         while(sz[ch[x][0]] != k ){
 73             if(k 0]]){
 74                 x = ch[x][0];
 75             }else{
 76                 k -= (sz[ch[x][0]] +1) ; 
 77                 x = ch[x][1];
 78             }
 79             push_down(x);
 80         }
 81         Splay(x,goal);
 82     }
 83     inline void NewNode(int &x ,int c){
 84          if(top2) x = ss[--top2];
 85          else x = ++ top1;
 86          ch[x][0] = ch[x][1] = pre[x] = 0 ; 
 87          sz[x] = 1; 
 88          
 89          val[x] = c ;
 90          rev[x] = 0 ;
 91 
 92     }
 93     inline void push_down(int x){
 94         if(rev[x])
 95         {
 96             swap(ch[x][0],ch[x][1]);
 97             rev[ch[x][0]] ^= 1; 
 98             rev[ch[x][1]] ^= 1; 
 99             rev[x] = 0 ; 
100         }
101     }
102     inline void push_up(int x){
103         sz[x] = 1 + sz[ch[x][0]] + sz[ch[x][1]];
104     }
105     inline void makeTree(int &x, int l ,int r ,int f){
106         if(l > r) return ;
107         int m = (l + r) >> 1; 
108         NewNode(x, m);
109         makeTree(ch[x][0] , l , m-1, x);
110         makeTree(ch[x][1] , m+1 , r, x);
111         pre[x] = f; 
112         push_up(x);
113     }
114     inline void init(int n){
115         rev[0] = ch[0][1] = ch[0][0] = pre[0] = sz[0] = 0 ;
116         root  = top1 =0 ; 
117         NewNode(root,-1);
118         NewNode(ch[root][1],-1);
119         pre[ch[root][1]] = root ;
120         sz[root] = 2; 
121     
122         makeTree(ch[ch[root][1]][0],1,n,ch[root][1]);
123         push_up(ch[root][1]);
124         push_up(root);
125     }
126     inline void cut(int l,int r, int si){
127     
128         RotateTo(l-1,0);
129         RotateTo(r+1,root);
130 
131         int del = ch[ch[root][1]][0];
132         ch[ch[root][1]][0] = 0 ; 
133         push_up(ch[root][1]);
134         push_up(root);
135         RotateTo(si,0);
136         RotateTo(si+1,root);
137         pre[del] = ch[root][1];
138         ch[ch[root][1]][0] = del ;
139         Splay(ch[ch[root][1]][0],0);
140         //push_up(ch[root][1]);
141         //push_up(root);
142     }
143     inline void flip(int l , int r )
144     {
145         RotateTo(l-1,0);
146         RotateTo(r+1,root);
147         rev[ch[ch[root][1]][0]] ^= 1; 
148         Splay(ch[ch[root][1]][0],0);
149     }
150     void print(int x,int k ){
151         if(x == 0 )
152             return;
153         push_down(x);
154         print(ch[x][0],k);
155         lastans[k+sz[ch[x][0]]] = val[x]; 
156         print(ch[x][1],k+1+sz[ch[x][0]]);
157     }
158     int val[maxn];
159 
160 }spt;
161 int main(){ 
162     while(scanf("%d %d",&n,&m) != EOF)
163     {
164        if(n == -1)
165            break;
166        spt.init(n);
167        for(int i = 1;i <= m;i ++)
168        {
169            scanf("%s",str);
170            if(str[0] == C)
171            {
172               scanf("%d %d %d",&ta,&tb,&tc);
173               spt.cut(ta,tb,tc);
174            }else{
175               scanf("%d %d",&ta,&tb);
176               spt.flip(ta,tb);
177            }
178        }
179        spt.print(spt.root,0);
180        for(int i = 1;i <= n; i ++)
181          printf(i == 1?"%d":" %d",lastans[i]);
182        printf("\n");
183     }
184 return 0;
185 }
View Code

HDU 3487 Play with Chain


推荐阅读
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 本文详细介绍了如何通过多种编程语言(如PHP、JSP)实现网站与MySQL数据库的连接,包括创建数据库、表的基本操作,以及数据的读取和写入方法。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • 本文介绍如何在应用程序中使用文本输入框创建密码输入框,并通过设置掩码来隐藏用户输入的内容。我们将详细解释代码实现,并提供专业的补充说明。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 几何画板展示电场线与等势面的交互关系
    几何画板是一款功能强大的物理教学软件,具备丰富的绘图和度量工具。它不仅能够模拟物理实验过程,还能通过定量分析揭示物理现象背后的规律,尤其适用于难以在实际实验中展示的内容。本文将介绍如何使用几何画板演示电场线与等势面之间的关系。 ... [详细]
  • MySQL中枚举类型的所有可能值获取方法
    本文介绍了一种在MySQL数据库中查询枚举(ENUM)类型字段所有可能取值的方法,帮助开发者更好地理解和利用这一数据类型。 ... [详细]
author-avatar
小阳
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有