BZOJ1251: 序列终结者(洛谷P4146)

292 篇文章 1 订阅
281 篇文章 1 订阅

Splay

BZOJ题目传送门
洛谷题目传送门

Splay维护数列入门题,然而太久没打都忘了,调了俩小时。

代码:

#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 50005
#define F inline
using namespace std;
struct tree{ bool f; int sz,fa,t,mx,w,to[2]; }t[N];
int n,m,rt,id[N];
F char readc(){
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    return l==r?EOF:*l++;
}
F int _read(){
    int x=0,f=1; char ch=readc();
    while (!isdigit(ch)) { if (ch=='-') f=-1; ch=readc(); }
    while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
    return x*f;
}
F void writec(int x){
    if (x<0) putchar('-'),x=-x;
    if (x>9) writec(x/10); putchar(x%10+48);
}
F void _write(int x){ writec(x),puts(""); }
F void pshp(int x){
    t[x].sz=t[t[x].to[0]].sz+t[t[x].to[1]].sz+1;
    t[x].mx=max(t[x].w,max(t[t[x].to[0]].mx,t[t[x].to[1]].mx));
}
F void pshd(int x){
    int &l=t[x].to[0],&r=t[x].to[1];
    if (t[x].t){
        int w=t[x].t; t[x].t=0;
        if (l) t[l].mx+=w,t[l].w+=w,t[l].t+=w;
        if (r) t[r].mx+=w,t[r].w+=w,t[r].t+=w;
    }
    if (t[x].f) t[x].f=0,t[l].f^=1,t[r].f^=1,swap(l,r);
}
void build(int l,int r,int fa){
    if (l>r) return; int mid=l+r>>1,x=id[mid];
    if (l==r) t[x].sz=1;
    build(l,mid-1,mid),build(mid+1,r,mid);
    t[x].fa=id[fa],pshp(x),t[id[fa]].to[mid>=fa]=x;
}
F void rtt(int x,int &w){
    int y=t[x].fa,z=t[y].fa,l=t[y].to[0]==x;
    if (y==w) w=x; else t[z].to[t[z].to[1]==y]=x;
    t[t[x].to[l]].fa=y,t[y].fa=x,t[x].fa=z;
    t[y].to[l^1]=t[x].to[l],t[x].to[l]=y;
    pshp(y),pshp(x);
}
F void splay(int x,int &w){
    for (int y=t[x].fa,z=t[y].fa;x!=w;rtt(x,w),y=t[x].fa,z=t[y].fa)
        if (y!=w) rtt((t[z].to[0]==y^t[y].to[0]==x?x:y),w);
}
int srch(int x,int w){
    pshd(x); int l=t[x].to[0];
    if (t[l].sz+1==w) return x;
    if (t[l].sz>=w) return srch(l,w);
    return srch(t[x].to[1],w-t[l].sz-1);
}
F int sprt(int l,int r){
    int x=srch(rt,l),y=srch(rt,r+2);
    splay(x,rt),splay(y,t[x].to[1]);
    return t[y].to[0];
}
F void mdfy(int l,int r,int w){
    int x=sprt(l,r); t[x].w+=w,t[x].t+=w,t[x].mx+=w;
}
int main(){
    n=_read(),m=_read(),t[0].mx=-2e9;
    for (int i=1;i<=n+2;i++) id[i]=i;
    build(1,n+2,0),rt=(n+3)>>1;
    while (m--){
        int f=_read(),l=_read(),r=_read(),x;
        if (f==1) x=_read(),mdfy(l,r,x);
        else if (f==2) t[sprt(l,r)].f^=1;
        else _write(t[sprt(l,r)].mx);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值