【LCT】BZOJ2843[極地旅行社]題解

題目概述

n 個點,第 i 個點有 ai 個帝王JZ,給出 m 個操作,操作有三種:1.連接 xy 。2.令 ax=y 。3.詢問 xy 路徑上帝王JZ的總和。

解題報告

emm……LCT裸題……我在練板子……

#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
const int maxn=30000;

int n,te,a[maxn+5],sum[maxn+5];
int son[maxn+5][2],fa[maxn+5],flip[maxn+5];

#define Eoln(x) ((x)==10||(x)==13||(x)==EOF)
inline char readc(){
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF;return *l++;
}
inline int readi(int &x){
    int tot=0,f=1;char ch=readc(),lst='+';
    while (!isdigit(ch)) {if (ch==EOF) return EOF;lst=ch;ch=readc();}
    if (lst=='-') f=-f;
    while (isdigit(ch)) tot=(tot<<3)+(tot<<1)+ch-48,ch=readc();
    return x=tot*f,Eoln(ch);
}
inline char getlwr() {char ch=readc();while (!islower(ch)) ch=readc();return ch;}
#define is_ro(p) ((p)!=son[fa[p]][0]&&(p)!=son[fa[p]][1])
#define Son(p) ((p)==son[fa[p]][1])
inline void Pushup(int p) {sum[p]=sum[son[p][0]]+a[p]+sum[son[p][1]];}
inline void Rotate(int t){
    int p=fa[t],d=Son(t)^1;son[p][d^1]=son[t][d];son[t][d]=p;
    Pushup(p);Pushup(t);if (!is_ro(p)) son[fa[p]][Son(p)]=t;
    if (son[p][d^1]) fa[son[p][d^1]]=p;fa[t]=fa[p];fa[p]=t;
}
inline void Addflip(int p) {swap(son[p][0],son[p][1]);flip[p]^=1;}
inline void Pushdown(int p) {if (flip[p]) flip[p]^=1,Addflip(son[p][0]),Addflip(son[p][1]);}
inline void Splay(int p){
    static int top,stk[maxn+5];stk[top=1]=p;;
    for (int i=p;!is_ro(i);i=fa[i]) stk[++top]=fa[i];
    while (top) Pushdown(stk[top--]);
    for (int pre=fa[p];!is_ro(p);Rotate(p),pre=fa[p])
        if (!is_ro(pre)) Rotate(Son(p)==Son(pre)?pre:p);
}
inline void Access(int p) {for (int lst=0;p;lst=p,p=fa[p]) Splay(p),son[p][1]=lst,Pushup(p);}
inline void Makero(int p) {Access(p);Splay(p);Addflip(p);}
inline void Link(int x,int y) {Makero(x);fa[x]=y;}
inline int getfa(int x) {Access(x);Splay(x);while (son[x][0]) x=son[x][0];return x;}
inline int Sum(int x,int y) {Makero(x);Access(y);Splay(x);return sum[x];}
int main(){
    freopen("program.in","r",stdin);
    freopen("program.out","w",stdout);
    readi(n);for (int i=1;i<=n;i++) readi(a[i]),Pushup(i);
    for (readi(te);te;te--){
        char td=getlwr();int x,y;readi(x);readi(y);
        if (td=='b') puts(getfa(x)==getfa(y)?"no":(Link(x,y),"yes"));
        else if (td=='p') Access(x),Splay(x),a[x]=y,Pushup(x);
        else if (getfa(x)==getfa(y)) printf("%d\n",Sum(x,y)); else puts("impossible");
    }
    return 0;
}
發佈了340 篇原創文章 · 獲贊 124 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章