现在的位置: 首页 > 综合 > 正文

道馆之战

2018年01月15日 ⁄ 综合 ⁄ 共 6685字 ⁄ 字号 评论关闭

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

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

树链剖分

首先先剖分树链

这道题问的是最大步数,比较麻烦

线段树中维护一个数组[0..1][0..1]

表示从左端的上面(下面)出发到右端的上面(下面)所能走过的最长路径,=0代表此路不通

线段树插入查询都比较好写

树链剖分部分十分冗长

为了判断是否有通路,对于每条链都采取了二分

需要注意的是将a到公共祖先、b到公共祖先两条链并起来的时候是头跟头接起来

program fight;
type
  group=array [0..1,0..1] of longint;
var
  o,z,ans:group;
  ok:boolean;
  tot,u,v,n,m,i,j,k,w,l,r,mid,x:longint;
  st,top,num,go,height,total,root:array [0..30001] of longint;
  left,right:array [0..500001] of longint;
  long:array [0..500001] of group;
  father:array [0..17,0..30001] of longint;
  next,point:array [0..60001] of longint;
  s:string;
  space,order:char;

function sum (a:group):longint;inline;
begin
  exit(a[0,0]+a[0,1]+a[1,0]+a[1,1]);
end;

function max (a,b:longint):longint;inline;
begin
  if a>b then exit(a)
         else exit(b);
end;

function big (var a:group):longint;inline;
begin
  exit(max(max(max(a[0,0],a[0,1]),a[1,0]),a[1,1]));
end;

procedure connect (u,v:longint);inline;
begin
  inc(tot);
  point[tot]:=v;
  next[tot]:=root[u];
  root[u]:=tot;
end;

function convert (var a,b:group):group;inline;
var
  i,j:longint;
  ans:group;
begin
  for i:=0 to 1 do
    for j:=0 to 1 do
      begin
        ans[i,j]:=0;
        if (a[i,0]<>0)and(b[0,j]<>0) then
          ans[i,j]:=max(ans[i,j],a[i,0]+b[0,j]);
        if (a[i,1]<>0)and(b[1,j]<>0) then
          ans[i,j]:=max(ans[i,j],a[i,1]+b[1,j]);
      end;
  exit(ans);
end;

function anticonvert (var a,b:group):group;inline;
var
  i,j:longint;
  ans:group;
begin
  for i:=0 to 1 do
    for j:=0 to 1 do
      begin
        ans[i,j]:=0;
        if (a[0,i]<>0)and(b[0,j]<>0) then
          ans[i,j]:=max(ans[i,j],a[0,i]+b[0,j]);
        if (a[1,i]<>0)and(b[1,j]<>0) then
          ans[i,j]:=max(ans[i,j],a[1,i]+b[1,j]);
      end;
  exit(ans);
end;

procedure swap (var a,b:longint);inline;
begin
  if a=b then exit;
  b:=a xor b;
  a:=a xor b;
  b:=a xor b;
end;

function lca (a,b:longint):longint;inline;
var
  i:longint;
begin
  if height[a]<height[b] then swap(a,b);
  while height[a]<>height[b] do
    a:=father[trunc(ln(height[a]-height[b])/ln(2)),a];
  if a=b then exit(a);
  i:=17;
  repeat
    if father[0,a]=father[0,b] then exit(father[0,a]);
    while father[i,a]=father[i,b] do dec(i);
    a:=father[i,a];
    b:=father[i,b];
  until false;
end;

procedure dfs (now,high:longint);
var
  i:longint;
begin
  height[now]:=high;
  i:=0;
  while father[i,now]<>0 do
    begin
      father[i+1,now]:=father[i,father[i,now]];
      inc(i);
    end;
  total[now]:=1;
  i:=root[now];
  go[now]:=0;
  while i<>0 do
    begin
      if point[i]<>father[0,now] then
        begin
          father[0,point[i]]:=now;
          dfs(point[i],high+1);
          total[now]:=total[now]+total[point[i]];
          if total[point[i]]>total[go[now]] then go[now]:=point[i];
        end;
      i:=next[i];
    end;
end;

procedure renum (now,up:longint);
var
  i:longint;
begin
  top[now]:=up;
  inc(tot);
  num[now]:=tot;
  if go[now]<>0 then renum(go[now],up);
  i:=root[now];
  while i<>0 do
    begin
      if (point[i]<>go[now])and(point[i]<>father[0,now]) then
        renum(point[i],point[i]);
      i:=next[i];
    end;
end;

procedure insert (c,x,l,r,now:longint);
var
  mid:longint;
begin
  if l=r then
    begin
      case c of
        0:begin
           long[now,0,0]:=1;
           long[now,1,1]:=1;
           long[now,1,0]:=2;
           long[now,0,1]:=2;
          end;
        1:begin
            long[now,0,0]:=0;
            long[now,0,1]:=0;
            long[now,1,1]:=1;
            long[now,1,0]:=0;
          end;
        2:begin
            long[now,0,0]:=1;
            long[now,0,1]:=0;
            long[now,1,0]:=0;
            long[now,1,1]:=0;
          end;
        3:begin
            long[now,0,0]:=0;
            long[now,0,1]:=0;
            long[now,1,0]:=0;
            long[now,1,1]:=0;
          end;
      end;
      exit;
    end;
  mid:=(l+r) div 2;
  if left[now]=0 then
    begin
      inc(tot);
      left[now]:=tot;
    end;
  if right[now]=0 then
    begin
      inc(tot);
      right[now]:=tot;
    end;
  if x<=mid then
    insert(c,x,l,mid,left[now])
            else
    insert(c,x,mid+1,r,right[now]);
  long[now]:=convert(long[left[now]],long[right[now]]);
end;

function find (s,e,l,r,now:longint):group;
var
  mid:longint;
  q,p,ans:group;
begin
  if (s=l)and(e=r) then exit(long[now]);
  mid:=(l+r) div 2;
  if e<=mid then exit(find(s,e,l,mid,left[now]))
            else
  if s>mid then exit(find(s,e,mid+1,r,right[now]))
           else
    begin
      q:=find(s,mid,l,mid,left[now]);
      p:=find(mid+1,e,mid+1,r,right[now]);
      ans:=convert(q,p);
      exit(ans);
    end;
end;

begin
  assign(input,'fight.in');
  reset(input);
  assign(output,'fight.out');
  rewrite(output);
  read(n,m);
  for i:=1 to n-1 do
    begin
      readln(u,v);
      connect(u,v);
      connect(v,u);
    end;
  dfs(1,1);
  tot:=0;
  renum(1,1);
  tot:=0;
  for i:=1 to n do
    begin
      k:=0;
      readln(s);
      if s[1]='#' then k:=k+1;
      if s[2]='#' then k:=k+2;
      st[num[i]]:=k;
      insert(k,num[i],1,n,0);
    end;
  for i:=1 to m do
    begin
      read(order);
      if order='Q' then
        begin
          readln(u,v);
          if st[num[u]]=4 then
            begin
              writeln(0);
              continue;
            end;
          k:=lca(u,v);
          ok:=true;
          ans:=find(num[u],num[u],1,n,0);
          if u<>k then
            begin
              u:=father[0,u];
              while top[u]<>top[k] do
                begin
                  o:=find(num[u],num[u],1,n,0);
                  if sum(convert(o,ans))=0 then
                    begin
                      ok:=false;
                      break;
                    end;
                  l:=num[top[u]];
                  r:=num[u];
                  while l<>r do
                    begin
                      mid:=(l+r) div 2;
                      o:=find(mid,num[u],1,n,0);
                      if sum(convert(o,ans))=0 then l:=mid+1
                                               else r:=mid;
                    end;
                  o:=find(l,num[u],1,n,0);
                  ans:=convert(o,ans);
                  if l<>num[top[u]] then
                    begin
                      ok:=false;
                      break;
                    end;
                  u:=father[0,top[u]];
                end;
              if not ok then
                begin
                  writeln(big(ans));
                  continue;
                end;
              o:=find(num[u],num[u],1,n,0);
              if sum(convert(o,ans))=0 then
                begin
                  writeln(big(ans));
                  continue;
                end;
              l:=num[k];
              r:=num[u];
              while l<>r do
                begin
                  mid:=(l+r) div 2;
                  o:=find(mid,num[u],1,n,0);
                  if sum(convert(o,ans))=0 then l:=mid+1
                                           else r:=mid;
                end;
              o:=find(l,num[u],1,n,0);
              ans:=convert(o,ans);
              if l<>num[k] then
                begin
                  writeln(big(ans));
                  continue;
                end;
            end
                  else
            begin
              ans:=find(num[k],num[k],1,n,0);
              if sum(ans)=0 then
                begin
                  writeln(0);
                  continue;
                end;
            end;
          if v=k then
            begin
              writeln(big(ans));
              continue;
            end;
          fillchar(z,sizeof(z),0);
          while top[v]<>top[k] do
            begin
              o:=find(num[top[v]],num[top[v]],1,n,0);
              if sum(o)=0 then z:=o
                          else
                begin
                  l:=num[top[v]];
                  r:=num[v];
                  while l<>r do
                    begin
                      mid:=l+r-(l+r) div 2;
                      o:=find(num[top[v]],mid,1,n,0);
                      if sum(o)=0 then r:=mid-1
                                  else l:=mid;
                    end;
                  o:=find(num[top[v]],l,1,n,0);
                  if (l<>num[v])or(sum(convert(o,z))=0) then
                    z:=o
                                                        else
                    z:=convert(o,z);
                end;
              v:=father[0,top[v]];
            end;
          if v=k then
            begin
              o:=anticonvert(ans,z);
              if sum(o)=0 then writeln(big(ans))
                          else writeln(big(o));
              continue;
            end;
          l:=num[k]+1;
          r:=num[v];
          o:=find(l,l,1,n,0);
          if sum(o)=0 then
            begin
              writeln(big(ans));
              continue;
            end;
          while l<>r do
            begin
              mid:=l+r-(l+r) div 2;
              o:=find(num[k]+1,mid,1,n,0);
              if sum(o)=0 then r:=mid-1
                          else l:=mid;
            end;
          o:=find(num[k]+1,l,1,n,0);
          if (r<>num[v])or(sum(convert(o,z))=0) then
              z:=o
                                                else
              z:=convert(o,z);
          o:=anticonvert(ans,z);
          if sum(o)=0 then writeln(big(ans))
                      else writeln(big(o));
        end
                  else
        begin
          readln(u,space,s);
          k:=0;
          if s[1]='#' then k:=k+1;
          if s[2]='#' then k:=k+2;
          st[num[u]]:=k;
          insert(k,num[u],1,n,0);
        end;
    end;
  close(input);
  close(output);
end.
【上篇】
【下篇】

抱歉!评论已关闭.