bzoj3091

来源:互联网 发布:网络切换app 编辑:程序博客网 时间:2024/05/20 00:12

裸体   只是第四个操作有点恼火

神tm期望。。。

http://www.tesoon.com/ask/htm/03/12356.htm

(居然真是小学生都会。。

(简直不如07年水平=.=

(我到河北省来>_<

安利一个博客

http://blog.csdn.net/popoqqq/article/details/40823659

popoqqq大爷讲的好


#include <cstdio>#define LL long longint n,m;int ch[50010][2],rv[50010],fa[50010];int x[50010],lz[50010],sz[50010];LL sum[50010],lsum[50010],rsum[50010],exp[50010];int st[50010],tp;int read_int () {char c = getchar();int re = 0;for(;c > '9' || c < '0';c = getchar());for(;c >= '0' && c <= '9';c = getchar())re = re * 10 + c - '0';return re;}void pu (int u) {sum[u] = (LL)x[u] + sum[ch[u][0]] + sum[ch[u][1]];sz[u] = sz[ch[u][0]] + sz[ch[u][1]] + 1;lsum[u] = lsum[ch[u][0]] + (LL)(sz[ch[u][0]] + 1) * (LL)x[u] + lsum[ch[u][1]] + (LL)(sz[ch[u][0]] + 1) * sum[ch[u][1]];rsum[u] = rsum[ch[u][1]] + (LL)(sz[ch[u][1]] + 1) * (LL)x[u] + rsum[ch[u][0]] + (LL)(sz[ch[u][1]] + 1) * sum[ch[u][0]];exp[u] = (LL)(sz[ch[u][1]] + 1) * lsum[ch[u][0]] + exp[ch[u][0]] + (LL)(sz[ch[u][0]] + 1) * (LL)(sz[ch[u][1]] + 1) * (LL)x[u] + (LL)(sz[ch[u][0]] + 1) * rsum[ch[u][1]] + exp[ch[u][1]];}void reverse (int u) {int t = ch[u][0];ch[u][0] = ch[u][1];ch[u][1] = t;LL tt = lsum[u];lsum[u] = rsum[u];rsum[u] = tt;rv[u] ^= 1;}void add (int u,int v) {x[u] += v;sum[u] += sz[u] * v;lsum[u] += (LL)(sz[u] + 1) * (LL)sz[u] * (LL)v / 2;rsum[u] += (LL)(sz[u] + 1) * (LL)sz[u] * (LL)v / 2;exp[u] += v * ((LL)sz[u] * (LL)(sz[u] + 1) * (LL)(sz[u] + 2) / 6LL);lz[u] += v;}void pd (int u) {if(rv[u]) {rv[u] = 0;reverse(ch[u][0]);reverse(ch[u][1]);}if(lz[u]) {add(ch[u][0],lz[u]);add(ch[u][1],lz[u]);lz[u] = 0;}}void rot (int u,int d) {ch[fa[u]][d ^ 1] = ch[u][d];if(ch[u][d])fa[ch[u][d]] = fa[u];int t = fa[fa[u]];ch[u][d] = fa[u];fa[fa[u]] = u;fa[u] = t;pu(ch[u][d]);pu(u);if(!t)return;if(ch[t][0] == ch[u][d])ch[t][0] = u;if(ch[t][1] == ch[u][d])ch[t][1] = u;}void splay (int u) {int t = u;st[tp = 1] = t;while(ch[fa[t]][0] == t || ch[fa[t]][1] == t) {t = fa[t];st[++tp] = t;}while(tp)pd(st[tp--]);while(ch[fa[u]][0] == u || ch[fa[u]][1] == u) {if(ch[fa[fa[u]]][0] != fa[u] && ch[fa[fa[u]]][1] != fa[u]) {if(ch[fa[u]][0] == u)rot(u,1);else rot(u,0);}else {int d = ch[fa[fa[u]]][0] == fa[u];if(ch[fa[u]][d] == u) {rot(u,d ^ 1);rot(u,d);}else {rot(u,d);rot(u,d);}}}}void access (int u) {int v = 0;while(u) {splay(u);ch[u][1] = v;pu(u);v = u;u = fa[u];}}void mtr (int u) {access(u);splay(u);reverse(u);}int fr (int u) {access(u);splay(u);int t = u;while(ch[t][0])t = ch[t][0];return t;}LL gcd (LL a,LL b) {if(a % b)return gcd(b,a % b);return b;}int main () {n = read_int();m = read_int();for(int i = 1;i <= n;++i) {exp[i] = lsum[i] = rsum[i] = x[i] = sum[i] = read_int();sz[i] = 1;}int f,u,v,t;for(int i = 1;i < n;++i) {u = read_int();v = read_int();mtr(u);mtr(v);fa[u] = v;}for(int i = 1;i <= m;++i) {f = read_int();u = read_int();v = read_int();if(f == 1) {if(fr(u) != fr(v))continue;mtr(u);access(v);splay(u);if(ch[u][1] == v && !ch[v][0]) {access(u);fa[v] = 0;}}else {if(f == 2) {if(fr(u) == fr(v))continue;mtr(u);mtr(v);fa[v] = u;}else {if(f == 3) {t = read_int();if(fr(u) != fr(v))continue;mtr(u);access(v);splay(u);add(u,t);}else {if(fr(u) != fr(v))printf("-1\n");else {mtr(u);access(v);splay(u);LL hh = gcd(exp[u],(LL)(sz[u] + 1) * (LL)sz[u] / 2LL);LL ff = exp[u] / hh;hh = (LL)(sz[u] + 1) * (LL)sz[u] / 2LL / hh;printf("%lld/%lld\n",ff,hh);}}}}}} 


0 0