[ZJOI2011]道馆之战

Description
口袋妖怪(又名神奇宝贝或宠物小精灵)红/蓝/绿宝石中的水系道馆需要经过三个冰地才能到达馆主的面前,冰地中的每一个冰块都只能经过一次。当一个冰地上的所有冰块都被经过之后,到下一个冰地的楼梯才会被打开。三个冰地分别如下:

当走出第三个冰地之后,就可以与馆主进行道馆战了。馆主发现这个难度太小,导致经常有挑战者能通过,为了加大难度,将道馆分成了n个房间,每个房间中是两个冰块或障碍,表示一列冰地。任意两个房间之间均有且仅有一条路径相连,即这n个房间构成一个树状结构。每个房间分成了A和B两个区域,每一区域都是一个薄冰块或者障碍物。每次只能移动到相邻房间的同一类区域(即若你现在在这个房间的A区域,那么你只能移动到相邻房间的A区域)或这个房间的另一区域。现在挑战者从房间u出发,馆主在房间v,那么挑战者只能朝接近馆主所在房间的方向过去。一开始挑战者可以在房间u的任意一个冰块区域内。如果挑战者踩过的冰块数达到了最大值(即没有一种方案踩过的冰块数更多了),那么当挑战者走到最后一个冰块上时,他会被瞬间传送到馆主面前与馆主进行道馆战。自从馆主修改规则后已经经过了m天,每天要么是有一个挑战者来进行挑战,要么就是馆主将某个房间进行了修改。对于每个来的挑战者,你需要计算出他若要和馆主进行战斗需要经过的冰块数。

Input
第一行包含两个正整数n和m。第2行到第n行,每行包含两个正整数x和y,表示一条连接房间x和房间y的边。房间编号为1…n。接下来n行,每行包含两个字符。第n + k行表示房间k的两个区域,第一个字符为A区域,第二个字符为B区域。其中“.”(ASCII码为46)表示是薄冰块,“#”(ASCII码为35)表示是障碍物。最后的m行,每行一个操作:
l C u s:将房间u里的两个区域修改为s。
l Q u v:询问挑战者在房间u,馆主在房间v时,挑战者能与馆主进行挑战需要踩的冰块数。如果房间u的两个区域都是障碍物,那么输出0。
N≤ 30 000
M ≤ 80 000

Output
包含若干行,每行一个整数。即对于输入中的每个询问,依次输出一个答案。

Sample Input

5 3
1 2
2 3
2 4
1 5
.#
..
#.
.#
..
Q 5 3
C 1 ##
Q 4 5

Sample Output

6
3

动态dp板子题?其实也就是树剖+线段树,不过线段树的记录信息就有点。。。

记录L[0/1]表示从左下/上出发能到达的最远距离,R[0/1]同理,并且还要记录一个T[0/1][0/1]

转移就枚举中间位置走上走下,然后展开的话就。。。

合并就看看代码吧。。。

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
    int x=0,f=1; char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline int read(){
    int x=0,f=1; char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
const int N=5e4,inf=1<<30;
int ID[N+10],dfn[N+10],v[N+10][2],n,m;
char str[5];
struct S1{
    #define ls (p<<1)
    #define rs (p<<1|1)
    struct node{
        int L[2],R[2],T[2][2];
        node(){
            memset(L,0,sizeof(L));
            memset(R,0,sizeof(R));
            memset(T,0,sizeof(T));
            T[0][1]=T[1][0]=-inf;
        }
        void Modify(int *V){
            if (V[0]&&V[1]) L[0]=R[0]=L[1]=R[1]=T[0][1]=T[1][0]=2,T[0][0]=T[1][1]=1;
            else    if (V[0])   L[0]=R[0]=T[0][0]=1,L[1]=R[1]=0,T[0][1]=T[1][0]=T[1][1]=-inf;
            else    if (V[1])   L[1]=R[1]=T[1][1]=1,L[0]=R[0]=0,T[0][0]=T[0][1]=T[1][0]=-inf;
            else    L[0]=L[1]=R[0]=R[1]=0,T[0][0]=T[0][1]=T[1][0]=T[1][1]=-inf;
        }
    }tree[(N<<2)+10];
    friend node operator +(const node &x,const node &y){
        node z;
        z.L[0]=max(x.L[0],max(x.T[0][0]+y.L[0],x.T[0][1]+y.L[1]));
        z.L[1]=max(x.L[1],max(x.T[1][0]+y.L[0],x.T[1][1]+y.L[1]));
        z.R[0]=max(y.R[0],max(y.T[0][0]+x.R[0],y.T[1][0]+x.R[1]));
        z.R[1]=max(y.R[1],max(y.T[0][1]+x.R[0],y.T[1][1]+x.R[1]));
        z.T[0][0]=max(-inf,max(x.T[0][0]+y.T[0][0],x.T[0][1]+y.T[1][0]));
        z.T[0][1]=max(-inf,max(x.T[0][0]+y.T[0][1],x.T[0][1]+y.T[1][1]));
        z.T[1][0]=max(-inf,max(x.T[1][0]+y.T[0][0],x.T[1][1]+y.T[1][0]));
        z.T[1][1]=max(-inf,max(x.T[1][0]+y.T[0][1],x.T[1][1]+y.T[1][1]));
        return z;
    }
    friend node operator -(const node &x){
        node y=x;
        y.L[0]=x.R[0],y.L[1]=x.R[1];
        y.R[0]=x.L[0],y.R[1]=x.L[1];
        y.T[0][0]=x.T[0][0],y.T[1][1]=x.T[1][1];
        y.T[0][1]=x.T[1][0],y.T[1][0]=x.T[0][1];
        return y;
    }
    void build(int p,int l,int r){
        if (l==r){
            tree[p].Modify(v[dfn[l]]);
            return;
        }
        int mid=(l+r)>>1;
        build(ls,l,mid),build(rs,mid+1,r);
        tree[p]=tree[ls]+tree[rs];
    }
    void Modify(int p,int l,int r,int x){
        if (l==r){
            tree[p].Modify(v[dfn[l]]);
            return;
        }
        int mid=(l+r)>>1;
        if (x<=mid) Modify(ls,l,mid,x);
        else    Modify(rs,mid+1,r,x);
        tree[p]=tree[ls]+tree[rs];
    }
    node Query(int p,int l,int r,int x,int y){
        if (x<=l&&r<=y) return tree[p];
        int mid=(l+r)>>1;
        if (y<=mid) return Query(ls,l,mid,x,y);
        if (x>mid)  return Query(rs,mid+1,r,x,y);
        return Query(ls,l,mid,x,y)+Query(rs,mid+1,r,x,y);
    }
    void Debug(int p,int l,int r){
        tree[p].print();
        if (l==r)   return;
        int mid=(l+r)>>1;
        Debug(ls,l,mid),Debug(rs,mid+1,r);
    }
    #undef ls
    #undef rs
}ST;//Segment Tree
struct S2{
    int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],tot,Time;
    int fa[N+10],deep[N+10],size[N+10],top[N+10],Rem[N+10];
    void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
    void insert(int x,int y){join(x,y),join(y,x);}
    void dfs(int x){
        deep[x]=deep[fa[x]]+1,size[x]=1;
        for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
            if (son==fa[x]) continue;
            fa[son]=x,dfs(son);
            size[x]+=size[son];
            if (size[Rem[x]]<size[son]) Rem[x]=son;
        }
    }
    void build(int x){
        if (!x) return;
        dfn[ID[x]=++Time]=x;
        top[x]=Rem[fa[x]]==x?top[fa[x]]:x;
        build(Rem[x]);
        for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
            if (son==fa[x]||son==Rem[x])    continue;
            build(son);
        }
    }
    void work(int x,int y){
        S1::node resx,resy,res;
        while (top[x]!=top[y]){
            if (deep[top[x]]<deep[top[y]]){
                resy=ST.Query(1,1,n,ID[top[y]],ID[y])+resy;
                y=fa[top[y]];
            }else{
                resx=ST.Query(1,1,n,ID[top[x]],ID[x])+resx;
                x=fa[top[x]];
            }
        }
        if (deep[x]<deep[y])    resy=ST.Query(1,1,n,ID[x],ID[y])+resy;
        else    resx=ST.Query(1,1,n,ID[y],ID[x])+resx;
        res=-resx+resy;
        printf("%d\n",max(res.L[0],res.L[1]));
    }
}HLD;//Heavy Light Decomposition
void read(int *V){
    scanf("%s",str);
    V[0]=str[0]=='.';
    V[1]=str[1]=='.';
}
int main(){
    n=read(),m=read();
    for (int i=1;i<n;i++){
        int x=read(),y=read();
        HLD.insert(x,y);
    }
    for (int i=1;i<=n;i++)  read(v[i]);
    HLD.dfs(1),HLD.build(1),ST.build(1,1,n);
    for (int i=1;i<=m;i++){
        scanf("%s",str);
        if (str[0]=='C'){
            int x=read(); read(v[x]);
            ST.Modify(1,1,n,ID[x]);
        }
        if (str[0]=='Q'){
            int x=read(),y=read();
            HLD.work(x,y);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Wolfycz/p/9994282.html
今日推荐