1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<stack> using namespace std;
typedef int int_; #define int unsigned long long
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=50005;
struct Tree { int child[2],father,size; bool rev; int val,sum,lazy,small,big,ans; };
struct Link_Cut_Tree { Tree tree[maxn]; stack<int>S; #define fa(x) tree[x].father #define ls(x) tree[x].child[0] #define rs(x) tree[x].child[1] #define rev(x) tree[x].rev #define val(x) tree[x].val #define big(x) tree[x].big #define sum(x) tree[x].sum #define ans(x) tree[x].ans #define lazy(x) tree[x].lazy #define size(x) tree[x].size #define small(x) tree[x].small bool isroot(int index) { return ls(fa(index))!=index&&rs(fa(index))!=index; } bool checkson(int index) { return rs(fa(index))==index; } void modify(int index,int v) { val(index)+=v; lazy(index)+=v; sum(index)+=size(index)*v; small(index)+=size(index)*(size(index)+1)/2*v; big(index)+=size(index)*(size(index)+1)/2*v; ans(index)+=size(index)*(size(index)+1)*(size(index)+2)/6*v; } void rever(int index) { rev(index)^=1; swap(ls(index),rs(index)); swap(small(index),big(index)); } void push_down(int index) { if(rev(index)) { if(ls(index))rever(ls(index)); if(rs(index))rever(rs(index)); rev(index)=0; } if(lazy(index)) { if(ls(index))modify(ls(index),lazy(index)); if(rs(index))modify(rs(index),lazy(index)); lazy(index)=0; } } void push_up(int index) { int ls=ls(index),rs=rs(index); size(index)=size(ls)+size(rs)+1; sum(index)=sum(ls)+sum(rs)+val(index); small(index)=small(ls)+small(rs)+(size(ls)+1)*sum(rs)+val(index)*(size(ls)+1); big(index)=big(ls)+(size(rs)+1)*sum(ls)+big(rs)+val(index)*(size(rs)+1); ans(index)=ans(ls)+(size(rs)+1)*small(ls)+ans(rs)+(size(ls)+1)*big(rs)+(size(ls)+1)*(size(rs)+1)*val(index); } void rotate(int index) { int father=fa(index),grand=fa(father),side=checkson(index); if(!isroot(father))tree[grand].child[checkson(father)]=index; tree[father].child[side]=tree[index].child[side^1]; fa(tree[father].child[side])=father; fa(father)=index; tree[index].child[side^1]=father; fa(index)=grand; push_up(father); push_up(index); } void splay(int index) { S.push(index); for(int i=index; !isroot(i); i=fa(i))S.push(fa(i)); while(!S.empty())push_down(S.top()),S.pop(); for(int father; !isroot(index); rotate(index)) { father=fa(index); if(!isroot(father))rotate(checkson(index)==checkson(father)?father:index); } } void access(int index) { for(int son=0; index; son=index,index=fa(index)) { splay(index); rs(index)=son; push_up(index); } } void reverse(int index) { access(index); splay(index); rev(index)^=1; swap(ls(index),rs(index)); } void link(int x,int y) { reverse(x); fa(x)=y; } void split(int x,int y) { reverse(x); access(y); splay(y); } void cut(int x,int y) { split(x,y); if(size(y)>2)return; ls(y)=fa(x)=0; } int get_root(int index) { access(index); splay(index); int u=index; while(ls(u))push_down(u),u=ls(u); push_down(u); return u; } void modify(int x,int y,int v) { split(x,y); modify(y,v); } pair<int,int> query(int x,int y) { split(x,y);
int u=ans(y),d=size(y)*(size(y)+1)/2; int gcd=__gcd(u,d); u/=gcd,d/=gcd; return make_pair(u,d); } void debug(int x) { if(ls(x))debug(ls(x)); cout<<x<<" "<<fa(x)<<" "<<ls(x)<<" "<<rs(x)<<" "<<small(x)<<" "<<big(x)<<" "<<ans(x)<<endl; if(rs(x))debug(rs(x)); } } lct;
int n,q;
int_ main() { n=Get_Int(); q=Get_Int(); for(int i=1; i<=n; i++)lct.tree[i].val=Get_Int(); for(int i=1; i<n; i++)lct.link(Get_Int(),Get_Int()); while(q--) { int opt=Get_Int(),x=Get_Int(),y=Get_Int(); if(opt==1)lct.cut(x,y); else if(opt==2) { if(lct.get_root(x)==lct.get_root(y))continue; lct.link(x,y); } else if(opt==3) { int v=Get_Int(); if(lct.get_root(x)!=lct.get_root(y))continue; lct.modify(x,y,v); } else { if(lct.get_root(x)!=lct.get_root(y)) { puts("-1"); continue; } pair<int,int>tmp=lct.query(x,y); printf("%llu/%llu\n",tmp.first,tmp.second); } } return 0; }
|