QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#234549#6520. Classic Problemucup-team1198AC ✓641ms111284kbC++209.4kb2023-11-01 19:05:162023-12-06 00:10:34

Judging History

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

  • [2023-12-06 00:10:34]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:641ms
  • 内存:111284kb
  • [2023-11-01 19:05:16]
  • 评测
  • 测评结果:100
  • 用时:599ms
  • 内存:114152kb
  • [2023-11-01 19:05:16]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<int, int>
#define ld long double
#define all(a) (a).begin(), (a).end()

const int MAXN = 1100000;

vector<pair<int, int>> to_right[MAXN];
vector<pair<int, int>> to_left[MAXN];


int par[MAXN];

int get(int v) {
    if (par[v] == v)
        return v;
    return par[v] = get(par[v]);
}

bool merge(int v, int u) {
    v = get(v);
    u = get(u);
    if (v == u)
        return false;
    par[v] = u;
    return true;
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<pair<pair<int, int>, int>> edges(m);
    vector<int> interesting;
    vector<int> without;
    set<pair<int, int>> was;
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        /*a = rand() % n + 1;
        b = rand() % n + 1;
        if (a == b)
            continue;
        if (was.count(make_pair(min(a, b), max(a, b))))
            continue;
        was.emplace(min(a, b), max(a, b));
        c = rand() % n;*/
        cin >> a >> b >> c;
        --a;
        --b;
        if (a > b)
            swap(a, b);
        if (a + 1 == b)
            without.emplace_back(a);
        edges[i] = make_pair(make_pair(a, b), c);
        interesting.emplace_back(a);
        interesting.emplace_back(b);
    }
    
    long long ans = 0;
    sort(without.begin(), without.end());
    int prev = 0;
    vector<pair<int, int>> segms;
    for (int v : without) {
        segms.emplace_back(prev, v);
        ans += v - prev;
        prev = v + 1;
    }
    segms.emplace_back(prev, n - 1);
    ans += n - 1 - prev;


    sort(interesting.begin(), interesting.end());
    interesting.resize(unique(interesting.begin(), interesting.end()) - interesting.begin());
    {
        vector<int> extra;
        int i = 0;
        for (auto [l, r] : segms) {
            int j = i;
            while (j < interesting.size() && interesting[j] <= r)
                ++j;
            // [i, j)
            int l1 = i;
            int l2 = l;
            while (l1 < j && interesting[l1] == l2) {
                ++l1;
                ++l2;
            }
            if (l2 <= r) {
                extra.emplace_back(l2);
            }
            int r1 = j - 1;
            int r2 = r;
            while (r1 >= i && interesting[r1] == r2) {
                --r1;
                --r2;
            }
            if (r2 >= l) {
                extra.emplace_back(r2);
            }
            i = j;
        }
        for (int x : extra)
            interesting.emplace_back(x);
        sort(interesting.begin(), interesting.end());
        interesting.resize(unique(interesting.begin(), interesting.end()) - interesting.begin());
    }
    

    // all the interesting
    map<int, int> ids;
    n = interesting.size();
    for (int i = 0; i < n; ++i)
        ids[interesting[i]] = i;
    for (auto [uv, w] : edges) {
        auto [u, v] = uv;
        int i1 = ids[u];
        int i2 = ids[v];
        to_right[i1].emplace_back(i2, w);
        to_left[i2].emplace_back(i1, w);
    }
    for (int i = 0; i < n; ++i) {
        sort(to_left[i].rbegin(), to_left[i].rend());
        sort(to_right[i].begin(), to_right[i].end());
    }
    {
        int i = 0;
        for (int l = 0; l < segms.size(); ++l) {
            int j = i;
            while (j < interesting.size() && interesting[j] <= segms[l].second) {
                ++j;
            }
            segms[l] = make_pair(i, j);
            assert(i != j);
            i = j;
        }
    }
    vector<vector<int>> comps;
    vector<int> szs;
    vector<int> comp_id(n);
    for (int i = 0; i < segms.size(); ++i) {
        comps.emplace_back();
        comps.back().emplace_back(i);
        szs.emplace_back(segms[i].second - segms[i].first);
        for (int j = segms[i].first; j < segms[i].second; ++j)
            comp_id[j] = i;
    }

    {
        iota(par, par + n, 0);
        for (auto [uv, w] : edges) {
            if (w != 0)
                continue;
            auto [u, v] = uv;
            u = ids[u];
            v = ids[v];
            if (merge(u, v)) {
                --ans;
                //cout << '+' << 1 << '\n';
            }
        }

        iota(par, par + segms.size(), 0);
        for (auto [uv, w] : edges) {
            if (w != 0)
                continue;
            auto [u, v] = uv;
            u = ids[u];
            v = ids[v];
            int i = comp_id[u];
            int j = comp_id[v];
            if (merge(i, j)) {
                ++ans;
                //cout << '+' << 1 << '\n';
            }
        }
    }

    while (comps.size() > 1) {
        vector<pair<int, pair<int, int>>> min_edges;
        vector<int> go_right(n);
        vector<int> go_left(n);
        iota(go_right.begin(), go_right.end(), 1);
        iota(go_left.begin(), go_left.end(), -1);
        for (auto comp : comps) {
            for (int segm_id : comp) {
                for (int i = segms[segm_id].first; i < segms[segm_id].second; ++i) {
                    go_right[i] = segms[segm_id].second;
                    go_left[i] = segms[segm_id].first - 1;
                }
            }

            pair<int, pair<int, int>> min_edge(1e9 + 228, make_pair(-1, -1));
            for (int segm_id : comp) {
                for (int i = segms[segm_id].first; i < segms[segm_id].second; ++i) {
                    int l = 0;
                    int j = go_right[i];
                    while (j < n) {
                        if (comp_id[j] == comp_id[i]) {
                            j = go_right[j];
                            continue;
                        }
                        while (l < to_right[i].size() && to_right[i][l].first < j) {
                            ++l;
                        }
                        if (l == to_right[i].size() || to_right[i][l].first > j) {
                            break;
                        }
                        j = go_right[j];
                    }
                    if (j != n) {
                        min_edge = min(min_edge, make_pair(interesting[j] - interesting[i], make_pair(i, j)));
                    }
                    for (auto [u, w] : to_right[i]) {
                        if (comp_id[u] != comp_id[i])
                            min_edge = min(min_edge, make_pair(w, make_pair(i, u)));
                    }

                    l = 0;
                    j = go_left[i];
                    while (j > -1) {
                        if (comp_id[j] == comp_id[i]) {
                            j = go_left[j];
                            continue;
                        }
                        while (l < to_left[i].size() && to_left[i][l].first > j) {
                            ++l;
                        }
                        if (l == to_left[i].size() || to_left[i][l].first < j) {
                            break;
                        }
                        j = go_left[j];
                    }
                    if (j != -1) {
                        min_edge = min(min_edge, make_pair(interesting[i] - interesting[j], make_pair(j, i)));
                    }   
                    for (auto [u, w] : to_left[i]) {
                        if (comp_id[u] != comp_id[i])
                            min_edge = min(min_edge, make_pair(w, make_pair(u, i)));
                    }
                }
            }
            min_edges.emplace_back(min_edge);

            for (int segm_id : comp) {
                for (int i = segms[segm_id].first; i < segms[segm_id].second; ++i) {
                    go_right[i] = i + 1;
                    go_left[i] = i - 1;
                }
            }
        }

        vector<bool> alive_comp(comps.size(), true);
        for (auto min_edge : min_edges) {
            auto [w, uv] = min_edge;
            auto [u, v] = uv;
            //cout << u << ' ' << v << ' ' << w << '\n';
            u = comp_id[u];
            v = comp_id[v];
            if (u == v)
                continue;
            ans += w;
            if (szs[u] > szs[v]) {
                swap(u, v);
            }
            szs[v] += szs[u];
            for (int i : comps[u]) {
                comps[v].emplace_back(i);
                for (int j = segms[i].first; j < segms[i].second; ++j)
                    comp_id[j] = v;
            }
            alive_comp[u] = false;
        }
        vector<vector<int>> new_comps;
        vector<int> new_szs;
        for (int i = 0; i < comps.size(); ++i) {
            if (!alive_comp[i])
                continue;
            new_comps.emplace_back();
            new_szs.emplace_back(szs[i]);
            sort(comps[i].begin(), comps[i].end());
            for (int j : comps[i]) {
                while (!new_comps.back().empty() && segms[new_comps.back().back()].second == segms[j].first) {
                    segms[j].first = segms[new_comps.back().back()].first;
                    new_comps.back().pop_back();
                }
                new_comps.back().emplace_back(j);
            }
            for (int segm_id : new_comps.back()) {
                for (int j = segms[segm_id].first; j < segms[segm_id].second; ++j) {
                    comp_id[j] = new_comps.size() - 1;
                }
            }
        }
        if (comps.size() == new_comps.size()) break;
        swap(comps, new_comps);
        swap(szs, new_szs);
    }
    cout << ans << '\n';
    for (int i = 0; i < n; ++i) {
        to_left[i].clear();
        to_right[i].clear();
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 55152kb

input:

3
5 3
1 2 5
2 3 4
1 5 0
5 0
5 4
1 2 1000000000
1 3 1000000000
1 4 1000000000
1 5 1000000000

output:

4
4
1000000003

result:

ok 3 number(s): "4 4 1000000003"

Test #2:

score: 0
Accepted
time: 629ms
memory: 78772kb

input:

16
1000000000 0
447 99681
1 2 1000000000
1 3 1000000000
2 3 1000000000
1 4 1000000000
2 4 1000000000
3 4 1000000000
1 5 1000000000
2 5 1000000000
3 5 1000000000
4 5 1000000000
1 6 1000000000
2 6 1000000000
3 6 1000000000
4 6 1000000000
5 6 1000000000
1 7 1000000000
2 7 1000000000
3 7 1000000000
4 7 ...

output:

999999999
446000000000
732256441
999999680
999899999
999966830
127
4
2186
1562
1176
5100
5
503
679
4

result:

ok 16 numbers

Test #3:

score: 0
Accepted
time: 551ms
memory: 75920kb

input:

5
1000000000 100000
158260500 877914550 822889311
24979400 861648750 548830120
433933400 476190600 342858071
211047200 971407750 480845266
731963950 822804750 449656269
430302150 982631900 545923679
880895700 923078500 816372580
189330700 910286900 135599877
303238500 404539650 605997004
492686550 7...

output:

999999999
999999999
999999999
999999999
999999999

result:

ok 5 number(s): "999999999 999999999 999999999 999999999 999999999"

Test #4:

score: 0
Accepted
time: 641ms
memory: 111284kb

input:

5
2000 100000
1873 1874 822889311
1290 1291 548830120
1162 1163 342858071
814 815 480845266
742 743 449656269
1609 1610 545923679
1431 1432 816372580
1143 1144 135599877
414 415 605997004
1117 1118 921749002
121 122 786119025
1672 1673 655702582
38 39 211428413
1639 1640 393671555
922 923 501983447
...

output:

4097
20020
198635
2099999
1000099999

result:

ok 5 number(s): "4097 20020 198635 2099999 1000099999"

Test #5:

score: 0
Accepted
time: 377ms
memory: 72624kb

input:

5
100000 100000
36426 60522 446755913
60522 90081 181424399
3497 60522 625711486
60522 94325 647639160
60522 68417 160714711
35902 60522 61020590
23857 60522 626006012
29211 60522 547865075
60522 63340 561330066
60016 60522 650693494
24593 60522 849028504
60522 90310 285323416
11431 60522 990607689
...

output:

100024
150003
200003
250000
999999999

result:

ok 5 number(s): "100024 150003 200003 250000 999999999"

Test #6:

score: 0
Accepted
time: 254ms
memory: 63564kb

input:

5
4000 100000
694 696 619136615
1611 1614 829153748
2551 2552 370724298
3034 3037 49559541
98 105 443754249
822 827 735959328
878 885 201346483
2729 2731 304071225
3961 3965 557890416
1631 1637 430215474
3195 3205 212882505
503 507 937363346
3141 3150 47574456
577 583 727402691
3673 3675 279029853
3...

output:

43935
6913
4336
18350
12678

result:

ok 5 number(s): "43935 6913 4336 18350 12678"

Test #7:

score: 0
Accepted
time: 582ms
memory: 88180kb

input:

5
100000 100000
73979 73980 5250107
1946 1947 757506029
87433 87434 443673136
64352 64354 338125488
69103 69104 235256717
60843 60845 20769731
23601 23602 214534313
92417 92419 669411181
57364 57365 519766962
42029 42031 827237806
98524 98525 593643533
71482 71483 662414293
6709 6710 745687608
36460...

output:

166767
127085
110197
1000033410
1000009975

result:

ok 5 number(s): "166767 127085 110197 1000033410 1000009975"

Test #8:

score: 0
Accepted
time: 364ms
memory: 66864kb

input:

5
1000000000 50000
46934929 214668318 1
539508531 807854772 3
81957905 439197851 0
259767671 917896915 1
415730678 521821941 0
728421737 764345460 2
266428928 403328188 2
287022349 739465773 2
374255863 866291129 2
260858097 908362294 1
413843056 908357026 3
498835884 656136616 4
294506717 617875204...

output:

999989920
999949999
999990020
999949999
999991707

result:

ok 5 number(s): "999989920 999949999 999990020 999949999 999991707"

Test #9:

score: 0
Accepted
time: 203ms
memory: 57844kb

input:

10
318 50000
202 215 664792164
27 240 237398902
211 303 234481289
30 100 677362025
22 264 146815592
119 291 400424695
259 315 252405962
216 247 5088627
157 290 178189323
155 316 734981880
193 247 88649525
111 284 566532841
131 228 543880192
35 295 517550073
11 162 662675576
222 260 74089536
317 318 ...

output:

23819960
39448612
8097046
59114094
45057369
51858561
4062351
79061552
43095015
16693179

result:

ok 10 numbers

Test #10:

score: 0
Accepted
time: 173ms
memory: 55356kb

input:

100
102 5000
48 70 8432902
25 75 334675824
56 84 328506917
62 66 204777391
14 27 171625816
26 63 83629619
57 101 321540985
41 85 311469363
15 66 351390467
25 26 211230409
35 74 290758638
11 52 127872342
29 41 36722590
7 40 305168650
57 59 281651751
13 19 227794663
11 68 263248839
11 13 201312984
4 3...

output:

26178167
22787545
57728222
59428785
9611076
18313917
46857052
2980349
14019519
7766478
32126352
23015186
24398047
5999746
38936614
16904414
35847382
20532753
4984654
19717352
5263678
25996814
15710632
604534
14654144
6955086
27641478
3343419
23105160
26432195
5553390
7056453
53902303
169550500
23592...

result:

ok 100 numbers

Test #11:

score: 0
Accepted
time: 181ms
memory: 55148kb

input:

500
50 1000
30 37 244841531
34 50 162121748
2 24 54057560
13 25 99874751
6 23 8670341
34 39 89926367
1 22 221235953
6 24 105286564
13 34 326397341
3 46 290792604
11 24 346530259
28 36 211580979
3 9 238363354
11 28 280948971
6 36 179816637
18 32 156586941
20 23 98299400
12 50 111363373
22 26 11434081...

output:

189
220
230
182
158
173
200
220
189
164
176
202
142
198
172
239
197
192
183
162
157
250
152
216
243
164
196
189
201
180
183
210
191
202
191
184
178
181
228
210
192
204
219
200
198
215
224
202
143
182
223
186
187
196
218
179
209
178
177
184
182
223
227
199
182
220
208
203
165
205
187
191
201
188
197
...

result:

ok 500 numbers

Test #12:

score: 0
Accepted
time: 178ms
memory: 55108kb

input:

5000
16 100
7 11 287768961
9 16 301149044
4 6 274782222
9 10 450284038
4 7 235183489
2 16 58894948
2 7 238261638
1 15 4511525
4 14 199094011
9 15 18708487
10 14 424492139
5 12 289396677
6 10 205753555
14 16 379921511
9 14 361051609
8 11 462083923
12 16 49451818
7 13 207151488
9 11 468320084
7 9 4016...

output:

84
71179265
61
63
4255393
72
25533862
58
87249761
71
84
19481903
71
46709478
822790
10042255
24930386
63
69894
58
48150185
862500
224220
42138089
740049
8599540
58154641
67865973
14516424
70
87090761
67494787
13034240
91
5581460
331588
67
9506689
80
34867161
43217292
75264458
56
1335841
22470126
266...

result:

ok 5000 numbers

Test #13:

score: 0
Accepted
time: 388ms
memory: 55108kb

input:

5000
2000 100
268 269 623604670
964 965 850016410
1696 1697 958745313
1507 1508 723608729
970 971 753143786
1384 1385 247122568
1448 1449 877299742
1096 1097 795129348
1042 1043 221000653
357 358 298701522
1767 1768 316413195
515 516 229633720
1682 1683 901777899
1181 1182 693437267
1960 1961 867110...

output:

2099
10099
100099
2000099
1000000099
2099
10099
100099
2000099
1000000099
2099
10099
100099
2000099
1000000099
2099
10099
100099
2000099
1000000099
2099
10099
100099
2000099
1000000099
2099
10099
100099
2000099
1000000099
2099
10099
100099
2000099
1000000099
2099
10099
100099
2000099
1000000099
2099...

result:

ok 5000 numbers

Extra Test:

score: 0
Extra Test Passed