【BZOJ1251】序列終結者

1251: 序列終結者
Time Limit: 20 Sec Memory Limit: 162 MB
Submit: 3778 Solved: 1583
[Submit][Status][Discuss]
Description
網上有許多題,就是給定一個序列,要你支持幾種操作:A、B、C、D。一看另一道題,又是一個序列 要支持幾種操作:D、C、B、A。尤其是我們這裏的某人,出模擬試題,居然還出了一道這樣的,真是沒技術含量……這樣 我也出一道題,我出這一道的目的是爲了讓大家以後做這種題目有一個“庫”可以依靠,沒有什麼其他的意思。這道題目 就叫序列終結者吧。 【問題描述】 給定一個長度爲N的序列,每個序列的元素是一個整數(廢話)。要支持以下三種操作: 1. 將[L,R]這個區間內的所有數加上V。 2. 將[L,R]這個區間翻轉,比如1 2 3 4變成4 3 2 1。 3. 求[L,R]這個區間中的最大值。 最開始所有元素都是0。

Input
第一行兩個整數N,M。M爲操作個數。 以下M行,每行最多四個整數,依次爲K,L,R,V。K表示是第幾種操作,如果不是第1種操作則K後面只有兩個數。

Output
對於每個第3種操作,給出正確的回答。

Sample Input
4 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4

Sample Output
2
【數據範圍】
N<=50000,M<=100000。

題解:
維護區間翻轉和add的標記即可
注意有負數

//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ls t[x].ch[0]
#define rs t[x].ch[1]
#define pa t[x].fa
using namespace std;
typedef long long LL;
const int inf=(1<<30),N=100010;
int n,m,root;
inline int in()
{
    char ch=getchar();
    int f=1,tmp=0;
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}
    return tmp*f;
}
struct node
{
    int ch[2],cnt,val,fa,rev,col,mx,siz;
}t[N];
inline int gi(int x) {return t[pa].ch[1]==x;}
inline void upd(int x)
{
    t[x].siz=t[ls].siz+t[rs].siz+t[x].cnt;
    t[x].mx=max(max(t[ls].mx,t[rs].mx),t[x].val);
}
inline void pushdown(int x)
{
    if(t[x].rev)
    {
        swap(ls,rs);
        if(ls) t[ls].rev^=1;
        if(rs) t[rs].rev^=1;
        t[x].rev=0;
    }
    if(t[x].col)
    {
        int w=t[x].col;
        if(ls) t[ls].col+=w,t[ls].val+=w,t[ls].mx+=w;
        if(rs) t[rs].col+=w,t[rs].val+=w,t[rs].mx+=w;
        t[x].col=0;
    }
}

int build(int l,int r)
{
    if(l>r) return 0;
    int x=(l+r)>>1;
    ls=build(l,x-1),rs=build(x+1,r);
    t[ls].fa=t[rs].fa=x;
    t[x].siz=t[x].cnt=1;
    upd(x);
    return x;
}

void rot(int x)
{
    int f=pa,g=t[f].fa,o=gi(x);
    t[f].ch[o]=t[x].ch[!o];
    t[t[x].ch[!o]].fa=f;
    t[x].ch[!o]=f;
    t[f].fa=x;
    t[x].fa=g;
    if(g) t[g].ch[t[g].ch[1]==f]=x;
    upd(f),upd(x);////////
}

void splay(int x,int tar)
{
    for(;pa!=tar;rot(x))
        if(t[pa].fa!=tar)
            rot((gi(x)==gi(pa)?pa:x));
    if(!tar) root=x;
}

int kth(int k)
{
    int x=root;
    while(1)
    {
        pushdown(x);
        if(k<=t[ls].siz&&ls) x=ls;
        else
        {
            int tmp=t[ls].siz+t[x].cnt;
            if(k<=tmp) return x;
            k-=tmp,x=rs;
        }
    }
}

void rev(int l,int r)
{
    splay(kth(l),0);
    int x=kth(r+2);
    splay(x,root);
    t[ls].rev^=1;   
}

void add(int l,int r,int v)
{
    splay(kth(l),0);
    int x=kth(r+2);
    splay(x,root);
    t[ls].col+=v;
    t[ls].mx+=v;
    t[ls].val+=v;
}

int gmax(int l,int r)
{
    splay(kth(l),0);
    int x=kth(r+2);
    splay(x,root);
    printf("%d\n",t[ls].mx);    
}

void print(int x)
{
    if(!x) return;
    pushdown(x);
    if(ls) print(ls);
    if(x!=1&&x!=n+2) printf("%d ",t[x].val);
    if(rs) print(rs);
}

int main()
{
    n=in(),m=in();
    t[0].mx=-inf;////
    root=build(1,n+2);
    for(int i=1,op,l,r,x;i<=m;i++)
    {
        op=in(),l=in(),r=in();
        switch(op)
        {
            case 1:x=in();add(l,r,x);break;
            case 2:rev(l,r);break;
            case 3:gmax(l,r);break;
        }
//      print(root);
//      cout<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章