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

【模板】文艺平衡树(Splay)区间翻转BZOJ3223

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,

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



N,M<&#61;100000

Sample Output4 3 2 1 5 Hint


Input

第一行为n,m n表示初始序列有n个数&#xff0c;这个序列依次是(1,2……n-1,n)  m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<&#61;l<&#61;r<&#61;n 

Output

 

输出一行n个数字&#xff0c;表示原始序列经过m次变换后的结果 

Sample Input5 3 1 3 1 3 1 4
Splay Tree &#xff0c;和上道题一样&#xff0c;我们的kth 返回的序列的第k个数&#xff1b;
然后中序遍历即可得到我们翻转后的序列&#xff1b;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#pragma GCC optimize(2)
//#include
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod &#61; 1e9 &#43; 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair pii;
#define pi acos(-1.0)
//const int N &#61; 1005;
#define REP(i,n) for(int i&#61;0;i<(n);i&#43;&#43;)inline ll rd() {ll x &#61; 0;char c &#61; getchar();bool f &#61; false;while (!isdigit(c)) {if (c &#61;&#61; &#39;-&#39;) f &#61; true;c &#61; getchar();}while (isdigit(c)) {x &#61; (x <<1) &#43; (x <<3) &#43; (c ^ 48);c &#61; getchar();}return f ? -x : x;
}ll gcd(ll a, ll b) {return b &#61;&#61; 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x &#61; 1; y &#61; 0; return a;}ans &#61; exgcd(b, a%b, x, y);ll t &#61; x; x &#61; y; y &#61; t - a / b * y;return ans;
}
*/ll qpow(ll a, ll b, ll c) {ll ans &#61; 1;a &#61; a % c;while (b) {if (b % 2)ans &#61; ans * a%c;b /&#61; 2; a &#61; a * a%c;}return ans;
}struct splay {int fa, ch[2], size;int lazy, rev, maxx, value;
}Sp[maxn];int n, m, root, a[maxn];
void pushup(int rt) {Sp[rt].size &#61; Sp[Sp[rt].ch[0]].size &#43; Sp[Sp[rt].ch[1]].size &#43; 1;Sp[rt].maxx &#61; max(Sp[rt].value, max(Sp[Sp[rt].ch[0]].maxx, Sp[Sp[rt].ch[1]].maxx));
}void pushdown(int rt) {if (Sp[rt].lazy) {if (Sp[rt].ch[0]) {Sp[Sp[rt].ch[0]].lazy &#43;&#61; Sp[rt].lazy;Sp[Sp[rt].ch[0]].maxx &#43;&#61; Sp[rt].lazy;Sp[Sp[rt].ch[0]].value &#43;&#61; Sp[rt].lazy;}if (Sp[rt].ch[1]) {Sp[Sp[rt].ch[1]].lazy &#43;&#61; Sp[rt].lazy;Sp[Sp[rt].ch[1]].maxx &#43;&#61; Sp[rt].lazy;Sp[Sp[rt].ch[1]].value &#43;&#61; Sp[rt].lazy;}Sp[rt].lazy &#61; 0;}if (Sp[rt].rev) {if (Sp[rt].ch[0]) {Sp[Sp[rt].ch[0]].rev ^&#61; 1;swap(Sp[Sp[rt].ch[0]].ch[0], Sp[Sp[rt].ch[0]].ch[1]);}if (Sp[rt].ch[1]) {Sp[Sp[rt].ch[1]].rev ^&#61; 1;swap(Sp[Sp[rt].ch[1]].ch[0], Sp[Sp[rt].ch[1]].ch[1]);}Sp[rt].rev &#61; 0;}
}int id(int x) {return Sp[Sp[x].fa].ch[1] &#61;&#61; x;
}
void link(int son, int fa, int k) {Sp[son].fa &#61; fa; Sp[fa].ch[k] &#61; son;
}void rotate(int x) {int y &#61; Sp[x].fa;int z &#61; Sp[y].fa;int yk &#61; id(x);int zk &#61; id(y);int b &#61; Sp[x].ch[yk ^ 1];link(b, y, yk); link(y, x, yk ^ 1);link(x, z, zk);pushup(y); pushup(x);
}void SPLAY(int x, int aim) {while (Sp[x].fa !&#61; aim) {int y &#61; Sp[x].fa;int z &#61; Sp[y].fa;if (z !&#61; aim)id(x) &#61;&#61; id(y) ? rotate(y) : rotate(x);rotate(x);}if (aim &#61;&#61; 0)root &#61; x;
}int kth(int k) {int now &#61; root;while (1) {pushdown(now);int left &#61; Sp[now].ch[0];if (Sp[left].size &#43; 1 &#61; k)now &#61; left;else return now;}
}int build(int l, int r, int fa) {if (l > r)return 0;if (l &#61;&#61; r) {Sp[l].fa &#61; fa; Sp[l].maxx &#61; Sp[l].value &#61; a[l];Sp[l].size &#61; 1; return l;}int mid &#61; (l &#43; r) >> 1;Sp[mid].ch[0] &#61; build(l, mid - 1, mid);Sp[mid].ch[1] &#61; build(mid &#43; 1, r, mid);Sp[mid].value &#61; a[mid];Sp[mid].fa &#61; fa;pushup(mid);return mid;
}int split(int l, int r) {l &#61; kth(l); r &#61; kth(r &#43; 2);SPLAY(l, 0); SPLAY(r, l);return Sp[Sp[root].ch[1]].ch[0];
}void upd(int l, int r, int v) {int now &#61; split(l, r);Sp[now].lazy &#43;&#61; v; Sp[now].maxx &#43;&#61; v; Sp[now].value &#43;&#61; v;pushup(Sp[root].ch[1]); pushup(root);
}void Reverse(int l, int r) {int now &#61; split(l, r);Sp[now].rev ^&#61; 1;swap(Sp[now].ch[0], Sp[now].ch[1]);pushup(Sp[root].ch[1]); pushup(root);
}
int query(int l, int r) {return Sp[split(l, r)].maxx;
}void dfs(int rt) {pushdown(rt);if (Sp[rt].ch[0])dfs(Sp[rt].ch[0]);if (Sp[rt].value !&#61; inf && Sp[rt].value !&#61; -inf) {cout <}int main()
{//ios::sync_with_stdio(0);rdint(n); rdint(m);for (int i &#61; 2; i <&#61; n &#43; 1; i&#43;&#43;)a[i] &#61; i - 1;a[1] &#61; -inf; a[n &#43; 2] &#61; inf;root &#61; build(1, n &#43; 2, 0);while (m--) {int l, r; rdint(l); rdint(r);Reverse(l, r);}dfs(root); cout <}

 


转:https://www.cnblogs.com/zxyqzy/p/10073020.html



推荐阅读
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本题探讨了一种字符串变换方法,旨在判断两个给定的字符串是否可以通过特定的字母替换和位置交换操作相互转换。核心在于找到这些变换中的不变量,从而确定转换的可能性。 ... [详细]
  • C++: 实现基于类的四面体体积计算
    本文介绍如何使用C++编程语言,通过定义类和方法来计算由四个三维坐标点构成的四面体体积。文中详细解释了四面体体积的数学公式,并提供了两种不同的实现方式。 ... [详细]
  • This document outlines the recommended naming conventions for HTML attributes in Fast Components, focusing on readability and consistency with existing standards. ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • 探索1000以内的完美数:因数和等于自身
    本文探讨了如何在1000以内找到所有完美数,即一个数的因数(不包括自身)之和等于该数本身。例如,6是一个完美数,因为1 + 2 + 3 = 6。通过编程实现这一过程,可以更好地理解完美数的特性。 ... [详细]
  • 本文探讨了《魔兽世界》中红蓝两方阵营在备战阶段的策略与实现方法,通过代码展示了双方如何根据资源和兵种特性进行战士生产。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 资源推荐 | TensorFlow官方中文教程助力英语非母语者学习
    来源:机器之心。本文详细介绍了TensorFlow官方提供的中文版教程和指南,帮助开发者更好地理解和应用这一强大的开源机器学习平台。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
author-avatar
平凡快乐的girl_819
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有