BZOJ1180OTOCI

1180: [CROATIAN2009]OTOCI
Time Limit: 50 Sec Memory Limit: 162 MB
Submit: 773 Solved: 474
Description
給出n個結點以及每個點初始時對應的權值wi。起始時點與點之間沒有連邊。有3類操作: 1、bridge A B:詢問結點A與結點B是否連通。如果是則輸出“no”。否則輸出“yes”,並且在結點A和結點B之間連一條無向邊。 2、penguins A X:將結點A對應的權值wA修改爲X。 3、excursion A B:如果結點A和結點B不連通,則輸出“impossible”。否則輸出結點A到結點B的路徑上的點對應的權值的和。給出q個操作,要求在線處理所有操作。數據範圍:1<=n<=30000, 1<=q<=300000, 0<=wi<=1000。
Input
第一行包含一個整數n(1<=n<=30000),表示節點的數目。第二行包含n個整數,第i個整數表示第i個節點初始時對應的權值。第三行包含一個整數q(1<=n<=300000),表示操作的數目。以下q行,每行包含一個操作,操作的類別見題目描述。任意時刻每個節點對應的權值都是1到1000的整數。
Output
輸出所有bridge操作和excursion操作對應的輸出,每個一行。
Sample Input
5
4 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5
Sample Output
4
impossible
yes
6
yes
yes
15
yes
15
16
LCT模板題。。
雙倍經驗題。。
附上本蒟蒻的代碼:

#include<cstdio>
#include<iostream>
using namespace std;
int n,m,father[30001],c[30001][2],st[30001],sum[30001],val[30001];
bool rev[30001];

int read()
{
    int w=0,f=1; char ch=getchar();
    while (ch<'0' || ch>'9')
      {
        if (ch=='-') f=-1;
        ch=getchar();
      }
    while (ch>='0' && ch<='9')
      w=w*10+ch-'0',ch=getchar();
    return w*f;
}

bool isroot(int x)
{
    return c[father[x]][0]!=x && c[father[x]][1]!=x;
}

void update(int x)
{
    int l=c[x][0],r=c[x][1];
    sum[x]=sum[l]+sum[r]+val[x];
}

void pushdown(int x)
{
    int l=c[x][0],r=c[x][1];
    if (rev[x]) rev[x]^=1,rev[l]^=1,rev[r]^=1,swap(c[x][0],c[x][1]);
}

void rotate(int x)
{
    int y=father[x],z=father[y],l,r;
    if (c[y][0]==x) l=0;
    else l=1;
    r=l^1;
    if (!isroot(y))
      if (c[z][0]==y) c[z][0]=x;
      else c[z][1]=x;
    father[x]=z,father[y]=x,father[c[x][r]]=y,c[y][l]=c[x][r],c[x][r]=y,update(y),update(x);
}

void splay(int x)
{
    int i,top=0,y,z;
    st[++top]=x;
    for (i=x;!isroot(i);i=father[i]) st[++top]=father[i];
    for (i=top;i;i--) pushdown(st[i]);
    while (!isroot(x))
      {
        y=father[x],z=father[y];
        if (!isroot(y))
          if (c[y][0]==x^c[z][0]==y) rotate(x);
          else rotate(y);
        rotate(x);
      }
}

void access(int x)
{
    int t;
    for (t=0;x;t=x,x=father[x]) splay(x),c[x][1]=t,update(x);
}

void rever(int x)
{
    access(x),splay(x),rev[x]^=1;
}

void link(int x,int y)
{
    rever(x),father[x]=y;
}

int find(int x)
{
    int y=x;
    access(x),splay(x);
    while (c[y][0]) y=c[y][0];
    return y;
}

int main()
{
    int i,x,y;
    char s[20];
    n=read();
    for (i=1;i<=n;i++) sum[i]=val[i]=read();
    m=read();
    for (i=1;i<=m;i++)
      {
        scanf("%s",&s),x=read(),y=read();
        if (s[0]=='b')
          if (find(x)!=find(y)) printf("yes\n"),link(x,y);
          else printf("no\n");
        if (s[0]=='p')
          rever(x),val[x]=y,update(x);
        if (s[0]=='e')
          if (find(x)!=find(y)) printf("impossible\n");
          else rever(x),access(y),splay(y),printf("%d\n",sum[y]);
      }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章