QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#303446#4924. 蜘蛛爬树zyc0704190 0ms33108kbC++147.7kb2024-01-12 15:43:082024-01-12 15:43:09

Judging History

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

  • [2024-01-12 15:43:09]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:33108kb
  • [2024-01-12 15:43:08]
  • 提交

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;}
    }t[N];
    // 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());
            // cerr << rt << ' ' << t.size() << '\n';
            // if (tot >= t.size()) puts("qwq"), exit(0);
            t[tot].k = k; t[tot].b = b;
            return;
        }
        // if (rt >= t.size()) puts("qwq"), exit(0);
        // assert(rt < t.size());
        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;
        // assert(rt < t.size());
        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;
    }

    // void init() {t.clear(); t.push_back(node()); tot = fir = 0;}
}t[3];

void dfs1(int x, int pa, int d, int anc) {
    t[0].update(t[0].fir, 0, 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, 0, 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, 0, 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, 0, 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 Update(int x) {
    t[2].update(t[2].fir, 0, inf, a[x], dis[x] + dis[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || vis[y]) continue;
        Update(y);
    }
}

void Query(int x, int anc) {
    for (auto tmp : mem[x]) {
        int y = tmp.y, d = tmp.d, id = tmp.id;
        ans[id] = min(ans[id], t[2].query(t[2].fir, 0, inf, d) + dis[x] + dis[y] - dis[anc] - dis[anc]);
        // printf("[%d %d %lld]\n", anc, id, ans[id]);
    }
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || vis[y]) continue;
        Query(y, anc);
    }
}

void dfs3(int x) {
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x] || vis[y]) continue;
        dfs3(y); t[2].init();
    }
    if (son[x]) dfs3(son[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x] || vis[y]) continue;
        Update(y);
    }
    t[2].update(t[2].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] || vis[y]) continue;
        // printf("[%d %d]\n", y, x);
        Query(y, x);
    }
    for (auto tmp : mem[x]) {
        int y = tmp.y, id = tmp.id, d = tmp.d;
        ans[id] = min(ans[id], t[2].query(t[2].fir, 0, inf, d) + dis[y] - dis[x]);
        // printf("[%d %d %d]\n", x, id, ans[id]);
    }
}

void dfs4(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, 0, inf, a[x], 0);
    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, 0, 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]) dfs4(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;
        dfs4(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);
    }
}

void solve(int rt) {
    t[0].init(); t[1].init(); dis[rt] = 0;
    dfs1(rt, rt, 1, -1);
    for (int i = 1; i <= n; ++i)
        for (auto tmp : vec[i])
            if (tmp.y == rt) mem[i].push_back(tmp);
    dfs3(rt); t[2].init(); dfs4(rt);
    // printf("[%d %lld]\n", rt, ans[1]);
    for (int i = 1; i <= n; ++i) son[i] = 0;
}

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;
    }
    // solve(3); return 0;
    for (int i = 1; i <= n; ++i) solve(i);
    // solve(63);
    for (int i = 1; i <= T; ++i) printf("%d %lld\n", i, ans[i]);
    return 0;
}

//考虑点分治套点分治,递归变成子任务6

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 33108kb

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
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
Time Limit Exceeded

Test #36:

score: 0
Time Limit Exceeded

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
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%