澪lane @ 2019-07-15 22:39:46
RT
#include <bits/stdc++.h>
#define scf scanf
#define ptf printf
#define ll long long
#define ull unsigned long long
#define Rint register int
#define Rll register long long
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))
using namespace std;
const int N = 1e6+101;
ll w_[N], v_[N];
ll vis[N], pla[N], chg[N], cdy[N], num[N];
ll cnt = 0, tot = 0, ans_ = 0, len, now_0 = 0, now_1 = 0;
ll ans[N];
ll belong[N];
ll fath[N][30], dep[N], first[N], last[N], order[N];
ll head[N], to[N], nex[N];
struct Q{
ll l,r;
ll id;
ll lca,t;
}qt[N];
inline ll read(){
ll x=0,f = 1;
char ch = getchar();
while(!isdigit(ch)){
if(ch == '-')
f = -1;
ch = getchar();
}
while(isdigit(ch)){
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x*f;
}
inline int cmp(Q x, Q y){
return (belong[x.l] ^ belong[y.l]) ? belong[x.l] < belong[y.l] :((belong[x.r] ^ belong[y.r]) ? belong[x.r] < belong[y.r] : x.t < y.t);
}
inline void add_e(Rll u, Rll v){
to[++tot] = v;
nex[tot] = head[u];
head[u] = tot;
}
inline void Dfs(Rll x){
order[++cnt] = x;
first[x] = cnt;
for(Rint i = head[x]; i + 1; i = nex[i]){
Rint y = to[i];
if(y == fath[x][0])
continue;
fath[y][0] = x;
dep[y] = dep[x] + 1;
for(Rint j = 1; (1 << j) <= dep[y]; j++)
fath[y][j] = fath[fath[y][j-1]][j-1];
Dfs(y);
}
order[++cnt] = x;
last[x] = cnt;
}
inline ll Lca(Rll x, Rll y){
if(dep[x] < dep[y])
swap(x,y);
for(Rint i = 20; i >= 0; i--)
if(dep[x] - (1 << i) >= dep[y])
x = fath[x][i];
if(x == y)
return x;
for(Rint i = 20; i >= 0; i--)
if(fath[x][i] != fath[y][i])
x = fath[x][i], y = fath[y][i];
return fath[x][0];
}
inline void add(Rll x){
ans_ += v_[cdy[x]] * w_[++num[cdy[x]]];
}
inline void del(Rll x){
ans_ -= v_[cdy[x]] * w_[num[cdy[x]]--];
}
inline void move(Rll x){
vis[x] ? del(x) : add(x);
vis[x] ^= 1;
}
inline void change(Rll x){
if(vis[pla[x]]){
move(pla[x]);
swap(cdy[pla[x]], chg[x]);
move(pla[x]);
}
else
swap(cdy[pla[x]], chg[x]);
}
int main(){
memset(head, -1, sizeof head);
memset(dep, 0, sizeof dep);
memset(vis, 0, sizeof vis);
int n = read(), m = read(), q_num = read();
for(Rint i = 1; i <= m; i++)
v_[i] = read();
for(Rint i = 1; i <= n; i++)
w_[i] = read();
for(Rint i = 1; i < n; i++){
int u = read(), v = read();
add_e(u, v), add_e(v, u);
}
for(Rint i = 1; i <= n ; i++)
cdy[i] = read();
dep[1] = 1;
Dfs(1);
// /*
len = pow(cnt, 2.0 / 3.0);
int part = ceil((double) cnt / len);
for(Rint i = 1; i <= part; i++)
for(Rint j = len * (i - 1) + 1; j <= i * len; j++)
belong[j] = i;
for(Rint i = 1; i <= q_num; i++){
int type = read();
if(type == 0){
++now_1;
pla[now_1] = read();
chg[now_1] = read();
}
else if(type == 1){
int l = read(), r = read(), lca = Lca(l, r);
++now_0;
qt[now_0].id = now_0,qt[now_0].t = now_1;
// cout << now_1 << endl;
if(first[l] > first[r])
swap(l, r);
if(l == lca)
qt[now_0].l = first[l], qt[now_0].r = first[r];
else
qt[now_0].l = last[l], qt[now_0].r = first[r], qt[now_0].lca = lca;
}
}
sort(qt + 1,qt + now_0 + 1,cmp);
int l = 1, r = 0, momt = 0;
for(Rint i = 1; i <= now_0; i++){
int L = qt[i].l, R = qt[i].r, tim = qt[i].t, lca = qt[i].lca;
// cout << "Tim = " << tim << endl;
// cout << L << " " << R << endl;
while(l < L)
move(order[l++]);
while(l > L)
move(order[--l]);
while(r < R)
move(order[++r]);
while(r > R)
move(order[r--]);
while(momt < tim)
change(++momt);
while(momt > tim)
change(momt--);
if(lca)
move(lca);
ans[qt[i].id] = ans_;
if(lca)
move(lca);
}
// */
for(Rint i = 1; i <= now_0; i++)
ptf("%lld\n", ans[i]);
}
by 澪lane @ 2019-07-15 22:40:35
交上去也是WA一片QAQ
by 澪lane @ 2019-07-16 08:57:53
可以沉贴了
by 澪lane @ 2019-07-16 08:58:17
但我也不知道改了什么QAQ