QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#481205#5439. Meet in the MiddleduankaidiWA 157ms8676kbC++235.6kb2024-07-16 21:27:022024-07-16 21:27:02

Judging History

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

  • [2024-07-16 21:27:02]
  • 评测
  • 测评结果:WA
  • 用时:157ms
  • 内存:8676kb
  • [2024-07-16 21:27:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct Tree {
// public:
    int n, s, root, tot;
    std::vector<int> fa, top, dfn, id, sz, son;
    std::vector<i64> dep;
    std::vector<std::vector<std::pair<int, int>>> adj;
    Tree() {Tree(0);}
    Tree(int _n) {
        init(_n, 0);
    }
    Tree(int _n, int rt) {
        init(_n, rt);
    }
    void init(int _n, int rt) {
        n = _n; tot = 0;
        dep.assign(n, 0);
        fa.assign(n, 0);
        top.assign(n, 0);
        dfn.assign(n, 0);
        id.assign(n, 0);
        sz.assign(n, 1);
        son.assign(n, -1);
        adj.assign(n, {});
        root = rt;
    }
    void add(int u, int v, int w = 1) {
        adj[u].push_back(std::pair(v, w));
        adj[v].push_back(std::pair(u, w));
    }
    void dfs(int x, int fath) {
    	// cerr << "CAO: " << adj.size() << ' ' << adj[x].size() << '\n';
    	// for (auto [v, w] : adj[x]) cerr << x <<' ' <<v << ' ' << w << '\n';
        for (auto [v, w] : adj[x])
            if (v != fath) {
                fa[v] = x;
                dep[v] = dep[x] + w;
                dfs(v, x);
                sz[x] += sz[v];
                if (son[x] == -1 || sz[son[x]] < sz[v])
                    son[x] = v;
            }
    }
    void dfs2(int x, int topf) {
        top[x] = topf;
        id[dfn[x] = tot++] = x;
        if (son[x] == -1) {
            return ;
        }
        dfs2(son[x], topf);
        for (auto [v, w] : adj[x]) {
            if (v == son[x] || v == fa[x])
                continue;
            dfs2(v, v);
        }
    }
    int lca(int u, int v) {
        while (top[u] != top[v]) {
            if (dep[top[u]] < dep[top[v]])
                std::swap(u, v);
            u = fa[top[u]];
        }
        return (dep[u] < dep[v] ? u : v);
    }
    i64 dist(int u, int v) {
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }
    bool isAcestor(int u, int v) {
        return lca(u, v) == u;
    }

    void init() {
    	// cerr << root << ' ' << root << "!\n";
        dfs(root, root);
        dfs2(root, root);
    }
} ;
template<class Info>
class SegmentTree {
public:
    int n;
    std::vector<Info> info;
    SegmentTree() : n{} {}
    SegmentTree(int _n) {
        Build(_n, std::vector<Info> (_n, {}));
    }
    SegmentTree(int _n, std::vector<Info> a) {
        Build(_n, a);
    }

    void Build(int _n, std::vector<Info> a) {
        n = _n;
        info.assign(n << 2, {});
        std::function<void(int, int, int)> build = [&](int x, int l, int r) {
            if (l + 1 == r) {
                info[x] = a[l];
                return ;
            }
            int mid = (l + r) >> 1;
            build(x << 1, l, mid);
            build(x << 1 | 1, mid, r);
            info[x] = info[x << 1] + info[x << 1 | 1];
        } ;
        build(1, 0, n);
    }

    void Modify(int x, int l, int r, int pos/*, Info val*/) {
        if (l + 1 == r) {
            // info[x] = val;
            return ;
        }
        int mid = (l + r) >> 1;
        if (pos < mid) {
            Modify(x << 1, l, mid, pos/*, val*/);
        } else {
            Modify(x << 1 | 1, mid, r, pos/*, val*/);
        }
        info[x] = info[x << 1] + info[x << 1 | 1];
    }
    void Modify(int pos/*, Info val*/) {
        Modify(1, 0, n, pos/*, val*/);
    }

    Info RangeQuery(int x, int l, int r, int L, int R) {
        if (l >= R || r <= L)
            return {};
        if (l >= L && r <= R)
            return info[x];
        int mid = (l + r) >> 1;
        return RangeQuery(x << 1, l, mid, L, R) + RangeQuery(x << 1 | 1, mid, r, L, R);
    }
    Info RangeQuery(int L, int R) {
        return RangeQuery(1, 0, n, L, R);
    }
    Info Query(int pos) {
        return RangeQuery(pos, pos + 1);
    }
} ;
struct Info {
	int l = 0, r = 0;
} ;
Tree A, B;
int now;
i64 getdis(int x, int y) {
	return A.dist(now, x) + A.dist(now, y) + B.dist(x, y);
}
Info operator + (Info lhs, Info rhs) {
	Info res = lhs; i64 R = getdis(lhs.l, lhs.r);
	vector<int> vec = {lhs.l, lhs.r, rhs.l, rhs.r};
	for (int i = 0; i < 4; ++i) for (int j = i + 1; j < 4; ++j) {
		if (getdis(vec[i], vec[j]) > R) {
			R = getdis(vec[i], vec[j]);
			res.l = vec[i], res.r = vec[j];
		}
	}
    return res;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, q; cin >> n >> q;
	A.init(n, 0); B.init(n, 0);
	vector<vector<array<int, 2>>> vec(n), t1(n);
	vector<i64> ans(q);
	for (int i = 1; i < n; ++i) {
		int u, v, w; cin >> u >> v >> w; --u, --v;
		A.add(u, v, w);
		t1[u].push_back({v, w});
		t1[v].push_back({u, w});
    }
	for (int i = 1; i < n; ++i) {
		int u, v, w; cin >> u >> v >> w, --u, --v;
		B.add(u, v, w);
	} 
	A.init(); 
	B.init();
	for (int i = 0; i < q; ++i) {
		int a, b; cin >> a >> b, --a, --b;
		vec[a].push_back({b, i});
	}
	vector<Info> init(n);
	for (int i = 0; i < n; ++i) init[i].l = init[i].r = A.id[i];
	SegmentTree<Info> seg(n, init);
	auto upd = [&](int x, int type) {
		if (type == 0) now = x;
		else now = A.fa[x];
		seg.Modify(A.dfn[now]); seg.Modify(A.dfn[now] + A.sz[now] - 1);
	} ;
	function<void(int, int)> dfs = [&](int x, int fa) {
		if (x > 1) upd(x, 0);
		int l = seg.RangeQuery(0, n).l, r = seg.RangeQuery(0, n).r;
        // cerr << x << ' ' << l << ' '<< r << "cao\n";
		for (auto [v, i] : vec[x]) {
			i64 vx = A.dist(l, x) + B.dist(l, v);
			i64 vy = A.dist(r, x) + B.dist(r, v);
			ans[i] = max(vx, vy);
		}
		for (auto [v, w] : t1[x]) if (v != fa) dfs(v, x);
		if (x > 1) upd(x, 1);
	} ;
	dfs(0, 0);
	for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3592kb

input:

3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2

output:

6
4
5
3

result:

ok 4 number(s): "6 4 5 3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

2 1
1 2 1
1 2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3892kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 104ms
memory: 7736kb

input:

10000 50000
8101 5996 108427744
5996 7605 870838849
5996 5599 603303696
8101 3006 339377417
8101 6463 442516687
6463 5560 109174079
5560 4063 127596224
3006 1682 947915262
5996 1986 130416311
6463 5455 279771516
6463 2634 516688796
4063 3034 217886863
7605 5092 742375061
5599 2266 193804402
5092 140...

output:

647838384844
626539793176
514273941704
637066393138
472546379596
645842915960
641537859258
573604504956
644081575470
803875451466
674370549986
734764046916
744862815441
763778393516
553499885160
526743824759
610373719514
689550247042
549161302822
726811438160
653134244120
666761991962
701575393972
6...

result:

ok 50000 numbers

Test #5:

score: 0
Accepted
time: 133ms
memory: 7724kb

input:

10000 50000
5314 8843 137901358
5314 4153 459134340
5314 8667 933926892
4153 6504 330487798
4153 8880 750362377
4153 5990 874275912
4153 546 563436331
5990 6216 902348875
8843 3101 669215553
6216 8138 732343176
8667 8675 581114294
6504 7416 127778711
546 4239 282695908
6504 9455 549237168
5314 8340 ...

output:

464564968641
331633000004
565299667784
484694871646
570451097836
417492802442
372302349684
638725688107
386235986078
355738655551
462027769535
558485994764
524714144289
450157947013
432701214095
494566741391
529031758638
637683369382
415646847933
344894296260
390294136162
527685175763
575151290175
3...

result:

ok 50000 numbers

Test #6:

score: 0
Accepted
time: 75ms
memory: 8016kb

input:

10000 50000
2808 2490 757309564
2808 9601 312271047
2808 4046 119325897
2808 4894 466822371
4894 1507 498399554
2490 5982 84088145
9601 1251 149019541
2808 6681 416590999
2808 6583 357757899
1251 3192 889947539
6583 9762 350572496
6681 22 597479070
5982 8744 263208242
8744 5281 49894126
1507 8806 30...

output:

1501072697023
2058806276380
2017086500812
2044250452467
1543567245539
1695101693278
1765462307870
2576423082091
2302805133490
2090282734929
2375783476943
1954788661090
2056530503168
2453153202726
1978028047409
2106220371212
2210163378358
2015714406862
1555876274751
2122832986951
2102262624814
169085...

result:

ok 50000 numbers

Test #7:

score: 0
Accepted
time: 73ms
memory: 8676kb

input:

10000 50000
4064 7188 81750473
7188 8466 310631946
8466 2276 154981798
2276 7347 162965456
7188 464 806245243
464 2250 849189978
8466 641 734602751
8466 9246 225800419
4064 5267 191524437
2276 5292 192776095
2276 9036 414997994
9246 5470 362146726
2250 473 98496385
4064 7726 700294189
473 9503 42824...

output:

3589143478793
5241855728342
3397106617685
3432843859461
4544481241003
3649934075137
3020107625030
3297847713344
3894730366667
3030559097282
4824131552194
4821302024170
4471510161493
3291683748595
4954639576578
2961243269520
3659899432127
3421183608349
5262802614761
4408705330639
5203984107670
500158...

result:

ok 50000 numbers

Test #8:

score: 0
Accepted
time: 122ms
memory: 7800kb

input:

10000 50000
8676 4714 406191383
8676 5040 603960140
5040 9715 635348098
4714 9483 594267326
9483 5451 409058229
8676 8913 909259106
9715 1399 320185961
9715 4857 180234031
4714 8888 585099487
5040 1244 645347755
5451 7736 479423492
9483 1038 272038574
1399 3970 638817231
8888 3314 55726955
8888 2295...

output:

447424387353
491327570749
614052040822
384218910068
429859933145
356174725430
609432604118
465420084327
472632020898
382647454960
343751681021
441874503695
463199624732
610943875286
563031986601
566780763247
346991783125
601234775562
619765985074
357316826763
495874578271
526431260851
331681020073
4...

result:

ok 50000 numbers

Test #9:

score: 0
Accepted
time: 79ms
memory: 7812kb

input:

10000 50000
30 8765 730632293
8765 4245 897288335
30 4974 965971295
4974 9464 585377707
9464 744 157095406
744 6387 969328235
744 235 905769171
744 912 989443452
744 1341 273641834
744 9933 802952118
4974 8348 248881694
8765 9127 36706230
912 6136 324362270
4974 8517 411159721
8348 1941 672019024
94...

output:

260285187513
334448828465
448073136303
349497881882
360969017248
402078622370
390257279014
308648100196
320994952264
289449337583
393159064851
453034865550
283828471834
446349617896
380894281657
408838602752
363824724502
420964873388
362606539223
391080537186
304570333981
245848347318
310973758007
3...

result:

ok 50000 numbers

Test #10:

score: 0
Accepted
time: 74ms
memory: 7692kb

input:

10000 50000
2152 8278 350040498
2152 3058 895649234
3058 2264 151370300
8278 6118 576488088
8278 7313 464941095
3058 6966 884173172
6966 9779 786319677
9779 9796 943877064
6118 929 517473780
6118 2651 550491074
7313 662 313307192
7313 8043 506406589
7313 1698 864683116
6118 5060 766592488
6966 1903 ...

output:

652477746679
597325264627
539318490039
597048004752
421646977029
649309274459
506349020540
429554460108
462828559625
593933122751
543584884281
652286846854
654020863570
717938057245
431237695994
601883634488
731084254857
588856926225
399175030875
575410680840
526320427336
494819850806
529049784844
5...

result:

ok 50000 numbers

Test #11:

score: 0
Accepted
time: 93ms
memory: 7664kb

input:

10000 50000
7747 7582 379514112
7582 4607 188977429
7582 5317 187026200
7747 8600 712822661
8600 4262 212978273
7747 827 649275004
827 8014 76935591
7747 6641 753086484
7582 8582 206016126
8014 8460 708095438
8014 9211 377732689
8582 4450 416298437
827 9208 699971259
9208 6823 416992550
8582 186 770...

output:

746737735180
498470031337
630778245801
714337715073
566315588400
809241414480
668680437815
575990359534
612421079786
631355089285
534254563162
566879359756
667087033615
650377712872
669587743650
611030906118
593248384501
735077133684
585253655611
595113935966
519628983099
603281284099
529926712130
5...

result:

ok 50000 numbers

Test #12:

score: 0
Accepted
time: 119ms
memory: 7836kb

input:

10000 50000
3472 122 628742395
3472 3867 379635964
3867 1902 749838600
3867 7438 305533780
122 6633 278565996
122 1661 208291710
7438 3819 677928429
1902 7425 657683150
1661 5239 676247552
1902 2756 448261111
7438 7365 97063037
3819 6763 371040229
6633 8865 356148629
6763 5581 863369674
6633 1551 46...

output:

608697605604
482134269787
577721966634
628074778968
445000575297
484814952220
511586460160
374153126368
469519128844
602443844531
619658782918
385417337773
345965815878
620132139546
609655051154
537845187251
622122602447
436588599524
531403283302
587074749895
441226010189
421564270566
406700017163
5...

result:

ok 50000 numbers

Test #13:

score: -100
Wrong Answer
time: 157ms
memory: 7664kb

input:

10000 50000
1160 9231 559787863
9231 4770 299521320
9231 9192 876373929
4770 3107 498755345
1160 5830 724175215
3107 9464 281278611
5830 3611 15139105
4770 7601 642740087
9464 910 538577221
9464 8134 554493711
7601 5225 259081456
9192 4493 741155925
5225 7756 789054604
5225 9044 160953940
7601 6104 ...

output:

44163199908
36544889589
45890256673
36776414195
36604003823
39650732470
43389306319
37419280779
42684423989
37470526473
42398992970
40710607327
42271798904
40981602830
42083787825
40682505559
39511748870
39133821656
42107800923
40131700757
38159799832
39161288828
42785415940
38415055107
37202416831
...

result:

wrong answer 5th numbers differ - expected: '37333219129', found: '36604003823'