QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#303560#4924. 蜘蛛爬树zyc0704190 919ms169308kbC++148.8kb2024-01-12 18:40:182024-01-12 18:40:19

Judging History

你现在查看的是最新测评结果

  • [2024-01-12 18:40:19]
  • 评测
  • 测评结果:0
  • 用时:919ms
  • 内存:169308kb
  • [2024-01-12 18:40:18]
  • 提交

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, d;
    Node(int a = 0, int b = 0) {id = a; d = b;}
};
struct NOde {
    int id, l, r, d;
    NOde(int a = 0, int b = 0, int c = 0, int D = 0) {id = a; l = b; r = c; d = D;}
};
struct NODe {
    int id, d, x;
    NODe(int a = 0, int b = 0, int c = 0) {id = a; d = b; x = c;}
};
int n, m, T, a[N];
int depth[N], fa[N], sz[N], son[N], id[N], num, top[N];
ll dis[N], now[N], ans[N], len[N];
vector< pair<int, ll> > g[N];
vector<Node> mem[N], vec[N];
vector<NODe> fuc[N];
vector<NOde> ex[N];
struct LC_Segment_Tree {
    struct node {
        int ls, rs, k; ll b;
        node() {ls = rs = k = 0; b = INF;}
    }t[N * 20];
    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[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() {
        for (int i = 0; i <= tot; ++i) t[i] = node();
        tot = fir = 0;
    }
}t;
struct Segment_Tree {
    #define ls (rt << 1)
    #define rs (rt << 1 | 1)

    int root[N << 2];

    void update(int rt, int l, int r, int pos, int k, ll b) {
        t.update(root[rt], 0, inf, k, b);
        if (l == r) return;
        int mid = (l + r) >> 1;
        if (pos <= mid) update(ls, l, mid, pos, k, b);
        else update(rs, mid + 1, r, pos, k, b);
    }

    ll query(int rt, int l, int r, int ql, int qr, int x) {
        if (ql <= l && r <= qr) return t.query(root[rt], 0, inf, x);
        int mid = (l + r) >> 1; ll res = INF;
        if (ql <= mid) res = min(res, query(ls, l, mid, ql, qr, x));
        if (qr > mid) res = min(res, query(rs, mid + 1, r, ql, qr, x));
        return res;
    }

    void build(int rt, int l, int r) {
        root[rt] = 0;
        if (l == r) return;
        int mid = (l + r) >> 1;
        build(ls, l, mid); build(rs, mid + 1, r);
    }

    void init(int l, int r) {
        build(1, l, r);
        t.init();
    }

    #undef ls
    #undef rs
}tt;

void dfs1(int x, int pa, int d) {
    depth[x] = d; fa[x] = pa; sz[x] = 1;
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == pa) continue;
        dis[y] = dis[x] + tmp.second;
        dfs1(y, x, d + 1);
        sz[x] += sz[y];
        if (sz[y] > sz[son[x]]) son[x] = y;
    }
}

void dfs2(int x, int anc) {
    id[x] = ++num; top[x] = anc;
    if (son[x]) dfs2(son[x], anc);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        dfs2(y, y);
    }
}

void Update(int x) {
    t.update(t.fir, 0, inf, a[x], dis[x] + dis[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x]) continue;
        Update(y);
    }
}

void update(int x, ll val) {
    t.update(t.fir, 0, inf, a[x], dis[x] + dis[x] - val - val);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x]) continue;
        update(y, val);
    }
}

void dfs3(int x) {
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        dfs3(y); t.init();
    }
    if (son[x]) dfs3(son[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        Update(y);
    }
    t.update(t.fir, 0, inf, a[x], dis[x] + dis[x]);
    for (auto tmp : mem[x]) ans[tmp.id] = min(ans[tmp.id], t.query(t.fir, 0, inf, tmp.d) - dis[x] - dis[x]);
    for (auto tmp : vec[x]) ans[tmp.id] = min(ans[tmp.id], t.query(t.fir, 0, inf, tmp.d) - dis[x] - dis[x]);
}

void dfs4(int x) {
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        update(y, dis[x]);
    }
    t.update(t.fir, 0, inf, a[x], 0);
    for (auto tmp : mem[x]) ans[tmp.id] = min(ans[tmp.id], t.query(t.fir, 0, inf, tmp.d));
    if (son[x]) dfs4(son[x]);
    else t.init();
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        dfs4(y);
    }
}

void UPdate(int x, int anc) {
    t.update(t.fir, 0, inf, a[x], (dis[x] - dis[anc] - dis[anc]) * 2);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x]) continue;
        UPdate(y, anc);
    }
}

void dfs5(int x) {
    t.update(t.fir, 0, inf, a[x], -dis[x] - dis[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        UPdate(y, x);
    }
    for (auto tmp : fuc[x]) ans[tmp.id] = min(ans[tmp.id], t.query(t.fir, 0, inf, tmp.d) + dis[tmp.x] + dis[tmp.x]);
    if (son[x]) dfs5(son[x]);
    else t.init();
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        dfs5(y);
    }
}

void UPDate(int x) {
    t.update(t.fir, 0, inf, a[x], dis[x] + dis[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x]) continue;
        UPDate(y);
    }
}

void dfs6(int x) {
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        dfs6(y); t.init();
    }
    if (son[x]) dfs6(son[x]);
    t.update(t.fir, 0, inf, a[x], dis[x] + dis[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x]) continue;
        UPDate(y);
    }
    for (auto tmp : fuc[x]) ans[tmp.id] = min(ans[tmp.id], t.query(t.fir, 0, inf, tmp.d) + (dis[tmp.x] << 1) - (dis[x] << 2));
}

void Update(int x, int L, int R, int anc) {
    tt.update(1, L, R, id[anc], a[x], (dis[x] - dis[anc]) << 1);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x]) continue;
        Update(y, L, R, anc);
    }
}

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));
    }
    dfs1(1, 1, 1); dfs2(1, 1);
    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;
        int fx = x, fy = y;
        while (top[fx] != top[fy]) {
            if (depth[top[fx]] < depth[top[fy]]) swap(fx, fy);
            mem[fx].push_back(Node(i, d));
            fx = fa[top[fx]];
        }
        if (id[fx] > id[fy]) swap(fx, fy);
        if (fx == top[fx]) mem[fy].push_back(Node(i, d));
        else {
            ex[top[fx]].push_back(NOde(i, id[fx], id[fy], d));
            vec[fy].push_back(Node(i, d));
        }
        len[i] = dis[x] + dis[y] - dis[fx] - dis[fx];
        ans[i] = INF;
        int uuu = fx;
        fuc[fx].push_back(NODe(i, d, uuu));
        while (top[fx] != 1) {
            fx = fa[top[fx]];
            fuc[fx].push_back(NODe(i, d, uuu));
        }
    }
    dfs3(1); t.init(); dfs4(1); dfs5(1); dfs6(1); t.init();
    for (int i = 1, x, L, R; i <= n; ++i) {
        if (ex[i].empty()) continue;
        x = i;
        while (son[x]) x = son[x];
        tt.init(L = id[i], R = id[x]);
        x = i;
        while (x) {
            tt.update(1, L, R, id[x], a[x], 0);
            for (auto tmp : g[x]) {
                int y = tmp.first;
                if (y == fa[x] || y == son[x]) continue;
                Update(y, L, R, x);
            }
            x = son[x];
        }
        for (auto tmp : ex[i]) ans[tmp.id] = min(ans[tmp.id], tt.query(1, L, R, tmp.l, tmp.r, tmp.d));
    }
    for (int i = 1; i <= T; ++i) printf("%d %lld\n", i, ans[i] + len[i]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 8ms
memory: 122764kb

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:

1 6130845802041
2 10806758605627
3 3440559556796
4 5426989115608
5 4458875959622
6 1566659300400
7 7908007295597
8 1846030561682
9 5112206409383
10 6968388472340
11 4706970599850
12 5270158948178
13 4633066810868
14 3176122148295
15 2331646415266
16 961435137842
17 14353365296423
18 9675072605938
19...

result:

wrong answer 1st lines differ - expected: '6130845802041', found: '1 6130845802041'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #20:

score: 0
Wrong Answer
time: 919ms
memory: 166324kb

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:

1 920563165
2 270738856
3 355012553
4 363898450
5 515535908
6 734168762
7 81197110
8 448355845
9 204186827
10 966151314
11 377621564
12 856252543
13 311456222
14 368700872
15 197258906
16 567302636
17 172379629
18 579171621
19 1043838058
20 244996663
21 621435809
22 278057792
23 727463012
24 5737833...

result:

wrong answer 1st lines differ - expected: '920563165', found: '1 920563165'

Subtask #4:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 564ms
memory: 169308kb

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:

1 29294995135992468
2 9003943574137677
3 39324997066279292
4 37544709020512848
5 57388992119827952
6 54425124319330092
7 19450449300737912
8 25838911017710871
9 2608104102967357
10 32395369352281774
11 5765752637876701
12 65609495812941401
13 57820177390587134
14 1971831067795873
15 1921368202538951...

result:

wrong answer 1st lines differ - expected: '29294995135992468', found: '1 29294995135992468'

Subtask #5:

score: 0
Wrong Answer

Test #36:

score: 0
Wrong Answer
time: 205ms
memory: 155824kb

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:

1 1352416884531
2 1380463318391
3 923920163167
4 1525224977139
5 1405019709299
6 869269749781
7 715671043328
8 876194052054
9 1358007874327
10 127994985855
11 1230162209719
12 1532026808855
13 611656467332
14 1023855959729
15 414792924571
16 1316679734677
17 827308370883
18 1265411315424
19 82148436...

result:

wrong answer 1st lines differ - expected: '1352416884531', found: '1 1352416884531'

Subtask #6:

score: 0
Wrong Answer

Test #43:

score: 0
Wrong Answer
time: 468ms
memory: 154792kb

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:

1 516260625003899
2 380880451347644
3 183401242058615
4 56975236749493
5 349851829300288
6 188845759476214
7 188011317678919
8 414887287533565
9 111834744858133
10 305218494040213
11 227244584301956
12 365579485207024
13 201761449059479
14 246263150359463
15 468212144364502
16 389353276591541
17 207...

result:

wrong answer 1st lines differ - expected: '516260625003899', found: '1 516260625003899'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%