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]);
}
}
}
}
上一篇:win10系统下*切换桌面


下一篇:门店评级VS坏客户