[bzoj] 3052: [wc2013]糖果公园

题意

大家直接看题目好了

传送门

分析

莫队算法

代码

原谅我用快读卡过去>_<

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
#include <cstdio>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
int CH , NEG ;

char chin[10000000],chout[5000000],*pi=chin,*po=chout;

template <typename TP>
void read(TP &ret){
trueret=0;
truewhile(*pi<48) pi++;
truewhile(*pi>47) ret=ret*10+*pi++-48;
}

template <typename TP>
void print(TP x){
truestatic int tim=0;
truestatic char p[30];
truewhile(x) p[++tim]=x%10+'0',x/=10;
truewhile(tim) *po++=p[tim--];
true*po++='\n';
}
typedef long long ll ;

#include <algorithm>
#include <cmath>

#define maxn 100010LL
#define maxm 100010LL
#define maxq 100010LL
#define maxk 18LL

//struct FST { int to , next ; } e[maxm<<1] ;
int next[maxm<<1] , to[maxm<<1] ;
int star[maxn] = {0} , tote = 1 ;
void AddEdge(int u , int v) { ++tote ; to[tote] = v ; next[tote] = star[u] ; star[u] = tote ; }

int n , m , timnow ;
ll ansnow ;
int cntblc , szblc , idx , top ;

int col[maxn] , cnt[maxn] = {0} , last[maxn] ;
int dfn[maxn] , sta[maxn] , belong[maxn] , dep[maxn] , size[maxn] , par[maxn][maxk] ;
ll ans[maxq] ;
bool vis[maxn] = {0} ;

struct QUERY {
trueint u , v , lca , id , tim ;
truebool operator < (const QUERY& b) const {
truetrueif (belong[u]!=belong[b.u]) return belong[u]<belong[b.u] ;
truetrueif (belong[v] != belong[b.v]) return belong[v]<belong[b.v] ;
truetruereturn id < b.id ;
true}
} q[maxq] ;

struct CHANGE {
trueint pos , from , to ;
} c[maxq] ;

inline void dfs(int u , int fa) {
int v , p ;
truesta[++top] = u ;
truepar[u][0] = fa ;
truedfn[u] = ++idx ;
truedep[u] = dep[fa]+1 ;
truesize[u] = 0 ;
truefor (p = star[u] ; p ; p = next[p])
truetrueif (v=to[p] , v!=fa) {
truetruetruedfs(v , u) ;
truetruetruesize[u] += size[v] ;
truetruetrueif (size[u] >= szblc) {
truetruetruetruesize[u] = 0 ;
truetruetruetruecntblc ++ ;
truetruetruetruewhile (sta[top] != u) belong[sta[top--]] = cntblc ;
truetruetrue}
truetrue}
truesize[u] ++ ;
}

inline void ST() {
int i , k ;
trueRep (k,1,maxk-1)
truetrueRep (i,1,n)
truetruetruepar[i][k] = par[par[i][k-1]][k-1] ;
}

inline int LCA(int u , int v) {
trueint k ;
trueif (dep[u] < dep[v]) std::swap(u,v) ;
trueRev (k,maxk-1,0)
truetrueif (dep[par[u][k]] >= dep[v]) u = par[u][k] ;
trueif (u == v) return u ;
trueRev (k,maxk-1,0)
truetrueif (par[u][k] != par[v][k])
truetruetrueu = par[u][k] , v = par[v][k] ;
truereturn par[u][0] ;
}

ll v[maxn] , w[maxm] ;

inline void Reverse(int u) {
trueif (vis[u]) { vis[u]=false;ansnow-=v[col[u]]*w[cnt[col[u]]--]; }
trueelse { vis[u]=true;ansnow+=v[col[u]]*w[++cnt[col[u]]]; }
}

inline void Modify(int u , int c) {
trueif (vis[u]) {
truetrueReverse(u) ;
truetruecol[u] = c ;
truetrueReverse(u) ;
true} else col[u] = c ;
}

inline void MoveTo(int u,int v) {
truewhile (u != v)
truetrueif (dep[u] > dep[v]) Reverse(u) , u = par[u][0] ;
truetrueelse Reverse(v) , v = par[v][0] ;
}

inline void GetAns(int i) {
trueReverse(q[i].lca) ;
trueans[q[i].id] = ansnow ;
trueReverse(q[i].lca) ;
}


#include <ctime>

int main() {
int i , a , b , Q , cmd ;
true#define READ
true#ifdef READ
truetruefreopen("park.in" ,"r",stdin ) ;
truetruefreopen("park.out","w",stdout) ;
true#endif
truefread(pi,1,10000000,stdin);
trueread(n) , read(m) , read(Q) ;
trueszblc = (int)pow(n,2.0/3.0)*.5 ;
trueRep (i,1,m) read(v[i]) ;
trueRep (i,1,n) read(w[i]) ;
trueRep (i,2,n) {
truetrueread(a) , read(b) ;
truetrueAddEdge(a,b) , AddEdge(b,a) ;
true}
trueRep (i,1,n) read(col[i]) , last[i] = col[i] ;
truecntblc = idx = dep[0] = ansnow = top = 0 ;
truedfs(1,0) ;
truecntblc ++ ;
truewhile (top) belong[sta[top--]] = cntblc ;
trueST() ;
trueint totq = 0 , totc = 0 ;
trueRep (i,1,Q) {
truetrueread(cmd) , read(a) , read(b) ;
truetrueif (cmd) {
truetruetruetotq ++ ;
truetruetrueif (belong[a] > belong[b]) std::swap(a,b) ;
truetruetrueq[totq].lca = LCA(a,b) ;
truetruetrueq[totq].id = totq , q[totq].u = a , q[totq].v = b ;
truetruetrueq[totq].tim = totc ;
truetrue} else {
truetruetruetotc ++ ;
truetruetruec[totc].pos = a ;
truetruetruec[totc].from = last[a] ;
truetruetruec[totc].to = last[a] = b ;
truetrue}
true}
truetimnow = 1 ;
truestd::sort(q+1,q+totq+1) ;
truewhile (timnow <= q[1].tim) Modify(c[timnow].pos,c[timnow].to) , ++timnow ;
trueMoveTo(q[1].u,q[1].v) ;
trueGetAns(1) ;
trueRep (i,2,totq) {
truetruewhile (timnow <= q[i].tim) Modify(c[timnow].pos,c[timnow].to) , ++timnow ;
truetruewhile (timnow-1 > q[i].tim) Modify(c[timnow-1].pos,c[timnow-1].from) , --timnow ;
truetrueMoveTo(q[i-1].u,q[i].u) ;
truetrueMoveTo(q[i-1].v,q[i].v) ;
truetrueGetAns(i) ;
true}
trueRep (i,1,totq) print(ans[i]) ;
truefwrite(chout,1,po-chout,stdout);

true//printf("%.2lf\n",clock()/1000.0) ;
true#ifdef READ
truetruefclose(stdin) ; fclose(stdout) ;
true#else
truetruegetchar() ; getchar() ;
true#endif
truereturn 0 ;
}

热评文章