lmAKf @ 2019-03-05 10:43:37
那位大佬可以帮忙看看代码吗?
#include<bits/stdc++.h>
using namespace std;
int read() {
char cc = getchar(); int cn = 0, flus = 1;
while(cc < '0' || cc > '9') { if( cc == '-' ) flus = -flus; cc = getchar(); }
while(cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar();
return cn * flus;
}
//#define file(a) freopen(); freopen();
const int N = 200000 + 5;
int v[N], w[N], vis[N], n, m, tot[N], p, b[N], st[N], ed[N], c[N], ctt;
int book[N], head[N], cnt, To[N * 2], Next[N * 2], q1, q2;
struct Q {
int l, r, time, lca, id;
}q[N];
struct C {
int wh, val;
}ch[N];
int fath[N][20], ans, dep[N], num[N], size;
bool cmp( Q x, Q y ) {
return ( x.l / size == y.l / size ) ? ( (x.r / size == y.r / size ) ? ( x.time < y.time ) : ( x.r < y.r ) ) : ( x.l < y.l );
}
void add( int x, int y ) {
To[++cnt] = y, Next[cnt] = head[x], head[x] = cnt,
To[++cnt] = x, Next[cnt] = head[y], head[y] = cnt;
}
void dfs1( int x, int fa ) {
b[++ctt] = x, st[x] = ctt;
fath[x][0] = fa, dep[x] = dep[fa] + 1;
for ( int i = 1; i <= 19; ++ i ) fath[x][i] = fath[fath[x][i - 1]][i - 1];
for ( int i = head[x]; i; i = Next[i] ) {
int to = To[i];
if ( to == fa ) continue;
dfs1( to, x );
}
b[++ctt] = x, ed[x] = ctt;
}
int LCA( int x, int y ) {
int lx = x, ly = y;
if ( dep[lx] < dep[ly] ) swap( lx, ly );
for ( int i = 19; i >= 0; -- i )
if ( dep[fath[lx][i]] >= dep[ly] ) lx = fath[lx][i];
for ( int i = 19; i >= 0; -- i ) {
if ( fath[lx][i] != fath[ly][i] ) lx = fath[lx][i], ly = fath[ly][i];
}
return ( lx != ly ) ? fath[lx][0] : lx;
}
void input() {
n = read(), m = read(), p = read(); size = pow( n, 0.67 );
for ( int i = 1; i <= m; ++ i ) v[i] = read();
for ( int i = 1; i <= n; ++ i ) w[i] = read();
int x, y;
for ( int i = 1; i < n; ++ i ) x = read(), y = read(), add( x, y );
for ( int i = 1; i <= n; ++ i ) c[i] = read();
}
void init() {
int type, x, y, lca;
for( int i = 1; i <= p; ++ i ) {
type = read(), x = read(), y = read();
if( type == 0 )
ch[++q2].wh = x, ch[q2].val = y;
else {
if( st[x] > st[y] ) swap( x, y );
++q1;
q[q1].id = q1, q[q1].time = q2, lca = LCA( x, y );
if ( x == lca ) q[q1].l = st[x], q[q1].r = st[y];
else q[q1].l = ed[x], q[q1].r = st[y], q[q1].lca = lca;
}
}
}
void Work( int x ) {
tot[x] ^= 1;
if( tot[x] == 0 ) {
ans -= w[vis[c[x]]] * v[c[x]];
vis[c[x]]--;
}
else {
vis[c[x]]++;
ans += w[vis[c[x]]] * v[c[x]];
}
}
void Time( int x ) {
int now = ch[x].wh;
if( tot[now] == 1 ) {
Work(now);
swap( c[now], ch[x].val );
Work(now);
}
else swap( c[now], ch[x].val );
}
void solve() {
int l = 1, r = 0, t = 0;
sort(q + 1, q + q1 + 1, cmp );
for ( int i = 1; i <= q1; ++ i ) {
while( r < q[i].r ) Work( b[++r] );
while( r > q[i].r ) Work( b[r--] );
while( l < q[i].l ) Work( b[l++] );
while( l > q[i].l ) Work( b[--l] );
while( t < q[i].time ) Time( ++t );
while( t > q[i].time ) Time( t-- );
num[q[i].id] = ans;
if( q[i].lca ) num[q[i].id] += w[vis[c[q[i].lca]] + 1] * v[c[q[i].lca]];
}
for ( int i = 1; i <= q1; ++ i ) {
printf("%lld\n", num[i]);
}
}
signed main()
{
input(); dfs1( 1, 1 );
init(); solve();
return 0;
}
by Soulist @ 2019-03-05 10:59:37
开
by NaCly_Fish @ 2019-03-05 11:36:38
@Light_Poet 我觉得2L说的挺对的,另外您为什么还要转到序列上做呢?
可以直接在树上分块然后莫队啊qwq
by lmAKf @ 2019-03-05 11:45:40
@NaCly_Fish 蒟蒻太弱不会树分块
by NaCly_Fish @ 2019-03-05 11:48:26
@Light_Poet 可是窝感觉这题树分块更好写。。qaq