您的位置:首页 > 其它

3083: 遥远的国度|树链剖分

2016-03-08 15:45 330 查看
首先先树链剖分,这样同时可以查询子树和修改链。

对于询问x的子树最小权值的操作,有以下三种情况:

x=rootx=root,直接返回整个树的最小值

lca(x,root)=xlca(x,root)=x,也就是rootroot在原树中的xx的子树中,那么就查询x的子树中含有root的子树的补集

lca(x,root)<>xlca(x,root)<>x直接查询原树中的xx中的子树

这真是检验自己能有多么SB的一道细节题。。

如果调不出来可以借鉴一下我的搞了2H的4处sb错误。。。。

[code]#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#define N 200005
#define mx 1e9
using namespace std;
int sc()
{
    int i=0,f=1; char c=getchar();
    while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar();
    return i*f;
}
int head
,lst[N*2],nxt[N*2];
int fa
,S
,T
,size
,pos
,top
,deep
;
int mn[N*5],ll[N*5],lr[N*5],tag[N*5],v
,wh
;
int n,m,cnt,tot,root;
void insert(int x,int y)
{
    lst[++tot]=y;nxt[tot]=head[x];head[x]=tot;
    lst[++tot]=x;nxt[tot]=head[y];head[y]=tot;
}
void dfs(int x,int f)
{
    for(int i=head[x];i;i=nxt[i])
        if(lst[i]!=f)
        {
            fa[lst[i]]=x;
            deep[lst[i]]=deep[x]+1;
            dfs(lst[i],x);
            size[x]+=size[lst[i]];
        }
    size[x]++;
}
void dfs1(int x,int htp)
{
    top[x]=htp;wh[S[x]=++cnt]=x;
    int k=0;
    for(int i=head[x];i;i=nxt[i])
        if(lst[i]!=fa[x]&&size[lst[i]]>size[k])k=lst[i];
    if(!k){T[x]=cnt;return;}dfs1(k,htp);  //掉了T[x]=cnt; sb*1 
    for(int i=head[x];i;i=nxt[i])
        if(lst[i]!=fa[x]&&lst[i]!=k)
            dfs1(lst[i],lst[i]);
    T[x]=cnt;
}
int Lca(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(deep[top[x]]<deep[top[y]])swap(x,y);
        x=fa[top[x]];
    }
    return deep[x]<deep[y]?x:y;// deep[x]>deep[y]?x:y; sb*2
}
#define L x<<1
#define R x<<1|1
void build(int x,int l,int r)
{
    ll[x]=l;lr[x]=r;
    if(l==r)
    {
        mn[x]=v[wh[l]];
        return;
    }
    int mid=l+r>>1;
    build(L,l,mid);build(R,mid+1,r);
    mn[x]=min(mn[L],mn[R]);
}   
void push_down(int x)
{
    if(ll[x]==lr[x])return;
    if(tag[x])
    {
        tag[L]=mn[L]=tag[x];
        tag[R]=mn[R]=tag[x];
        tag[x]=0;
    }
}
void modify(int x,int l,int r,int v)
{
    if(ll[x]==l&&lr[x]==r){mn[x]=tag[x]=v;return;}
    push_down(x);
    int mid=ll[x]+lr[x]>>1;
    if(r<=mid) modify(L,l,r,v);
    else if(l>mid) modify(R,l,r,v);
    else modify(L,l,mid,v),modify(R,mid+1,r,v);
    mn[x]=min(mn[L],mn[R]);
}
int query(int x,int l,int r)
{
    push_down(x);
    if(ll[x]==l&&lr[x]==r)return mn[x];
    int mid=ll[x]+lr[x]>>1;
    if(r<=mid)return query(L,l,r);
    else if(l>mid)return query(R,l,r);
    else return min(query(L,l,mid),query(R,mid+1,r));
}
void change(int x,int y,int v)
{
    while(top[x]!=top[y])
    {
        if(deep[top[x]]<deep[top[y]])swap(x,y);
        modify(1,S[top[x]],S[x],v);
        x=fa[top[x]];
    }
    if(deep[x]>deep[y])swap(x,y);
    modify(1,S[x],S[y],v);
}
int solve(int x)
{
    int y=root;
    for(int i=head[x];i;i=nxt[i])
        if(lst[i]!=fa[x])//掉了 if(lst[i]!=fa[x])   sb*3
        if(S[lst[i]]<=S[root]&&T[lst[i]]>=T[root]){y=lst[i];break;}//  S[lst[i]]<S[root]&&T[lst[i]]>T[root] root可能是x的儿子 sb*4
    int ans=0x7FFFFFFF;
    if(S[y]>1)ans=min(ans,query(1,1,S[y]-1));
    if(T[y]<n)ans=min(ans,query(1,T[y]+1,n));
    return ans;
}
int main()
{
    n=sc(),m=sc();
    for(int i=1;i<n;i++)
    {
        int x=sc(),y=sc();
        insert(x,y);
    }
    for(int i=1;i<=n;i++)v[i]=sc();
    root=sc();
    dfs(1,0);dfs1(1,1);
    build(1,1,n); 
    while(m--)
    {
        int opt=sc();
        if(opt==1)root=sc();
        else if(opt==2)
        {
            int x=sc(),y=sc(),v=sc();
            change(x,y,v);
        }
        else if(opt==3)
        {
            int x=sc();
            if(x==root)printf("%d\n",mn[1]);
            else 
            {
                int lca=Lca(x,root);
                if(lca!=x) printf("%d\n",query(1,S[x],T[x]));
                else printf("%d\n",solve(x));
            }
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: