标签:put -- his cut 提示 ret include ted 范围
W国地人物博,有n座城市组成,共n-1条双向道路连接其中的两座城市,且任意两座城市都可相互到达。
风景秀美的w国吸引了无数慕名而来的游客,根据游客对每座城市的打分,我们定义第i座城市的美丽度为\(a_i\)。一次从城市x到城市y的旅行,所获得的的偷悦指数为从城市x到城市y所有城市的美丽度之和(包括X和y)。我们诅义这个值为H(x,y)。
现在小A在城市X,Sharon在城市Y,他们想知道如果在城市X到城市Y之间的所有城市中任选两座城市x和y(x可以等于y),那么H(x,y)的期望值是多少,我们记这个期望值为E(x,y)。
当然,城市之间的交通状况飘忽不定,因此我们不能排除某些时刻某些道路将无法通行。某些时刻会突然添加新的道路。以及游客们审美观的改变,某些城市的美丽度也会发现变化。作为W国负责旅游行业的T君,他要求你来写一个程序来模拟上而的所有过程。
第一行两个整数,n,m表示城市个数和操作个数。
接下来一行n个整数,第i个表示\(a_i\)。 接下来n-1行,每行两个整数u,v,表示u和v之间有一条路。 接下来m行,是进行下面的操作:
1 u v
如果城市u和城市v已经无直接连接的道路,则忽略这个操作,否则删除u,v之间的道路。2 u v
如果城市u和城市v联通那么忽略。否则在u,v之间添加一条道路。3 u v d
如果城市u和城市v不连通,那么忽略。否则将城市u到城市v的路径中所有城市(包括u和v)的美丽度都增加d。4 u v
询问E(u,v)的值对于操作4,输出答案,一个经过化简的分数p/q。如果u和v不连通输出-1。
4 5
1 3 2 5
1 2
1 3
2 4
4 2 4
1 2 4
2 3 4
3 1 4 1
4 1 4
16/3
6/1
对于所有数据满足 \(1<=N<=50,000 1<=M<=50,000 1<=a_i<=10^6 1<=D<=100 1<=U,V<=N\)
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
LL read() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
template<class T> bool chkmax(T &a, const T &b) { return a < b? a = b, 1 : 0; }
template<class T> bool chkmin(T &a, const T &b) { return b < a? a = b, 1 : 0; }
const int maxn = 1e5 + 10;
LL f[maxn];
void predoit(int n) {
for(int i = 1; i <= n; i++) f[i] = i;
for(int i = 1; i <= n; i++) f[i] += f[i - 1];
for(int i = 1; i <= n; i++) f[i] += f[i - 1];
}
LL gcd(LL x, LL y) { return y? gcd(y, x % y) : x; }
struct LCT {
protected:
struct node {
node *ch[2], *fa;
LL sum, val, add, lsum, rsum, totval;
int siz, rev;
node(LL sum = 0, LL val = 0, LL add = 0, LL lsum = 0, LL rsum = 0, LL totval = 0, int siz = 0, int rev = 0): sum(sum), val(val), add(add), lsum(lsum), rsum(rsum), totval(totval), siz(siz), rev(rev) { ch[0] = ch[1] = fa = NULL; }
void upd() {
siz = ch[0]->siz + ch[1]->siz + 1;
totval = ch[0]->totval + ch[1]->totval + val;
lsum = ch[0]->lsum + ch[1]->lsum + 1LL * (ch[1]->totval + val) * (ch[0]->siz + 1);
rsum = ch[1]->rsum + ch[0]->rsum + 1LL * (ch[0]->totval + val) * (ch[1]->siz + 1);
sum = ch[0]->sum + ch[1]->sum + 1LL * ch[0]->lsum * (ch[1]->siz + 1) + 1LL * ch[1]->rsum * (ch[0]->siz + 1) + 1LL * (ch[0]->siz + 1) * (ch[1]->siz + 1) * val;
}
void trnadd(LL v) {
if(this->fa == this) return;
totval += 1LL * siz * v;
add += v;
lsum += (1LL * siz * (siz + 1) >> 1) * v;
rsum += (1LL * siz * (siz + 1) >> 1) * v;
sum += f[siz] * v;
val += v;
}
void trnrev() {
if(this->fa == this) return;
std::swap(ch[0], ch[1]), rev ^= 1;
std::swap(lsum, rsum);
}
void dwn() {
if(rev) {
ch[0]->trnrev();
ch[1]->trnrev();
rev = 0;
}
if(add) {
ch[0]->trnadd(add);
ch[1]->trnadd(add);
add = 0;
}
}
bool ntr() { return (fa->ch[0] == this || fa->ch[1] == this); }
bool isr() { return this == fa->ch[1]; }
}pool[maxn], *null;
void rot(node *x) {
node *y = x->fa, *z = y->fa;
bool k = x->isr(); node *w = x->ch[!k];
if(y->ntr()) z->ch[y->isr()] = x;
(x->ch[!k] = y)->ch[k] = w;
(y->fa = x)->fa = z;
if(w != null) w->fa = y;
y->upd(), x->upd();
}
void splay(node *o) {
static node *st[maxn]; int top;
st[top = 1] = o;
while(st[top]->ntr()) st[top + 1] = st[top]->fa, top++;
while(top) st[top--]->dwn();
while(o->ntr()) {
if(o->fa->ntr()) rot(o->isr() ^ o->fa->isr()? o : o->fa);
rot(o);
}
}
void access(node *x) {
for(node *y = null; x != null; x = (y = x)->fa)
splay(x), x->ch[1] = y, x->upd();
}
void makeroot(node *x) { access(x), splay(x), x->trnrev(); }
node *findroot(node *x) {
access(x), splay(x);
while(x->dwn(), x->ch[0] != null) x = x->ch[0];
return x;
}
public:
void link(int l, int r) {
node *x = pool + l, *y = pool + r;
if(findroot(x) == findroot(y)) return;
makeroot(x), x->fa = y;
}
void cut(int l, int r) {
node *x = pool + l, *y = pool + r;
makeroot(x), access(y), splay(y);
if(y->ch[0] == x && x->ch[1] == null) y->ch[0] = x->fa = null, y->upd();
}
void add(int l, int r, LL val) {
node *x = pool + l, *y = pool + r;
if(findroot(x) != findroot(y)) return;
makeroot(x), access(y), splay(y);
y->trnadd(val);
}
void query(int l, int r) {
node *x = pool + l, *y = pool + r;
if(findroot(x) != findroot(y)) return (void)(puts("-1"));
makeroot(x), access(y), splay(y);
LL dn = 1LL * y->siz * (y->siz + 1) >> 1;
LL up = y->sum;
LL gc = gcd(up, dn);
printf("%lld/%lld\n", up / gc, dn / gc);
}
void set(int x, LL val) {
pool[x].val = val, pool[x].siz = 1;
pool[x].ch[0] = pool[x].ch[1] = pool[x].fa = null;
pool[x].upd();
}
void init() {
null = new node();
null->ch[0] = null->ch[1] = null->fa = null;
}
}T;
int main() {
int n = read(), m = read();
T.init(); predoit(n);
for(int i = 1; i <= n; i++) T.set(i, read());
for(int i = 1; i < n; i++) T.link(read(), read());
LL p, x, y, d;
while(m --> 0) {
p = read();
if(p == 1) x = read(), y = read(), T.cut(x, y);
if(p == 2) x = read(), y = read(), T.link(x, y);
if(p == 3) x = read(), y = read(), d = read(), T.add(x, y, d);
if(p == 4) x = read(), y = read(), T.query(x, y);
}
return 0;
}
标签:put -- his cut 提示 ret include ted 范围
原文地址:https://www.cnblogs.com/olinr/p/10652745.html