QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#108750 | #4924. 蜘蛛爬树 | 1234567890 | 0 | 8ms | 24016kb | C++14 | 6.9kb | 2023-05-26 15:55:36 | 2023-05-26 15:55:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define inf (ll)1e18
#define pii pair <ll, ll>
#define fr first
#define se second
const ll mod = 1e9 + 7;
char buf[1 << 21], *p1 = buf, *p2 = buf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 18, stdin), p1 == p2) ? EOF : *p1++)
inline ll read() {
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') f =((ch == '-') ? -1 : f), ch = getchar();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
inline void write(ll x) {
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
inline ll Abs(ll x) {
if(x > 0) return x;
return -x;
}
ll cnt;
struct kk {
ll k, b;
kk() {}
kk(ll A, ll B) {
k = A, b = B;
}
inline ll val(ll x) {
return x * k + b;
}
}p[20000005];
struct Li_Chao {
ll tot;
struct st {
ll l, r, p;
}t[4000005];
inline void clear() {
for(ll i = 1; i <= tot; i++) t[i].l = t[i].r = t[i].p = 0;
tot = 0;
}
inline void insert(ll &id, ll pre, ll l, ll r, ll x) {
id = ++tot;
t[id] = t[pre];
if(!t[id].p) {
t[id].p = x;
return ;
}
if(l == r) {
if(p[t[id].p].val(l) > p[x].val(l)) t[id].p = x;
return ;
}
ll mid = (l + r) >> 1;
if(p[t[id].p].val(mid) <= p[x].val(mid)) {
if(p[t[id].p].k < p[x].k) insert(t[id].l, t[pre].l, l, mid, x);
else insert(t[id].r, t[pre].r, mid + 1, r, x);
}
else {
swap(t[id].p, x);
if(p[t[id].p].k < p[x].k) insert(t[id].l, t[pre].l, l, mid, x);
else insert(t[id].r, t[pre].r, mid + 1, r, x);
}
}
inline ll Merge(ll u, ll v, ll l, ll r) {
if(!u || !v) return u + v;
if(l == r) {
ll id = ++tot;
if(p[t[u].p].val(l) < p[t[v].p].val(l)) t[id].p = t[u].p;
else t[id].p = t[v].p;
return id;
}
ll mid = (l + r) >> 1, id = ++tot;
if(p[t[u].p].val(mid) > p[t[v].p].val(mid)) swap(u, v);
t[id].p = t[u].p;
t[id].l = Merge(t[u].l, t[v].l, l, mid);
t[id].r = Merge(t[u].r, t[v].r, mid + 1, r);
insert(id, id, l, r, t[v].p);
return id;
}
inline ll get_ans(ll id, ll l, ll r, ll x) {
if(!id) return inf;
if(l == r) return p[t[id].p].val(x);
ll Ans = p[t[id].p].val(x);
ll mid = (l + r) >> 1;
if(x <= mid) Ans = min(Ans, get_ans(t[id].l, l, mid, x));
else Ans = min(Ans, get_ans(t[id].r, mid + 1, r, x));
return Ans;
}
}T;//min
ll n, m, Q;
struct st {
ll v, e;
st() {}
st(ll A, ll B) {
v = A, e = B;
}
};
struct Query {
ll u, v, x;
ll lca;
}q[200005];
vector <st> G[200005];
ll a[200005], ans[200005];
namespace solve_single {
vector <ll> Qu[200005];
ll vis[200005], siz[200005];
ll Mn, Rt;
void get_rt(ll x, ll fa, ll all) {
ll mx = 0;
siz[x] = 1;
for(auto y : G[x]) {
if(vis[y.v] || y.v == fa) continue;
get_rt(y.v, x, all);
siz[x] += siz[y.v];
mx = max(mx, siz[y.v]);
}
mx = max(mx, all - siz[x]);
if(mx < Mn) Mn = mx, Rt = x;
}
ll tot;
pii seq[200005];
inline void get_path(ll x, ll fa, ll d) {
seq[++tot] = mp(x, d);
for(auto y : G[x]) {
if(vis[y.v] || y.v == fa) continue;
get_path(y.v, x, d + y.e);
}
}
void dfs(ll x, ll Siz) {
Mn = Rt = inf;
get_rt(x, 0, Siz);
x = Rt, vis[x] = 1, get_rt(x, 0, Siz);
cnt = 0;
T.clear();
ll LRt = 0;
for(auto y : G[x]) {
if(vis[y.v]) continue;
tot = 0;
get_path(y.v, x, y.e);
for(ll i = 1; i <= tot; i++) for(auto id : Qu[seq[i].fr]) ans[id] = min(ans[id], T.get_ans(LRt, 0, (ll)1e9, q[id].x) + 2 * seq[i].se);
for(ll i = 1; i <= tot; i++) p[++cnt] = kk(a[seq[i].fr], 2 * seq[i].se), T.insert(LRt, LRt, 0, (ll)1e9, cnt);
}
for(auto y : G[x]) if(!vis[y.v]) dfs(y.v, siz[y.v]);
}
};
const ll NN = 200005;
struct Tree {
ll siz[NN], son[NN], dep[NN], dis[NN], Fa[NN];
void dfs(ll x, ll fa) {
dep[x] = dep[fa] + 1;
Fa[x] = fa;
siz[x] = 1;
for(auto y : G[x]) {
if(y.v == fa) continue;
dis[y.v] = dis[x] + y.e;
dfs(y.v, x);
siz[x] += siz[y.v];
if(!son[x] || siz[y.v] > siz[son[x]]) son[x] = y.v;
}
}
ll top[NN];
void dfs2(ll x) {
ll fa = Fa[x];
if(son[fa] == x) top[x] = top[fa];
else top[x] = x;
if(son[x]) dfs2(son[x]);
for(auto y : G[x]) if(!top[y.v]) dfs2(y.v);
}
inline ll LCA(ll x, ll y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = Fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
return x;
}
inline ll Dis(ll x, ll y) {
return dis[x] + dis[y] - 2 * dis[LCA(x, y)];
}
inline void init() {
dfs(1, 0), dfs2(1);
}
vector <ll> P[200005], oth[200005], I;
void get_oth(ll whi, ll x, ll ban) {
oth[whi].push_back(x);
for(auto y : G[x]) {
if(y.v == Fa[x] || y.v == ban) continue;
get_oth(whi, y.v, ban);
}
}
inline void add(ll u, ll v, ll id) {
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u, v);
P[u].push_back(id);
u = Fa[top[u]];
}
if(dep[u] > dep[v]) swap(u, v);
P[v].push_back(id);
}
inline void solve_chain() {
for(ll i = 1; i <= Q; i++) add(q[i].u, q[i].v, i);
for(ll i = 1; i <= n; i++) {
if(top[i] == i) I.push_back(i);
get_oth(i, i, son[i]);
}
for(auto x : I) {
cnt = 0, T.clear();
ll LRt = 0;
ll now = x;
while(now) {
for(auto y : oth[now]) p[++cnt] = kk(a[y], 2 * (dis[y] - dis[now])), T.insert(LRt, LRt, 0, (ll)1e9, cnt);
for(auto y : P[now]) ans[y] = min(ans[y], T.get_ans(LRt, 0, (ll)1e9, q[y].x));
now = son[now];
}
}
}
}Tr;
namespace solve_subtree {
ll rt[200005], dis[200005];
inline void dfs(ll x, ll fa) {
p[++cnt] = kk(a[x], dis[x]);
T.insert(rt[x], rt[x], 0, (ll)1e9, cnt);
for(auto y : G[x]) {
if(y.v == fa) continue;
dis[y.v] = dis[x] + y.e;
dfs(y.v, x);
rt[x] = T.Merge(rt[x], rt[y.v], 0, (ll)1e9);
}
}
inline void solve() {
cnt = 0, T.clear();
dfs(1, 0);
for(ll i = 1; i <= Q; i++) {
ans[i] = min(ans[i], T.get_ans(rt[q[i].u], 0, (ll)1e9, q[i].x) - dis[q[i].u]);
ans[i] = min(ans[i], T.get_ans(rt[q[i].v], 0, (ll)1e9, q[i].x) - dis[q[i].v]);
}
}
};
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
n = read(), m = read(), Q = read();
for(ll i = 1; i <= n; i++) a[i] = read();
for(ll i = 1; i < n; i++) {
ll u = read(), v = read(), e = read();
G[u].push_back(st(v, e));
G[v].push_back(st(u, e));
}
Tr.init();
for(ll i = 1; i <= Q; i++) {
ll u = read(), v = read();
ll u1 = (u - 1) / n + 1, v1 = (v - 1) / n + 1;
ll u2 = u - (u1 - 1) * n, v2 = v - (v1 - 1) * n;
q[i].u = u2, q[i].v = v2, q[i].x = Abs(u1 - v1);
q[i].lca = Tr.LCA(u2, v2);
solve_single :: Qu[q[i].lca].push_back(i);
}
memset(ans, 0x3f, sizeof ans);
solve_single :: dfs(1, n);
solve_subtree :: solve();
Tr.solve_chain();
for(ll i = 1; i <= Q; i++) write(ans[i] + Tr.Dis(q[i].u, q[i].v)), putchar('\n');
return 0;
}
/*
*/
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 8ms
memory: 24016kb
input:
97 99 1000 763118300 558295517 80676328 362318619 473105413 468927175 311496507 936935430 176032966 304576040 308583326 681580095 549392233 518152994 829474320 751189715 542810029 587869244 878512027 530678371 832766129 535259635 799122942 596982955 884696876 605325753 495661541 506105495 561218313 ...
output:
6130845802041 10806543345556 3435147536116 5423542374253 4445399957621 1527525769886 7904973461261 1843969110146 5112206409383 6968388472340 4706970599850 5265970376578 4629925382168 3171356798511 2331646415266 953140619366 14353365296423 9672855573154 4251455641947 7333255321628 8375971798233 12317...
result:
wrong answer 2nd lines differ - expected: '10806758605627', found: '10806543345556'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Runtime Error
Test #20:
score: 0
Runtime Error
input:
200000 20 200000 679416469 548913625 468159997 137709609 883140368 682558021 473174374 400192350 124143873 825920417 372498686 851213321 822264481 78195915 5427143 453304163 233551905 810910186 810046144 52603791 282167184 385032797 81387991 747194790 917579656 585184539 12659388 249218417 158295502...
output:
result:
Subtask #4:
score: 0
Runtime Error
Test #28:
score: 0
Runtime Error
input:
200000 1000000000 200000 28270302 472359923 262785485 923929785 393684160 761485431 72038469 116384740 426631758 437934930 610834083 455314140 734276543 903544756 220163018 756113376 732404264 947339315 109784905 625451008 794076307 818852312 758007217 124450858 674924509 311761991 507260538 7032362...
output:
result:
Subtask #5:
score: 0
Runtime Error
Test #36:
score: 0
Runtime Error
input:
199918 1000000000 199903 1496 2382 3896 3664 1177 1627 2821 4200 3074 3783 2069 4403 629 2610 4991 4074 3033 2798 4333 3501 3667 3064 663 2821 2818 458 2950 4020 2665 3578 63 4855 4941 3492 2423 4510 1489 1018 4829 1912 3133 3174 309 287 2909 4102 4296 4526 3170 3683 4960 4863 4738 2927 2405 3600 44...
output:
result:
Subtask #6:
score: 0
Runtime Error
Test #43:
score: 0
Runtime Error
input:
200000 1000000000 200000 81882094 47220813 43282454 17633207 52769165 4830673 31396360 64793163 9174729 36727563 71268262 24662923 40146030 1430053 62926106 30042905 1330107 81817720 98841078 87766129 51155045 23216268 79896310 66625868 87854925 42976560 86542933 28336449 34932261 19698851 584453 90...
output:
result:
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%