序列终结者

序列终结者

时间限制: 1 Sec 内存限制: 128 MB

题目描述
给定一个长度为N的序列,每个序列的元素是一个整数。
要支持以下三种操作:
1. 将[L,R]这个区间内的所有数加上V。
2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。
3. 求[L,R]这个区间中的最大值。
最开始所有元素都是0。

输入
第一行两个整数N,M。M为操作个数。 以下M行,每行最多四个整数,依次为K,L,R,V。K表示是第几种操作,如果不是第1种操作则K后面只有两个数。
n<=50000,m<=100000

输出
对于每个第3种操作,给出正确的回答。

样例输入
4 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4

样例输出
2

题解

splay练习题

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define inf 2100000000
using namespace std;
int n,m,null,root;
struct node{int num,fa,s[2],turn,maxn,sum,size;}t[50010];

class splay_tree
{
  void res(int x)
  {
    if(x==null)return;
    t[x].turn^=t[t[x].fa].turn;swap(t[x].s[0],t[x].s[1]);
  }
  void addup(int x,int v)
  {
    if(x==null)return;
    t[x].sum+=v;t[x].maxn+=v;t[x].num+=v;
  }
  void up(int x)
  {
    int lc=t[x].s[0],rc=t[x].s[1];
    t[x].size=t[lc].size+t[rc].size+1;
    t[x].maxn=max(t[x].num,max(t[lc].maxn,t[rc].maxn));
  }
  void down(int x)
  {
    int lc=t[x].s[0],rc=t[x].s[1];
    if(t[x].turn)res(lc),res(rc),t[x].turn=0;
    if(t[x].sum)addup(lc,t[x].sum),addup(rc,t[x].sum),t[x].sum=0;
  }
  void rotate(int x,int k)
  {
    int ff=t[t[t[x].fa].fa].s[0]==t[x].fa?0:1;
    t[t[x].fa].s[k]=t[x].s[k^1];
    t[t[x].s[k^1]].fa=t[x].fa;
    t[x].s[k^1]=t[x].fa;
    int temp=t[t[x].fa].fa;
    t[t[x].fa].fa=x;t[x].fa=temp;
    t[t[x].fa].s[ff]=x;
    up(t[x].s[k^1]);
  }
  public:
  void splay(int x,int pos)
  {
    int flag=0;
    while(!flag)
    {
      down(x);int fa=t[x].fa,fb=t[fa].fa;
      if(fa==pos||fb==pos)flag=1;
      if(fa==pos){rotate(x,t[fa].s[0]==x?0:1);continue;}
      if(x==t[fa].s[0]&&fa==t[fb].s[0]){rotate(x,0),rotate(x,0);continue;}
      if(x==t[fa].s[1]&&fa==t[fb].s[1]){rotate(x,1),rotate(x,1);continue;}
      rotate(x,t[fa].s[0]==x?0:1);rotate(x,t[fb].s[0]==x?0:1);
    }up(x);if(pos==root)root=x;
  }
  void build(int x,int l,int r)
  {
    int mid;t[x].s[0]=t[x].s[1]=null;
    if(l<x)mid=(l+x-1)>>1,t[mid].fa=x,t[x].s[0]=mid,build(mid,l,x-1);
    if(r>x)mid=(r+x+1)>>1,t[mid].fa=x,t[x].s[1]=mid,build(mid,x+1,r);
    up(x);
  }
  void select(int x,int des,int pos)
  {
    int cnt=des+1;
    while(cnt)
    {
      int temp=t[t[x].s[0]].size;down(x);
      if(cnt==temp+1)break;
      if(cnt<=temp)x=t[x].s[0];
      else x=t[x].s[1],cnt-=temp+1;
    }
    if(x!=pos)splay(x,pos);
  }
}T;

int main()
{
  int x,l,r,v;
  scanf("%d%d",&n,&m);
  null=n+2;root=n>>1;
  t[0].num=t[0].maxn=t[null].num=t[null].maxn=t[n+1].num=t[n+1].maxn=-inf;
  T.build(root,0,n+1);t[root].fa=50005;
  while(m--)
  {
    scanf("%d%d%d",&x,&l,&r);
    T.select(root,l-1,root);
    T.select(root,r+1,t[root].s[1]);
    int pos=t[t[root].s[1]].s[0];
    if(x==1)scanf("%d",&v),t[pos].sum+=v,t[pos].maxn+=v,t[pos].num+=v,T.splay(pos,root);
    if(x==2)t[pos].turn^=1,swap(t[pos].s[0],t[pos].s[1]),T.splay(pos,root);
    if(x==3)printf("%d\n",t[pos].maxn);
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值