Sindar's Art Exhibition

HDU - 6612

题意:带着x个艺术品在树上从s到t,每到一个地方会给出si个艺术品,收获si * fi的价值, 问你总的价值是多少,有多个询问

题解:容易得到\(ans = \sum_{i = s}^t f[i] * (x - \sum_{j = s}^{i - 1} s[i])\) , 进行树链剖分后,用\(sum_s\)代表\(s_i\)前缀和,考虑从s往上走, 得到 \(ans = \sum_{i = s}^t f[i] * (x - (sum_s[s] - sum_s[i]))\) , 也就是 \(ans = \sum_{i = s}^t f[i] *(x - sum_s[s]) + \sum_{i = s}^t f[i]* sum_s[i]\) , 考虑从\(Point_i\)到t,也就是从t往前回溯,易得到 \(ans = \sum_{i = s}^t f[i] * (x - (sum_s[i - 1] - sum_s[s - 1]))\) , 也就是 \(ans = \sum_{i = s}^t f[i] *(x + sum_s[s - 1]) - \sum_{i = s}^t f[i]* sum_s[i - 1]\)

现在能处理每段了,考虑合并的时候,从s往上走时,每一段都会去掉之前送出的礼物和乘当前的友好值,由f往上回溯时,每一段都会减去之前友好值和乘送出的礼物,如此合并时减去冗余部分即可,直到走到lca。每段路的合并都是减去前路的所有礼物和乘以后路的友好值和

需要注意的是输入会超过模数,我之前直接输入到modInt的里面,出现了绕过取模的情况,浪费了我24h 😱

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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*************************************************************************
> File Name: solve.cpp
> Author: liupo
> Mail: lanzongwei@gmail.com
> Created Time: 2020-05-20 21:44:26
************************************************************************/

#define GOODOJ
#define SYNC 0

#ifdef GOODOJ
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#include <chrono>
#include <random>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#else
#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
#include <limits>
#include <cassert>
#include <sstream>
#include <iterator>
#include <functional>
#endif
using namespace std;

#define endl '\n'
#define fep(i,b,e) for(int i=(b);i<(e);++i)
#define rep(i,x) for(int i=0;i<(x);++i)
#define rap(i,x) for(auto& i : (x))
#define seg(t) (t).begin(), (t).end()
#define ep emplace_back
#define mkp make_pair
#define mkt make_tuple
#define qxx(i,x) for(int i = head[x]; ~i; i = node[i].nex)
#define F first
#define S second
#define lowbit(x) ((-(x))&(x))
#define RE register
#define getchar() getchar_unlocked()
#ifdef DEBUG
void err(istream_iterator<string>){}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ' ';
err(++it, args...);
}
#define debug(args...) {string _s=#args;replace(seg(_s),',',' ');\
cerr<<"DEBUG:";istringstream it(_s);\
err(istream_iterator<string>(it), args);cerr<<endl;}
#else
#define debug(...)
#endif

template<typename T> inline bool cmax(T& a,const T& b) {return a<b?a=b,1:0;}
template<typename T> inline bool cmin(T& a,const T& b) {return a>b?a=b,1:0;}

#ifdef GOODOJ
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
typedef __gnu_pbds::priority_queue<int> pq;
#endif
typedef std::string str;
typedef long long ll;
typedef double db;
typedef pair<int, int> pa;
#define state(x) (1<<(x))

const double P = acos(-1.0), eps = 1e-9;
struct point { db x ,y;};
inline int sign(db a) {return a < -eps ? -1 : a > eps;}
#define dot(p1,p2,p3) ((p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y))
#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))
#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))

const int Ma = 1e5 + 100, inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;

namespace InMod {
vector<int> phi;
void gen(int n) {
phi.emplace_back(1);
phi.emplace_back(1);
for (int i = 2; i <= n; ++i) phi.emplace_back((mod - mod / i) * phi[mod % i] % mod);
}

struct Modint {
ll x;
Modint(ll x=0) : x((x%mod+mod)%mod){}
Modint operator -() const {return Modint(-x);}
Modint& operator += (const Modint b) {
if ((x += b.x) >= mod) x -= mod;
return *this;
}
Modint& operator -= (const Modint b) {
if ((x += mod - b.x) >= mod) x -= mod;
return *this;
}
Modint& operator *= (const Modint b) {
(x *= b.x) %= mod;
return *this;
}
Modint& operator /= (const Modint b) {return *this *= b.inv();}
Modint operator + (const Modint b) const {return Modint(*this) += b;}
Modint operator - (const Modint b) const {return Modint(*this) -= b;}
Modint operator * (const Modint b) const {return Modint(*this) *= b;}
Modint operator / (const Modint b) const {return Modint(*this) /= b;}
Modint pow(ll t) const {
if (!t) return 1;
Modint a = pow(t>>1);
a *= a;
if (t & 1) a *= *this;
return a;
}
Modint inv() const {return pow(mod - 2);}
Modint inv(const int& n) const {
if (phi.empty()) gen(n);
return phi[x];
}
};
}

typedef InMod::Modint mint;

istream& operator >> (istream& in, mint& a) {return in >> a.x;}
ostream& operator << (ostream& ou, const mint& a) {return ou << a.x;}

struct Bit {
mint bit[Ma]; int ma;
void init(int n) {
memset(bit, 0, sizeof(mint) * (n + 1));
ma = n;
}
void add(int pos, mint val) {
while (pos <= ma) bit[pos] += val, pos += lowbit(pos);
}
mint sum(int pos) {
mint ans = 0;
while (pos > 0) ans += bit[pos], pos -= lowbit(pos);
return ans;
}
mint range(int l, int r) {
return sum(r) - sum(l - 1);
}
} fu, fd, si, fi;

ll f[Ma], y[Ma];
vector<int> g[Ma];

int dfn[Ma], rnk[Ma], fa[Ma], top[Ma], son[Ma], dep[Ma], siz[Ma], cnt;

void dfs1(int u) {
son[u] = -1;
siz[u] = 1;
rap (i, g[u]) if (!dep[i]) {
dep[i] = dep[u] + 1; fa[i] = u;
dfs1(i); siz[u] += siz[i];
if (!~son[u]) son[u] = i;
else if (siz[i] > siz[son[u]]) son[u] = i;
}
}

void dfs2(int u, int v) {
top[u] = v;
dfn[++cnt] = u;
rnk[u] = cnt;
if (!~son[u]) return ;
dfs2(son[u], v);
rap (i, g[u]) if (i != son[u] and i != fa[u])
dfs2(i, i);
}

void init(int n) {
memset(dep, 0, sizeof(int) * (n + 1));
dep[1] = 1; cnt = 0;
dfs1(1);
dfs2(1, 0);
fu.init(n), fd.init(n), si.init(n), fi.init(n);
for (int i = 1; i <= n; ++i)
si.add(i, y[dfn[i]]), fi.add(i, f[dfn[i]]),
fu.add(i, si.sum(i - 1) * f[dfn[i]]),
fd.add(i, si.sum(i) * f[dfn[i]]);
}

mint querry(int s, int t, mint x) {
mint df, ds, ans; int now = 0;
while (top[s] != top[t]) {
//debug(s, t, top[s], top[t]);
if (dep[top[s]] < dep[top[t]]) swap(s, t), now = !now;
if (!now) {
ans += fi.range(rnk[top[s]], rnk[s]) * (x - si.sum(rnk[s])) + fd.range(rnk[top[s]], rnk[s]);
ans -= ds * fi.range(rnk[top[s]], rnk[s]);
ds += si.range(rnk[top[s]], rnk[s]);
} else {
ans += fi.range(rnk[top[s]], rnk[s]) * (x + si.sum(rnk[top[s]] - 1)) - fu.range(rnk[top[s]], rnk[s]);
ans -= si.range(rnk[top[s]], rnk[s]) * df;
df += fi.range(rnk[top[s]], rnk[s]);
}
s = fa[top[s]];
}
if (dep[s] < dep[t]) swap(s, t), now = !now;
//debug(s, t, ans, now, df, ds);
if (!now) {
ans += fi.range(rnk[t], rnk[s]) * (x - si.sum(rnk[s])) + fd.range(rnk[t], rnk[s]);
ans -= ds * fi.range(rnk[t], rnk[s]);
ds += si.range(rnk[t], rnk[s]);
} else {
ans += fi.range(rnk[t], rnk[s]) * (x + si.sum(rnk[t] - 1)) - fu.range(rnk[t], rnk[s]);
ans -= si.range(rnk[t], rnk[s]) * df;
df += fi.range(rnk[t], rnk[s]);
}
//assert((ans - df * ds).x >= 0 and (ans - df * ds).x < mod);
return ans - df * ds;
}

signed main() {
#if SYNC==1
ios::sync_with_stdio(false);
cin.tie(0);
#endif
int T; scanf("%d", &T);
while (T--) {
int n; scanf("%d", &n);
fep (i, 1, n + 1) scanf("%lld", f + i);
fep (i, 1, n + 1) scanf("%lld", y + i);
fep (i, 1, n + 1) g[i].clear();
rep (i, n - 1) {
int u, v; scanf("%d%d", &u, &v);
g[u].ep(v), g[v].ep(u);
}
int q; scanf("%d", &q);
init(n);
while (q--) {
int aim; cin >> aim;
if (aim == 1) {
int s, t; mint x; scanf("%d%d%lld", &s, &t, &x.x);
printf("%lld\n", querry(s, t, x).x);
} else {
int c; ll V; scanf("%d%lld", &c, &V);
mint v(V);
mint val = v - f[c];
f[c] = v.x;
fi.add(rnk[c], val);
fu.add(rnk[c], val * si.sum(rnk[c] - 1));
fd.add(rnk[c], val * si.sum(rnk[c]));
//for (int i = 1; i <= n; i++)
// debug(fi.sum(i), fu.sum(i), fd.sum(i), si.sum(i), i);
}
}
}

return 0;
}

Comments

2020-05-26

⬆︎TOP