热门标签 | 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


推荐阅读
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文探讨了如何通过最小生成树(MST)来计算严格次小生成树。在处理过程中,需特别注意所有边权重相等的情况,以避免错误。我们首先构建最小生成树,然后枚举每条非树边,检查其是否能形成更优的次小生成树。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 本文详细介绍了如何通过多种编程语言(如PHP、JSP)实现网站与MySQL数据库的连接,包括创建数据库、表的基本操作,以及数据的读取和写入方法。 ... [详细]
  • This document outlines the recommended naming conventions for HTML attributes in Fast Components, focusing on readability and consistency with existing standards. ... [详细]
  • Splay Tree 区间操作优化
    本文详细介绍了使用Splay Tree进行区间操作的实现方法,包括插入、删除、修改、翻转和求和等操作。通过这些操作,可以高效地处理动态序列问题,并且代码实现具有一定的挑战性,有助于编程能力的提升。 ... [详细]
  • 使用Vultr云服务器和Namesilo域名搭建个人网站
    本文详细介绍了如何通过Vultr云服务器和Namesilo域名搭建一个功能齐全的个人网站,包括购买、配置服务器以及绑定域名的具体步骤。文章还提供了详细的命令行操作指南,帮助读者顺利完成建站过程。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 火星商店问题:线段树分治与持久化Trie树的应用
    本题涉及编号为1至n的火星商店,每个商店有一个永久商品价值v。操作包括每天在指定商店增加一个新商品,以及查询某段时间内某些商店中所有商品(含永久商品)与给定密码值的最大异或结果。通过线段树分治和持久化Trie树来高效解决此问题。 ... [详细]
  • JavaScript中属性节点的类型及应用
    本文深入探讨了JavaScript中属性节点的不同类型及其在实际开发中的应用,帮助开发者更好地理解和处理HTML元素的属性。通过具体的案例和代码示例,我们将详细解析如何操作这些属性节点。 ... [详细]
  • 本文探讨了 Objective-C 中的一些重要语法特性,包括 goto 语句、块(block)的使用、访问修饰符以及属性管理等。通过实例代码和详细解释,帮助开发者更好地理解和应用这些特性。 ... [详细]
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社区 版权所有