好题,错了不知道多少遍。这题其他几个操作都是比较经典的,多了一个最大子序列的。这时候对于当前的区间,最大子序列,可能使左区间的最值,可能是右区间的最值,也可能是整个区间的。所以维护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]);
}
}
}
}

最新文章

  1. 用Windows PowerShell 控制管理 Microsoft Office 365
  2. jquery 原理
  3. PS5穿越云层3D文字
  4. Ubuntu下搭建本地WordPress站点
  5. 在头文件声明全局变量和创建extern
  6. C#之自己定义的implicit和explicit转换
  7. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom
  8. 华为JAVA(面试问题及答案节)
  9. 设置Linux可以查看历史命令的执行时间
  10. vue-router源码学习(一)
  11. [ 10.4 ]CF每日一题系列—— 486C
  12. Adam优化算法
  13. logging模块知识点及应用小结
  14. 释放linux的buff/cache
  15. 来了解一下Redis的分布式锁
  16. Form Data 和 Request Payload 区别
  17. 服务器运行两个或两个以上的tomcat
  18. 微博(MicroBlog)
  19. JAVA使用并行流(ParallelStream)时要注意的一些问题
  20. MySQL中 IFNULL、NULLIF和ISNULL函数的用法

热门文章

  1. 【Android 我的博客APP】1.抓取博客首页文章列表内容——网页数据抓取
  2. lock与C#多线程
  3. Unity开发 资源准备
  4. java9-8 局部内部类
  5. android图片缩小和放大Matrix
  6. Android SQLite (五 ) 全面详解(三)
  7. 使用EXISTS语句注意点
  8. python socket发送魔法包网络唤醒开机.py
  9. webpack+react+redux+es6
  10. Oracle数据库,数字强制显示2位小数(转)