NOI2005维修数列 splay
好题,错了不知道多少遍。这题其他几个操作都是比较经典的,多了一个最大子序列的。这时候对于当前的区间,最大子序列,可能使左区间的最值,可能是右区间的最值,也可能是整个区间的。所以维护lx[],rx[],mx[]。lx[rt] = max(lx[l],sum[l]+key[rt]+max(0,lx[r]));当前节点的左区间最值可能是左孩子的最值,也可能是左孩子加右孩子的一部分。
rx[]类似。mx[rt] = max(0,rx[l])+key[rt]+max(0,lx[r]);mx[rt] = max(mx[rt],max(mx[l],mx[r]));
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 99999999
#define ll __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define key_value ch[ch[root][1]][0]
using namespace std;
const int MAXN = ;
int pre[MAXN],ch[MAXN][],siz[MAXN],sum[MAXN],rev[MAXN],lazy[MAXN],key[MAXN],root,tot1;
int n,a[MAXN],cnt,s[MAXN],tot2;
int mx[MAXN],lx[MAXN],rx[MAXN];
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d key=%2d lazy=%2d rev=%2d siz[0]=%2d\n",x,ch[x][],ch[x][],pre[x],siz[x],key[x],lazy[x],rev[x],siz[]);
Treavel(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
void Newnode(int &rt,int pa,int k)
{
if(tot2){
rt = s[tot2--];
}
else {
rt = ++tot1;
}
pre[rt] = pa;
sum[rt] = k;
rev[rt] = ;
lazy[rt] = ;
siz[rt] = ;
key[rt] = lx[rt] = rx[rt] = mx[rt] = k;
ch[rt][] = ch[rt][] = ;
}
void pushup(int rt)
{
int l = ch[rt][];
int r = ch[rt][];
siz[rt] = siz[l] + siz[r] + ;
sum[rt] = sum[l] + sum[r] + key[rt];
lx[rt] = max(lx[l],sum[l]+key[rt]+max(,lx[r]));
rx[rt] = max(rx[r],sum[r]+key[rt]+max(,rx[l]));
mx[rt] = max(,rx[l]) + key[rt] + max(,lx[r]);
mx[rt] = max(mx[rt],max(mx[l],mx[r]));
}
void updata_same(int rt,int k)
{
if(!rt)
return ;
key[rt] = k;
sum[rt] = k*siz[rt];
lx[rt] = rx[rt] = mx[rt] = max(k,k*siz[rt]);
lazy[rt] = ;
}
void updata_rev(int rt)
{
rev[rt] ^= ;
swap(ch[rt][],ch[rt][]);
swap(lx[rt],rx[rt]);
}
void pushdown(int rt)
{
if(lazy[rt]){
updata_same(ch[rt][],key[rt]);
updata_same(ch[rt][],key[rt]);
lazy[rt] = ;
}
if(rev[rt]){
updata_rev(ch[rt][]);
updata_rev(ch[rt][]);
rev[rt] = ;
}
}
void build(int &rt,int l,int r,int pa)
{
if(l > r){
return ;
}
int m = (l+r)/;
Newnode(rt,pa,a[m]);
build(ch[rt][],l,m-,rt);
build(ch[rt][],m+,r,rt);
pushup(rt);
}
void Init()
{
root = tot1 = tot2 = ;
pre[root] = sum[root] = key[root] = lazy[root] = rev[root] = siz[root] = ;
lx[root] = rx[root] = mx[root] = -INF;//重要
ch[root][] = ch[root][] = ;
Newnode(root,,);
Newnode(ch[root][],root,);
build(key_value,,n,ch[root][]);
pushup(ch[root][]);
pushup(root);
}
int Get_kth(int rt,int k)
{
pushdown(rt);
int t = siz[ch[rt][]] + ;
if(t == k){
return rt;
}
else if(t > k){
return Get_kth(ch[rt][],k);
}
else {
return Get_kth(ch[rt][],k-t);
}
}
void Rotate(int rt,int kind)
{
int y = pre[rt];
pushdown(y);
pushdown(rt);
ch[y][!kind] = ch[rt][kind];
pre[ch[rt][kind]] = y;
if(pre[y]){
ch[pre[y]][ch[pre[y]][]==y] = rt;
}
pre[rt] = pre[y];
ch[rt][kind] = y;
pre[y] = rt;
pushup(y);
}
void splay(int rt,int goal)
{
pushdown(rt);
while(pre[rt] != goal)
{
if(pre[pre[rt]] == goal){
pushdown(pre[rt]);
pushdown(rt);
Rotate(rt,ch[pre[rt]][]==rt);
}
else {
pushdown(pre[pre[rt]]);
pushdown(pre[rt]);
pushdown(rt);
int y = pre[rt];
int kind = ch[pre[y]][]==y;
if(ch[y][kind] == rt){
Rotate(rt,!kind);
Rotate(rt,kind);
}
else {
Rotate(y,kind);
Rotate(rt,kind);
}
}
}
if(goal == )
root = rt;
pushup(rt);
}
void erase(int rt)
{
if(!rt)
return;
s[++tot2] = rt;
erase(ch[rt][]);
erase(ch[rt][]);
}
int main()
{
int m,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=; i<=n; i++){
scanf("%d",&a[i]);
}
Init();
char c[];
while(m--)
{
scanf("%s",c);
if(c[] == 'I'){
int x,y;
scanf("%d%d",&x,&y);
for(i=; i<=y; i++){
scanf("%d",&a[i]);
}
splay(Get_kth(root,x+),);
splay(Get_kth(root,x+),root);
build(key_value,,y,ch[root][]);
pushup(ch[root][]);
pushup(root);
}
else if(c[] == 'D'){
int x,y;
scanf("%d%d",&x,&y);
splay(Get_kth(root,x),);
splay(Get_kth(root,x+y+),root);
erase(key_value);
pre[key_value] = ;
key_value = ;
pushup(ch[root][]);
pushup(root);
}
else if(c[] == 'M' && c[] == 'K'){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
splay(Get_kth(root,x),);
splay(Get_kth(root,x+y+),root);
updata_same(key_value,z);
pushup(ch[root][]);
pushup(root);
}
else if(c[] == 'R'){
int x,y;
scanf("%d%d",&x,&y);
splay(Get_kth(root,x),);
splay(Get_kth(root,x+y+),root);
updata_rev(key_value);
pushup(ch[root][]);
pushup(root);
}
else if(c[] == 'G'){
int x,y;
scanf("%d%d",&x,&y); splay(Get_kth(root,x),);
splay(Get_kth(root,x+y+),root);
printf("%d\n",sum[key_value]);
}
else{
splay(Get_kth(root,),);
splay(Get_kth(root,siz[root]),root);
printf("%d\n",mx[key_value]);
}
}
}
}
NOI2005维修数列 splay的更多相关文章
- bzoj 1500: [NOI2005]维修数列 splay
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 6556 Solved: 1963[Submit][Status ...
- BZOJ 1500: [NOI2005]维修数列 (splay tree)
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 4229 Solved: 1283[Submit][Status ...
- 【BZOJ1500】[NOI2005]维修数列 Splay
[BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行 ...
- BZOJ1500: [NOI2005]维修数列[splay ***]
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 12278 Solved: 3880[Submit][Statu ...
- [NOI2005]维修数列 Splay tree 区间反转,修改,求和,求最值
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 Description Input 输入文件的第1行包含两个数N和M,N表示初始时数 ...
- [bzoj1500][NOI2005]维修数列——splay
题目 题解 这道题可以说是数列问题的大BOSS,也算是这一周来学习splay等数据结构的一个总结. 我们一个一个地看这些操作. 对于操作1,我们首先建一棵子树,直接接上原树即可. 对于操作2,我们找到 ...
- BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】
以前写过这道题了,但我把以前的内容删掉了,因为现在感觉没法看 重写! 题意: 维护一个数列,支持插入一段数,删除一段数,修改一段数,翻转一段数,查询区间和,区间最大子序列 splay序列操作裸题 需要 ...
- [bzoj1500][NOI2005 维修数列] (splay区间操作)
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目. 第2行包含N个数字,描述初始时的数列. 以下M行,每 ...
- BZOJ1500 [NOI2005]维修数列(Splay tree)
[Submit][Status][Discuss] Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Inp ...
- bzoj1500: [NOI2005]维修数列 (Splay+变态题)
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 11353 Solved: 3553 [Submit][Status][Discuss] Descrip ...
随机推荐
- 《MapReduce: Simplified Data Processing on Large Cluster 》翻译
Abstract MapReduce是一种编程模型和一种用来处理和产生大数据集的相关实现.用户定义map函数来处理key/value键值对来产生一系列的中间的key/value键值对.还要定义一个re ...
- 首个攻击该Mac OS系统的恶意软件——KeRanger
首个攻击该Mac OS系统的恶意软件——KeRanger 曾几何时,苹果操作系统一度被人认为是最安全的操作系统.然而近几年,针对苹果系统的攻击日益增多,影响范围也越来越大.无独有偶,近日,苹果Mac ...
- 解密TDE加密数据库
1 找到加密的数据库,new query 2 执行sql 语句:ALTER DATABASE database_name SET ENCRYPTION OFF; DROP DATABASE ENCR ...
- UVA 12382 Grid of Lamps --贪心+优先队列
题意:给出每行每列至少有的灯泡数,问最少有的灯泡数. 解法:要使灯泡数尽量小,说明要使交叉点尽量多,这样即抵了行,又抵了列,为最优的.所以可以用行来消去列,也可以用列来消去行,我这里是列来消去行.首先 ...
- SGU 180 Inversions
题意:求逆序数对数量. 思路一:暴力,O(N^2),超时. 思路二:虽然Ai很大,但是n比较小,可以离散化,得到每个Ai排序后的位置Wi,然后按照输入的顺序,每个Ai对答案的贡献是Wi-Sum(Wi- ...
- linux之间进程通信
进程间通信方式: 同主机进程间数据交换机制: pipe(无名管道) / fifo(有名管道)/ message queue(消息队列)和共享内存. 必备基础: f ...
- 第22章 DLL注入和API拦截(2)
22.4 使用远程线程来注入DLL 22.4.1 概述 (1)远程线程注入是指一个进程在另一个进程中创建线程,然后载入我们编写的DLL,并执行该DLL代码的技术.其基本思路是通过CreateRemot ...
- 测试杂感:Bug Bash
缺陷大扫除(Bug Bash)是一项短期的全员测试活动.在微软,许多开发团队会在里程碑(milestone)的末期执行缺陷大扫除.程序员.测试员.程序经理.内部用户.市场人员在1~3天的时间窗口中,运 ...
- C# Thread.Join()用法的理解 转
指在一线程里面调用另一线程join方法时,表示将本线程阻塞直至另一线程终止时再执行 比如 1using System; 2 3namespace TestThreadJoin 4{ 5 class P ...
- 超详细图解:自己架设NuGet服务器
原文:http://diaosbook.com/Post/2012/12/15/setup-private-nuget-server NuGet 是.NET程序员熟知的给.NET项目自动配置安装lib ...