题意:维护一个文本编辑,并且查询最大前缀和。

写了splay,wa了13次

过了之后觉着特傻逼。发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和。

哎,傻逼,太弱了,还是没仔细分析题目的特殊性质。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
#include <iomanip>
using namespace std; #define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define PI acos(-1.0)
#define sqr(x) ((x)*(x)) #define inf 0x3f3f3f3f
#define keyTree (ch[ ch[root][1] ][0]) const int maxn = ; struct SplayTree {
int sz[maxn];
int ch[maxn][];
int pre[maxn];
int root , top1 , top2;
//int ss[maxn] , que[maxn]; void Rotate(int x,int f) {
int y = pre[x];
ch[y][!f] = ch[x][f];
pre[ ch[x][f] ] = y;
pre[x] = pre[y];
if(pre[x]) ch[ pre[y] ][ ch[pre[y]][] == y ] = x;
ch[x][f] = y;
pre[y] = x;
push_up(y);
}
void Splay(int x,int goal) {
while(pre[x] != goal) {
if(pre[pre[x]] == goal) {
Rotate(x , ch[pre[x]][] == x);
} else {
int y = pre[x] , z = pre[y];
int f = (ch[z][] == y);
if(ch[y][f] == x) {
Rotate(x , !f) , Rotate(x , f);
} else {
Rotate(y , f) , Rotate(x , f);
}
}
}
push_up(x);
if(goal == ) root = x;
}
void RotateTo(int k,int goal) {//把第k位的数转到goal下边
int x = root;
while(sz[ ch[x][] ] != k) {
// printf("x = %d k = %d sz[x] = %d\n",x,k,sz[x]);
if(k < sz[ ch[x][] ]) {
x = ch[x][];
} else {
k -= (sz[ ch[x][] ] + );
x = ch[x][];
}
}
Splay(x,goal);
} //以上一般不修改//////////////////////////////////////////////////////////////////////////////
void debug() {
printf("%d\n",root);
Treaval(root);
}
void Treaval(int x) {
if(x) {
Treaval(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d\n",x,ch[x][],ch[x][],pre[x],sz[x],val[x]);
Treaval(ch[x][]);
}
}
//以上Debug
//以下是题目的特定函数:
void NewNode(int &x,int c) {
x = ++top1;
ch[x][] = ch[x][] = pre[x] = ;
sz[x] = ;
val[x] = c;
sum[x] = c;
pf[x] = c;
}
//把孩子状态更新上来
void push_up(int x) {
sz[x] = + sz[ ch[x][] ] + sz[ ch[x][] ];
sum[x] = val[x] + sum[ch[x][]] + sum[ch[x][]]; pf[x] = max(pf[ch[x][]] , sum[ch[x][]] + val[x] + pf[ch[x][]] );
pf[x] = max(pf[x] , sum[ch[x][]] + val[x]); return ;
}
/*初始化*/
void makeTree(int &x,int l,int r,int f) {
if(l > r) return ;
int m = (l + r)>>;
// NewNode(x , num[m]); /*num[m]权值改成题目所需的*/
makeTree(ch[x][] , l , m - , x);
makeTree(ch[x][] , m + , r , x);
pre[x] = f;
push_up(x);
}
void clear() {
cnt = ;
ch[][] = ch[][] = pre[] = sz[] = ;
val[] = sum[] = ;
pf[] = -inf;
root = top1 = ;
//为了方便处理边界,加两个边界顶点
NewNode(root , -inf);
NewNode(ch[root][] , );
pre[top1] = root;
sz[root] = ;
}
void init(int pos,int n) {
clear();
cnt = n;
RotateTo(pos , );
RotateTo(pos + , root);
// makeTree(keyTree , 1 , n , ch[root][1]);
push_up(ch[root][]);
push_up(root);
}
void insert(int k , int value) {
cnt ++ ;
RotateTo(k - , );
RotateTo(k , root);
NewNode(keyTree,value);
pre[keyTree] = ch[root][];
push_up(root);
}
void del(int k) {
RotateTo(k - , );
RotateTo(k + , root);
//printf("val = %d\n",val[keyTree]);
//printf("sz = %d\n",sz[keyTree]);
cnt -- ;
keyTree = ;
push_up(root);
// erase(keyTree);
}
int query(int k)
{
if(k > cnt) k = cnt;
RotateTo(,);
RotateTo(k + ,root);
//printf("sz = %d\n",sz[keyTree]);
return pf[keyTree];
}
int cnt ;
/*这是题目特定变量*/
int val[maxn];
int sum[maxn];
int pf[maxn];
} spt; int main()
{
char str[];
int Q;
while(scanf("%d",&Q)!=EOF)
{
spt.init(,);
int loc;
int x;
loc = ;
while(Q -- )
{
scanf("%s",str);
if(str[] == 'I')
{
scanf("%d",&x);
spt.insert(loc + ,x);
loc ++ ;
}
else if(str[] == 'Q')
{
scanf("%d",&x);
if(x > loc ) x = loc ;
int ans = spt.query(x);
printf("%d\n",ans);
}
else if(str[] == 'L')
{
if(loc > )
loc -- ;
}
else if(str[] == 'R')
{
if(loc < spt.cnt)
loc ++ ;
}
else if(str[] == 'D')
{
spt.del(loc);
loc -- ;
}
// spt.debug();
}
}
return ;
}

代码不忍直视

以后补上维护栈的代码

hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!的更多相关文章

  1. hdu4705 Y 简单树形DP 2013多校训练第十场 J题

    题意:求一棵树中不在一条链中的三个点的对数. 转化一下,用总对数减去在一条链上的三点对数即可. 考虑经过根节点,然后可能是不同的子树中各选一个:或者是子树中选一个,然后当前节点为根的子树以外的节点选一 ...

  2. HDU 4639 hehe 杭电2013多校联赛第四场1008题

    解题报告:题目的意思是输入一个字符串,并规定,里面的“hehe”可以用"wqnmlgb"来代替,也可以不代替,问输入的这个字符串在经过相关的代替之后可以有多少种不同的形态.先打一个 ...

  3. 牛客网多校训练第三场 C - Shuffle Cards(Splay / rope)

    链接: https://www.nowcoder.com/acm/contest/141/C 题意: 给出一个n个元素的序列(1,2,...,n)和m个操作(1≤n,m≤1e5),每个操作给出两个数p ...

  4. 2019年杭电多校第二场 1012题Longest Subarray(HDU6602+线段树)

    题目链接 传送门 题意 要你找一个最长的区间使得区间内每一个数出现次数都大于等于\(K\). 思路 我们通过固定右端点考虑每个左端点的情况. 首先对于每个位置,我们用线段树来维护它作为\(C\)种元素 ...

  5. 2019牛客暑期多校训练营(第一场)I Points Division(dp+线段树优化)

    给你n个点,第i个点在的位置为(xi,yi),有两个属性值(ai,bi).现在让你把这n个点划分为A和B两个部分,使得最后不存在i∈A和j∈B,使得xi>=xj且yi<=yj.然后对于所有 ...

  6. 【杂题总汇】HDU多校赛第十场 Videos

    [HDU2018多校赛第十场]Videos 最后一场比赛也结束了…… +HDU传送门+ ◇ 题目 <简要翻译> 有n个人以及m部电影,每个人都有一个快乐值.每场电影都有它的开始.结束时间和 ...

  7. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  8. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  9. HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)

    HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...

随机推荐

  1. Android在ExpandableListView控制的基本使用

    在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main. ...

  2. CodeBlocks暴力恢复默认设置

    昨天,我不知道怎么去CodeBlocks干净的界面使自己都不知道怎么走.然后找到默认设置恢复方法,找不到.然后,我用了一个恢复方法暴力,卸载重装,有一点须要注意.卸载后CodeBlocks的配置文件还 ...

  3. Git使用操作指南和GitHub

    本文记录Git的使用操作,把散落的记忆整理到一起.并介绍GitHub的使用. 使用Git代表着一种思想和境地,和SVN相比,不是技术上的差异有多么大,而是代表融入了一种新的生态环境.一种开放开源的心态 ...

  4. python中使用traceback来追踪异常

    test1.py中,当分母为0的时候,调用系统退出 #!/usr/bin/python import sys def division(a=1, b=1): if b==0: print 'b eq ...

  5. SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法

    原文:SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法 在new SqlBulkCopy对象的时候,设置一下SqlBulkCopyOptions选项即可,按位或运算 SqlBulkC ...

  6. 使用GDB在远程开发机上调试

    由于一些环境限制,很多学生很可能需要在开发机器上调试.但是,由于对计算机资源的开发限制.在本地的直接机的发展GDB环境配置问题已经成为,其实,我们可以利用这段时间GDB自带gdbserver工具将能够 ...

  7. Android - 用Fragments实现动态UI - 使用Android Support Library

    Android Support Library提供了一个带有API库的JAR文件来让你可以在使用最新的Android API的同时也也已在早期版本的Android上运行.例如,Support Libr ...

  8. Android UI - 实现广告Banner旋转木马效果

    Android UI - 实现广告Banner旋转木马效果 前言 本篇博客要分享的一个效果是实现广告Banner轮播效果,这个效果也比較常见,一些视频类应用就常常有,就拿360影视大全来举例吧: 用红 ...

  9. Android Studio 1.0 (稳定版) 完全攻略

    这篇博文中主要从以下几点进行叙述: 1.Android Studio安装与使用 2.Android Studio特性 3.Android Studio优点 Android Studio 安装与使用 A ...

  10. Lua 环境结构 --Linux

    curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz tar zxf lua-5.2.3.tar.gz cd lua-5.2.3 make linux ...