【平衡树】[NOI2005]维修数列

Description

请写一个程序,要求维护一个数列,支持以下 6 种操作:

请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格

Input

输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目。

第2行包含N个数字,描述初始时的数列。

以下M行,每行一条命令,格式参见问题描述中的表格。

任何时刻数列中最多含有500 000个数,数列中任何一个数字均在[-1 000, 1 000]内。

插入的数字总数不超过4 000 000个,输入文件大小不超过20MBytes。

Output

对于输入数据中的GET-SUM和MAX-SUM操作,向输出文件依次打印结果,每个答案(数字)占一行。

Sample Input

9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM

Sample Output

-1
10
1
10

HINT

内存限制: 256 MB

时间限制: 1000 ms

Solution 

https://www.luogu.org/problemnew/solution/P2042

 难点

  • 内存回收
  • treap O(N)区间建树
  • lmax rmax maxx数组 初始值  答案必须至少选一个数 
  • 注意判断儿子有无再进行操作
  • merge 先pushdown     因为pushdown改变了左右儿子 不能因为数空了就不pushdown了
  • 翻转标记 传递到的时候交换左右儿子 而不是pushdown的时候  因为lmax和rmax是分左右的
  •  

treap O(N)区间建树

https://www.cnblogs.com/BCOI/p/9090383.html

https://www.cnblogs.com/BCOI/p/9072444.html

https://blog.csdn.net/qq_36056315/article/details/79845193

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=500000+15,inf=1047483647;
int root,num,ch[N][2],siz[N],rnd[N],val[N]  ,sum[N],          lazy[N],
                                             lmax[N],rmax[N],maxx[N]; bool rev[N];
queue<int> q;
inline int make(int v)
 {int x;
  if(!q.empty()) x=q.front(),q.pop();	
  else x=++num;
  val[x]=sum[x]=maxx[x]=v; rnd[x]=rand(); siz[x]=1; 
  ch[x][0]=ch[x][1]=0; rev[x]=0;
  lmax[x]=rmax[x]=max(v,0);
  lazy[x]=inf;
  return x;
 }
void dfs(int x)
 {if(!x) return;
  dfs(ch[x][0]);
  q.push(x);
  dfs(ch[x][1]);
 } 
inline void pushup(int x) 
 {if(!x) return;
  siz[x]=1     +  siz[ch[x][0]]   +     siz[ch[x][1]];
  sum[x]=val[x]+  sum[ch[x][0]]   +     sum[ch[x][1]];  
  maxx[x]=max(   rmax[ch[x][0]]+val[x]+lmax[ch[x][1]]  ,val[x]);
  
  if(ch[x][0]) maxx[x]=max(maxx[x],maxx[ch[x][0]]);
  if(ch[x][1]) maxx[x]=max(maxx[x],maxx[ch[x][1]]);
  
  lmax[x]=max(max(sum[ch[x][0]]+val[x]+lmax[ch[x][1]],lmax[ch[x][0]]),0);
  rmax[x]=max(max(sum[ch[x][1]]+val[x]+rmax[ch[x][0]],rmax[ch[x][1]]),0);
 }
inline void fan(int x)
 {rev[x]^=1; swap(ch[x][0],ch[x][1]); swap(lmax[x],rmax[x]);
 }
inline void pass(int x,int v)
 {rev[x]=0;
  lazy[x]=val[x]=v; sum[x]=siz[x]*v;
  lmax[x]=rmax[x]=max(0,sum[x]); 
  maxx[x]=max(val[x],sum[x]);
 } 
inline void pushdown(int x)
 {if(!x) return;
  if(lazy[x]!=inf) 
   {if(ch[x][0]) pass(ch[x][0],lazy[x]);
    if(ch[x][1]) pass(ch[x][1],lazy[x]);
    rev[x]=0;lazy[x]=inf;
   }
  if(rev[x])
   {rev[x]=0;
    if(ch[x][0]) fan(ch[x][0]);
    if(ch[x][1]) fan(ch[x][1]);
   } 
 }
void split(int now,int k,int &x,int &y)
 {if(!now) {x=y=0; return;}
  
  pushdown(now);
  if(k<=siz[ ch[now][0] ])
    {y=now; split(ch[now][0],k,x,ch[now][0]);
    }
  else 
    {x=now; split(ch[now][1],k-siz[ch[now][0]]-1,ch[now][1],y);
    } 
  pushup(now);	 
 }
int merge(int x,int y)
 {pushdown(x); pushdown(y);
 
  if(!x||!y) return x+y;
  if(rnd[x]<rnd[y])
          {ch[x][1]=merge(ch[x][1],y);
           pushup(x); return x;
          }
  else    {ch[y][0]=merge(x,ch[y][0]);
           pushup(y); return y;
          }        
 } 
inline int read()
{   int k=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9')k=(k<<3)+(k<<1)+ch-'0',ch=getchar();
    return k*f;
} 
int stk[N],top;
int build(int len)
{   top=0;
    for(int i=1;i<=len;i++)
	{   int temp=make(read()),last=0;
        while(top && rnd[stk[top]] > rnd[temp])
            pushup(stk[top]),last=stk[top],stk[top--]=0;
        if(top) ch[stk[top]][1]=temp;
        ch[temp][0]=last, stk[++top]=temp;
    }
    while(top) pushup(stk[top--]); return stk[1];
} 
int main()
 {int n,m,x,y,z,k,tot; 	char op[15];
  n=read(); m=read();	
  root=build(n);
  
  while(m--)	
   {scanf("%s",op); 
   	if(op[0]=='I')
   	  {k=read(); tot=read();
   	  
   	   split(root,k,x,z);
   	   y=build(tot);
   	   
   	   root=merge(merge(x,y),z);
   	  }
   	  
   	else if(op[0]=='D')
   	  {k=read(); tot=read();
   	  
	   split(root,k-1,x,y);
	   split(y,tot,y,z);
	   
	   root=merge(x,z); 
	   
	   dfs(y);
   	  }
   	  
	else if(op[0]=='M' && op[2]=='K')
	  {k=read(); tot=read(); int c=read();
	  
	   split(root,k-1,x,y);
	   split(y,tot,y,z);
	   
	   pass(y,c);
	   
	   root=merge(merge(x,y),z);
	  }
	else if(op[0]=='R')  
	  {k=read(); tot=read();
	  
	   split(root,k-1,x,y);
	   split(y,tot,y,z);
	   
	   fan(y);
	   
	   root=merge(merge(x,y),z);
	  } 
   	else if(op[0]=='G')
	  {k=read(); tot=read();
	  
	   split(root,k-1,x,y);
	   split(y,tot,y,z);
	   
	   printf("%d\n",sum[y]);
	   
	   root=merge(merge(x,y),z);
	  }   
	else if(op[0]=='M' && op[2]=='X')
	   {printf("%d\n",maxx[root]);
	   }       
   }	
return 0;
 }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值