QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303337 | #4924. 蜘蛛爬树 | zyc070419 | 0 | 166ms | 52892kb | C++14 | 5.6kb | 2024-01-12 08:41:21 | 2024-01-12 08:41:22 |
Judging History
answer
#include <bits/stdc++.h>
#define INF 9e18
#define inf 1e9
#define ll long long
using namespace std;
const int N = 2e5 + 3;
inline int read() {
char ch = getchar(); int x = 0;
while (!isdigit(ch)) {ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
return x;
}
inline ll Read() {
char ch = getchar(); ll x = 0;
while (!isdigit(ch)) {ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
return x;
}
struct Node {
int id, y, d;
Node(int a = 0, int b = 0, int c = 0) {id = a; y = b; d = c;}
};
int n, m, T, a[N];
int depth[N], fa[N], sz[N], son[N], col[N];
int sum, root, mx[N];
ll dis[N], now[N], ans[N];
vector< pair<int, ll> > g[N];
bitset<N> vis;
vector<Node> vec[N], mem[N], nxt;
struct LC_Segment_Tree {
struct node {
int ls, rs, k; ll b;
node() {ls = rs = k = 0; b = INF;}
};
vector<node> t;
int tot, fir;
inline ll f(int x, int k, ll b) {return 1ll * k * x + b;}
void update(int &rt, int l, int r, int k, ll b) {
if (!rt) {
rt = ++tot; t.push_back(node());
t[tot].k = k; t[tot].b = b;
return;
}
int mid = (l + r) >> 1;
if (f(mid, k, b) < f(mid, t[rt].k, t[rt].b)) swap(k, t[rt].k), swap(b, t[rt].b);
if (l == r) return;
if (f(l, k, b) < f(l, t[rt].k, t[rt].b)) update(t[rt].ls, l, mid, k, b);
if (f(r, k, b) < f(r, t[rt].k, t[rt].b)) update(t[rt].rs, mid + 1, r, k, b);
}
ll query(int rt, int l, int r, int x) {
if (!rt) return INF;
if (l == r) return f(x, t[rt].k, t[rt].b);
int mid = (l + r) >> 1; ll res = f(x, t[rt].k, t[rt].b);
if (x <= mid) res = min(res, query(t[rt].ls, l, mid, x));
else res = min(res, query(t[rt].rs, mid + 1, r, x));
return res;
}
void init() {t.clear(); t.push_back(node()); t.push_back(node()); tot = fir = 1;}
}t[2];
void dfs1(int x, int pa, int d, int anc) {
t[0].update(t[0].fir, 1, inf, a[x], dis[x] + dis[x]);
depth[x] = d; fa[x] = pa; sz[x] = 1; col[x] = anc;
for (auto tmp : g[x]) {
int y = tmp.first;
if (y == pa || vis[y]) continue;
dis[y] = dis[x] + tmp.second;
dfs1(y, x, d + 1, anc == -1 ? y : anc);
sz[x] += sz[y];
if (sz[y] > sz[son[x]]) son[x] = y;
}
}
void dfs2(int x) {
for (auto tmp : vec[x]) {
int y = tmp.y;
if (col[x] == col[y]) {
int id = tmp.id, d = tmp.d;
ans[id] = min(ans[id], t[0].query(t[0].fir, 1, inf, d) + dis[x] + dis[y]);
nxt.push_back(tmp);
}
else mem[x].push_back(tmp);
}
swap(vec[x], nxt);
nxt.clear();
for (auto tmp : g[x]) {
int y = tmp.first;
if (y == fa[x] || vis[y]) continue;
dfs2(y);
}
}
void update(int x, ll val) {
t[1].update(t[1].fir, 1, inf, a[x], dis[x] + dis[x] - val - val);
for (auto tmp : g[x]) {
int y = tmp.first;
if (y == fa[x] || vis[y]) continue;
update(y, val);
}
}
void query(int x) {
for (auto tmp : mem[x]) {
int y = tmp.y, id = tmp.id, d = tmp.d;
ans[id] = min(ans[id], t[1].query(t[1].fir, 1, inf, d) + dis[x] + dis[y]);
}
for (auto tmp : g[x]) {
int y = tmp.first;
if (y == fa[x] || vis[y]) continue;
query(y);
}
}
void dfs3(int x) {
for (auto tmp : g[x]) {
int y = tmp.first;
if (y == fa[x] || y == son[x] || vis[y]) continue;
update(y, dis[x]);
}
t[1].update(t[1].fir, 1, inf, a[x], dis[x]);
for (auto tmp : mem[x]) {
int y = tmp.y, id = tmp.id, d = tmp.d;
ans[id] = min(ans[id], t[1].query(t[1].fir, 1, inf, d) + dis[x] + dis[y]);
}
mem[x].clear();
for (auto tmp : g[x]) {
int y = tmp.first;
if (y == fa[x] || y == son[x] || vis[y]) continue;
query(y);
}
if (son[x]) dfs3(son[x]);
else t[1].init();
for (auto tmp : g[x]) {
int y = tmp.first;
if (y == fa[x] || y == son[x] || vis[y]) continue;
dfs3(y);
}
}
void get_rt(int x, int pa) {
sz[x] = 1; mx[x] = 0;
for (auto tmp : g[x]) {
int y = tmp.first;
if (y == pa || vis[y]) continue;
get_rt(y, x);
sz[x] += sz[y];
mx[x] = max(mx[x], sz[y]);
}
mx[x] = max(mx[x], n - sz[x]);
if (mx[x] < mx[root]) root = x;
}
void work(int x) {
t[0].init(); t[1].init();
dis[x] = 0; vis[x] = 1;
dfs1(x, x, 1, -1); dfs2(x); dfs3(x);
for (auto tmp : g[x]) {
int y = tmp.first;
if (vis[y]) continue;
sum = sz[y]; mx[root = 0] = inf; get_rt(y, 0); work(root);
}
}
int main() {
n = read(); m = read(); T = read();
for (int i = 1; i <= n; ++i) a[i] = read();
for (int i = 1; i < n; ++i) {
int x = read(), y = read(); ll v = Read();
g[x].push_back(make_pair(y, v));
g[y].push_back(make_pair(x, v));
}
for (int i = 1; i <= T; ++i) {
ll xx = Read() - 1, yy = Read() - 1;
int x = xx % n + 1, y = yy % n + 1, d = xx / n - yy / n;
if (d < 0) d = -d;
// printf("[%d %d %d]\n", x, y, d);
vec[x].push_back(Node(i, y, d));
vec[y].push_back(Node(i, x, d));
ans[i] = INF;
}
sum = n; mx[root = 0] = inf; get_rt(1, 0); work(root);
for (int i = 1; i <= T; ++i) printf("%lld\n", ans[i]);
return 0;
}
//考虑点分治套点分治,递归变成子任务6
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 6ms
memory: 27664kb
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:
6142716735003 10810061158998 3440559556796 5436318324202 4494351652028 1576462196548 7926080341913 1858310964948 5113840012487 6974341695088 4707554029530 5282660377018 4642442882498 3178392425395 2369520344240 965219608895 14377989990517 9688279832092 4259573672967 7350303186770 8380301414777 12334...
result:
wrong answer 1st lines differ - expected: '6130845802041', found: '6142716735003'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #20:
score: 0
Time Limit Exceeded
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
Time Limit Exceeded
Test #28:
score: 0
Time Limit Exceeded
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
Wrong Answer
Test #36:
score: 0
Wrong Answer
time: 166ms
memory: 52892kb
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:
1352416884531 1380463318391 923920163167 1525224977139 1405019709299 869269749781 715671043328 876194052054 1358007874327 127994985855 1230162209719 1532026808855 611656467332 1023855959729 414792924571 1316679734677 827308370883 1265411315424 821484360433 1051517948640 837509712760 582943943131 457...
result:
wrong answer 258th lines differ - expected: '1251426204676', found: '1251613120990'
Subtask #6:
score: 0
Time Limit Exceeded
Test #43:
score: 0
Time Limit Exceeded
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%