BZOJ 4127 Abs 樹鏈剖分,bzoj4127

來源:互聯網
上載者:User

BZOJ 4127 Abs 樹鏈剖分,bzoj4127

題目大意:給定一棵樹,每個點有一個整數權值(可以是負數),要求支援兩種操作:
1.鏈上加
2.鏈上絕對值之和

由於加的數保證非負,因此一個負數變成一個正數最多有n次
樹鏈剖分,線上段樹中維護一下區間最大負數即可
不知道為何 寫了兩個線段樹就TLE 把兩個線段樹合并成一個就7s過了

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 100100using namespace std;struct Segtree{    Segtree *ls,*rs;    long long sum,mark;    int cnt;//區間中負數的個數    pair<int,int> max_pos;    void* operator new (size_t)    {        static Segtree mempool[M<<1],*C=mempool;        return C++;    }    void Add(int x,int y,long long val)    {        sum+=(y-x+1-2*cnt)*val;        mark+=val;        if(max_pos.first!=(signed)0xefefefef)            max_pos.first+=val;    }    void Push_Up(int x,int y)    {        sum=ls->sum+rs->sum;        cnt=ls->cnt+rs->cnt;        max_pos=max(ls->max_pos,rs->max_pos);    }    void Push_Down(int x,int y)    {        int mid=x+y>>1;        if(mark)        {            ls->Add(x,mid,mark);            rs->Add(mid+1,y,mark);            mark=0;        }    }    void Build_Tree(int x,int y,int a[])    {        int mid=x+y>>1;        if(x==y)        {            if(a[mid]<0)                max_pos=pair<int,int>(a[mid],mid),cnt=1;            else                max_pos=pair<int,int>(0xefefefef,0);            sum=abs(a[mid]);            return ;        }        (ls=new Segtree)->Build_Tree(x,mid,a);        (rs=new Segtree)->Build_Tree(mid+1,y,a);        Push_Up(x,y);    }    void Reverse(int x,int y,int pos)    {        int mid=x+y>>1;        if(x==y)        {            cnt=0;            sum=-sum;            max_pos=pair<int,int>(0xefefefef,0);            return ;        }        Push_Down(x,y);        if(pos<=mid)            ls->Reverse(x,mid,pos);        else            rs->Reverse(mid+1,y,pos);        Push_Up(x,y);    }    void Add(int x,int y,int l,int r,long long val)    {        int mid=x+y>>1;        if(x==l&&y==r)        {            Add(x,y,val);            return ;        }        Push_Down(x,y);        if(r<=mid)            ls->Add(x,mid,l,r,val);        else if(l>mid)            rs->Add(mid+1,y,l,r,val);        else            ls->Add(x,mid,l,mid,val) , rs->Add(mid+1,y,mid+1,r,val) ;        Push_Up(x,y);    }    long long Query(int x,int y,int l,int r)    {        int mid=x+y>>1;        if(x==l&&y==r)            return sum;        Push_Down(x,y);        if(r<=mid)            return ls->Query(x,mid,l,r);        if(l>mid)            return rs->Query(mid+1,y,l,r);        return ls->Query(x,mid,l,mid) + rs->Query(mid+1,y,mid+1,r) ;    }    pair<int,int> Get_Pos(int x,int y,int l,int r)    {        int mid=x+y>>1;        if(x==l&&y==r)            return max_pos;        Push_Down(x,y);        if(r<=mid)            return ls->Get_Pos(x,mid,l,r);        if(l>mid)            return rs->Get_Pos(mid+1,y,l,r);        return max( ls->Get_Pos(x,mid,l,mid) , rs->Get_Pos(mid+1,y,mid+1,r) );    }}*tree=new Segtree;struct abcd{    int to,next;}table[M<<1];int head[M],tot;int n,m,a[M];int fa[M],son[M],dpt[M],size[M];int pos[M],top[M],_a[M];void Add(int x,int y){    table[++tot].to=y;    table[tot].next=head[x];    head[x]=tot;}void DFS1(int x){    int i;    dpt[x]=dpt[fa[x]]+1;    size[x]=1;    for(i=head[x];i;i=table[i].next)        if(table[i].to!=fa[x])        {            fa[table[i].to]=x;            DFS1(table[i].to);            size[x]+=size[table[i].to];            if(size[table[i].to]>size[son[x]])                son[x]=table[i].to;        }}void DFS2(int x){    static int T;    int i;    _a[pos[x]=++T]=a[x];    if(son[fa[x]]==x)        top[x]=top[fa[x]];    else        top[x]=x;    if(son[x])        DFS2(son[x]);    for(i=head[x];i;i=table[i].next)        if(table[i].to!=fa[x]&&table[i].to!=son[x])            DFS2(table[i].to);}void Add(int x,int y,int z){    int fx=top[x],fy=top[y];    while(fx!=fy)    {        if(dpt[fx]<dpt[fy])            swap(fx,fy),swap(x,y);        while(1)        {            pair<int,int> temp=tree->Get_Pos(1,n,pos[fx],pos[x]);            if(temp.first+z>=0)                tree->Reverse(1,n,temp.second);            else                break;        }        tree->Add(1,n,pos[fx],pos[x],z);        x=fa[fx];fx=top[x];    }    if(dpt[x]<dpt[y])        swap(x,y);    while(1)    {        pair<int,int> temp=tree->Get_Pos(1,n,pos[y],pos[x]);        if(temp.first+z>=0)            tree->Reverse(1,n,temp.second);        else            break;    }    tree->Add(1,n,pos[y],pos[x],z);}long long Query(int x,int y){    int fx=top[x],fy=top[y];    long long re=0;    while(fx!=fy)    {        if(dpt[fx]<dpt[fy])            swap(fx,fy),swap(x,y);        re+=tree->Query(1,n,pos[fx],pos[x]);        x=fa[fx];fx=top[x];    }    if(dpt[x]<dpt[y])        swap(x,y);    return re+tree->Query(1,n,pos[y],pos[x]);}int main(){    int i,p,x,y,z;    cin>>n>>m;    for(i=1;i<=n;i++)        scanf("%d",&a[i]);    for(i=1;i<n;i++)    {        scanf("%d%d",&x,&y);        Add(x,y);Add(y,x);    }    DFS1(1);DFS2(1);    tree->Build_Tree(1,n,_a);    for(i=1;i<=m;i++)    {        scanf("%d%d%d",&p,&x,&y);        if(p==1)        {            scanf("%d",&z);            if(z) Add(x,y,z);        }        else        {            #ifdef PoPoQQQ                printf("%I64d\n",Query(x,y));            #else                printf("%lld\n",Query(x,y));            #endif        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.