[NOI2005]维护数列_Splay
真的毫无算法可言,就是比谁的码力强罢了...
Code:
#include<stack>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 500000 + 200;
const int inf = -1223;
int ch[maxn][2], f[maxn], numv[maxn], maxv[maxn], lmax[maxn], rmax[maxn], sumv[maxn], tag[maxn], lazy[maxn],siz[maxn];
int root, arr[maxn];
stack <int> S;
struct Operation
{
inline void init(){ for(int i = maxn - 10; i >= 1; --i) S.push(i); }
inline int new_node(){ int u = S.top(); S.pop(); return u; }
inline void erase(int x)
{
ch[x][0] = ch[x][1] = f[x] = 0;
maxv[x] = numv[x] = lmax[x] = rmax[x] = sumv[x] = tag[x] = siz[x] = 0;
lazy[x] = inf;
}
inline void trash(int u)
{
if(!u) return ;
S.push(u);
trash(ch[u][0]);
trash(ch[u][1]);
erase(u);
}
inline void pushup(int o)
{
int ls = ch[o][0], rs = ch[o][1];
maxv[o] = numv[o];
sumv[o] = sumv[ls] + sumv[rs] + numv[o];
siz[o] = siz[ls] + siz[rs] + 1;
int sums = numv[o];
if(ls) maxv[o] = max(maxv[o], maxv[ls]), sums += rmax[ls];
if(rs) maxv[o] = max(maxv[o], maxv[rs]), sums += lmax[rs];
maxv[o] = max(maxv[o], sums);
lmax[o] = max(lmax[ls], sumv[ls] + numv[o] + lmax[rs]);
rmax[o] = max(rmax[rs], sumv[rs] + numv[o] + rmax[ls]);
}
inline void maintain(int o)
{
maxv[o] = max(numv[o], sumv[o]);
lmax[o] = rmax[o] = max(0, sumv[o]);
}
inline void pushdown(int x)
{
int ls = ch[x][0], rs = ch[x][1];
if(lazy[x] != inf)
{
if(ls){ numv[ls] = lazy[ls] = lazy[x], sumv[ls] = siz[ls] * numv[ls]; maintain(ls); }
if(rs){ numv[rs] = lazy[rs] = lazy[x], sumv[rs] = siz[rs] * numv[rs]; maintain(rs); }
lazy[x] = inf;
}
if(tag[x])
{
swap(lmax[ls], rmax[ls]); swap(lmax[rs], rmax[rs]);
swap(ch[ls][0], ch[ls][1]); swap(ch[rs][0], ch[rs][1]);
if(ls) tag[ls] ^= 1;
if(rs) tag[rs] ^= 1;
tag[x] = 0;
}
}
void build(int l,int r,int fa,int &o)
{
if(l > r) return ;
int mid = (l + r) >> 1;
o = new_node();
f[o] = fa, numv[o] = arr[mid], lazy[o] = inf;
build(l, mid - 1, o, ch[o][0]);
build(mid + 1, r, o, ch[o][1]);
pushup(o);
}
}T;
inline int get(int x){ return ch[f[x]][1] == x;}
inline void rotate(int x)
{
int old = f[x], oldf = f[old], which = get(x);
ch[old][which] = ch[x][which ^ 1], f[ch[old][which]] = old;
ch[x][which ^ 1] = old, f[old] = x, f[x] = oldf;
if(oldf)
ch[oldf][ch[oldf][1] == old] = x;
T.pushup(old); T.pushup(x);
}
inline void splay(int x,int &tar)
{
int a = f[tar];
for(int fa; (fa = f[x]) != a; rotate(x))
if(f[fa] != a) rotate(get(x) == get(fa) ? fa : x);
tar = x;
}
inline int findx(int x, int top)
{
int cur = top;
while(1)
{
T.pushdown(cur);
int ls = ch[cur][0];
if(x == siz[ls] + 1) return cur;
if(x <= siz[ls]) cur = ch[cur][0];
else x -= siz[ls] + 1, cur = ch[cur][1];
}
return 0;
}
inline void split(int &a, int nums, int &b)
{
if(nums == 0)
{
b = a, a = 0; return ;
}
if(nums == siz[a])
{
b = 0; return ;
}
int u = findx(nums, a);
splay(u, a);
b = ch[a][1], f[ch[a][1]] = 0 , ch[a][1] = 0;
T.pushup(a);
}
inline void merge(int &a, int &b)
{
if(!a)
{
a = b; return ;
}
int u = findx(siz[a], a);
splay(u, a);
ch[a][1] = b, f[ch[a][1]] = a;
T.pushup(a);
}
inline void Insert()
{
int pos, tot;
scanf("%d%d",&pos,&tot);
for(int i = 1;i <= tot; ++i) scanf("%d",&arr[i]);
int a = 0, b = 0;
split(root, pos, a);
T.build(1, tot, 0, b);
merge(root, b);
merge(root, a);
}
inline void Delete()
{
int pos, tot;
scanf("%d%d",&pos,&tot);
int a , b;
split(root, pos - 1, a);
split(a, tot, b);
T.trash(a);
merge(root, b);
}
inline void Make_Same()
{
int pos, tot, c, a = 0, b = 0;
scanf("%d%d%d",&pos,&tot,&c);
split(root, pos - 1, a), split(a, tot, b);
sumv[a] = siz[a] * c, lazy[a] = c, numv[a] = c;
T.maintain(a);
merge(root, a), merge(root, b);
}
inline void Reverse()
{
int pos, tot, a = 0, b = 0;
scanf("%d%d",&pos,&tot);
split(root, pos - 1, a); split(a, tot, b);
tag[a] ^= 1;
swap(lmax[a], rmax[a]), swap(ch[a][0], ch[a][1]);
merge(root, a), merge(root, b);
}
inline void Get_sum()
{
int pos, tot, a = 0, b = 0;
scanf("%d%d",&pos,&tot);
split(root, pos - 1, a), split(a, tot, b);
printf("%d\n",sumv[a]);
merge(root, a), merge(root, b);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
T.init();
for(int i = 1;i <= n; ++i) scanf("%d",&arr[i]);
T.build(1, n, 0, root);
for(int i = 1;i <= m; ++i)
{
char str[30];
scanf("%s",str);
if(str[0] == 'I') Insert();
if(str[0] == 'D') Delete();
if(str[2] == 'K') Make_Same();
if(str[0] == 'R') Reverse();
if(str[0] == 'G') Get_sum();
if(str[2] == 'X') printf("%d\n",maxv[root]);
}
return 0;
}
[NOI2005]维护数列_Splay的更多相关文章
- BZOJ_1500_[NOI2005]维修数列_splay
BZOJ_1500_[NOI2005]维修数列_splay 题意: 分析: 节点维护从左开始的最大连续子段和,从右开始的最大连续子段和,区间的最大连续子段和 插入:重新建一棵树,把pos旋到根,把po ...
- 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列
339. [NOI2005] 维护数列 时间限制:3 s 内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际 ...
- [NOI2005] 维护数列
[NOI2005] 维护数列 题目 传送门 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格) 操作编号 输入文件中的格式 说明 1 ...
- P2042 [NOI2005]维护数列 && Splay区间操作(四)
到这里 \(A\) 了这题, \(Splay\) 就能算入好门了吧. 今天是个特殊的日子, \(NOI\) 出成绩, 大佬 \(Cu\) 不敢相信这一切这么快, 一下子机房就只剩我和 \(zrs\) ...
- 洛谷 P2042 [NOI2005]维护数列-Splay(插入 删除 修改 翻转 求和 最大的子序列)
因为要讲座,随便写一下,等讲完有时间好好写一篇splay的博客. 先直接上题目然后贴代码,具体讲解都写代码里了. 参考的博客等的链接都贴代码里了,有空再好好写. P2042 [NOI2005]维护数列 ...
- P2042 [NOI2005]维护数列[splay或非旋treap·毒瘤题]
P2042 [NOI2005]维护数列 数列区间和,最大子列和(必须不为空),支持翻转.修改值.插入删除. 练码力的题,很毒瘤.个人因为太菜了,对splay极其生疏,犯了大量错误,在此记录,望以后一定 ...
- Luogu P2042 [NOI2005]维护数列(平衡树)
P2042 [NOI2005]维护数列 题意 题目描述 请写一个程序,要求维护一个数列,支持以下\(6\)种操作:(请注意,格式栏中的下划线'_'表示实际输入文件中的空格) 输入输出格式 输入格式: ...
- [NOI2005]维护数列(区间splay)
[NOI2005]维护数列(luogu) 打这玩意儿真是要了我的老命 Description 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际输入文 ...
- [转载]无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )
转自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182631.html 1500: [NOI2005]维修数列 Time Limit: 10 Sec Mem ...
随机推荐
- java异常处理的面试题
package test; public class Test { public static int method(int i) throws Exception { try { return 10 ...
- javascript--闭包与this
理解javascript中的闭包与this http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html http: ...
- mysql备份恢复中的常见错误
从A主机备份到B主机 mysqldump -uroot -p vw>vw.sql 现备份数据库文件,需要恢复到目标机B,B的数据库版本为5.5.23,A机器的mysql版本为5.0.2 ...
- cocos2d-x 3.0游戏实例学习笔记 《跑酷》 第六步--金币&岩石加入而且管理
说明:这里是借鉴:晓风残月前辈的博客,他是将泰然网的跑酷教程,用cocos2d-x 2.X 版本号重写的,眼下我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记 ...
- centos 80端口占用
netstat -lnp|grep 80 kill -9 1777 #杀掉编号为1777的进程(请根据实际情况输入)service httpd start #启动apache
- 前端总结·基础篇·CSS
前端总结·基础篇·CSS 1 常用重置+重置插件(Normalize.css,IE8+) * {box-sizing:border-box;} /* IE8+ */body {margin:0;} ...
- 【读书笔记】UEFI原理与编程(1)概述及开发环境的搭建
一.概述: 0.为什么会有这篇文章 说实在的,在2016初的时候,我就萌生了写一个操作系统的念头,但是这对于我一个菜鸟来说,犹如登天. 既然想了就去写,即使最后做不完,也不后悔. 抱着这样的念头,我开 ...
- 树莓派-基于aria2实现离线下载
安装aria2 aria2是linux下的一个下载工具,它支持http.bt种子.磁力链接三种方式下载 sudo apt-get install aria2 配置aria2 aria2支持命令参数,也 ...
- ViewData与ViewBag的使用区别
在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从 ...
- Android应用优化之代码检测优化
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...