QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#255931#7749. A Simple MST Problemucup-team1191#RE 614ms121692kbC++233.2kb2023-11-18 17:32:222023-11-18 17:32:23

Judging History

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

  • [2023-11-18 17:32:23]
  • 评测
  • 测评结果:RE
  • 用时:614ms
  • 内存:121692kb
  • [2023-11-18 17:32:22]
  • 提交

answer

#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

const int N = 1e6 + 7;
const int Inf = 1e9;

int par[N];

int get(int a) { return (a == par[a] ? a : par[a] = get(par[a])); }

bool join(int a, int b) {
  a = get(a);
  b = get(b);
  if (a != b) {
    par[a] = b;
    return true;
  } else {
    return false;
  }
}

int mn[N][2];
int cmp[N][2];
int cur_cmp[N];
int best_c[N];
int best_edge[N];
int cost[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  vector<bool> is_prime(N, true);
  vector<bool> is_good(N, true);
  for (int d = 2; d < N; ++d) {
    if (is_prime[d]) {
      ll d_sq = (ll)d * d;
      for (int x = d; x < N; x += d) {
        cost[x] += 1;
        if (x != d) {
          is_prime[x] = false;
          if (x % d_sq == 0) {
            is_good[x] = false;
          }
        }
      }
    }
  }
  vector<vector<int>> subms(N);
  for (int d = 1; d < N; ++d) {
    if (is_good[d]) {
      for (int x = d; x < N; x += d) {
        subms[x].push_back(d);
      }
    }
  }
  int t;
  cin >> t;
  while (t--) {
    int l, r;
    cin >> l >> r;
    ll ans = 0;
    for (int i = l; i <= r; ++i) {
      par[i] = i;
    }
    int cmps = r - l + 1;
    while (cmps > 1) {
      for (int i = l; i <= r; ++i) {
        cur_cmp[i] = get(i);
        best_c[i] = Inf;
        best_edge[i] = -1;
      }
      for (int d = 1; d <= r; ++d) {
        if (is_good[d]) {
          mn[d][0] = mn[d][1] = Inf;
          cmp[d][0] = cmp[d][1] = -1;
          for (int x = d; x <= r; x += d) {
            if (x < l)
              continue;
            int x_cmp = cur_cmp[x];
            int x_cost = cost[x] - cost[d];
            for (int k = 0; k < 2; ++k) {
              if (x_cost < mn[d][k] && (k == 0 || x_cmp != cmp[d][0])) {
                swap(x_cost, mn[d][k]);
                swap(x_cmp, cmp[d][k]);
              }
            }
          }
        }
      }
      for (int x = l; x <= r; ++x) {
        int best_cost = Inf;
        int best_cmp = -1;
        for (int d : subms[x]) {
          for (int k = 0; k < 2; ++k) {
            if (mn[d][k] < best_cost && cmp[d][k] != cur_cmp[x]) {
              best_cost = mn[d][k];
              best_cmp = cmp[d][k];
            }
          }
        }
        assert(best_cmp != -1);
        best_cost += cost[x];
        if (best_cost < best_c[cur_cmp[x]]) {
          best_c[cur_cmp[x]] = best_cost;
          best_edge[cur_cmp[x]] = best_cmp;
        }
      }
      for (int i = l; i <= r; ++i) {
        if (best_edge[i] != -1) {
          if (join(i, best_edge[i])) {
            --cmps;
            ans += best_c[i];
          }
        }
      }
    }
    cout << ans << '\n';
  }
}

/*
5
1 1
4 5
1 4
1 9
19 810

2
27 30
183704 252609
*/

详细

Test #1:

score: 100
Accepted
time: 486ms
memory: 100832kb

input:

5
1 1
4 5
1 4
1 9
19 810

output:

0
2
3
9
1812

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 499ms
memory: 111532kb

input:

2
27 30
183704 252609

output:

8
223092

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 499ms
memory: 106984kb

input:

1
183704 252609

output:

223092

result:

ok single line: '223092'

Test #4:

score: 0
Accepted
time: 583ms
memory: 121692kb

input:

2
639898 942309
30927 34660

output:

983228
11512

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 555ms
memory: 114892kb

input:

3
21731 33468
46192 370315
1712 3753

output:

34608
948775
5299

result:

ok 3 lines

Test #6:

score: 0
Accepted
time: 520ms
memory: 110800kb

input:

4
15237 67700
10918 86104
98287 116980
17432 23592

output:

148811
210927
60893
18687

result:

ok 4 lines

Test #7:

score: 0
Accepted
time: 613ms
memory: 117004kb

input:

5
5594 70302
19202 69588
5634 7877
59821 469300
97439 261121

output:

179735
145706
6565
1203684
488669

result:

ok 5 lines

Test #8:

score: 0
Accepted
time: 614ms
memory: 110808kb

input:

6
43477 229639
188276 269887
72424 150178
9009 36918
137421 180271
14666 124530

output:

541705
255651
232054
77284
135277
313203

result:

ok 6 lines

Test #9:

score: 0
Accepted
time: 570ms
memory: 117268kb

input:

7
461436 501798
98856 148334
20953 119408
910 5027
762 14117
10959 46088
96149 130311

output:

139017
151124
281536
10504
34818
98004
108375

result:

ok 7 lines

Test #10:

score: 0
Accepted
time: 601ms
memory: 109976kb

input:

8
6448 11162
33691 94044
137536 140277
106719 267437
13494 38185
3185 4173
4835 299526
25162 43201

output:

13177
175485
9711
480992
69059
2808
840950
53422

result:

ok 8 lines

Test #11:

score: 0
Accepted
time: 565ms
memory: 111108kb

input:

9
4136 54985
38425 155322
107602 157683
115533 137627
13677 44903
43432 69310
70004 129314
43033 55373
76424 147123

output:

139668
339337
153266
68520
87592
76612
183238
39109
211339

result:

ok 9 lines

Test #12:

score: 0
Accepted
time: 583ms
memory: 111208kb

input:

10
21662 27103
55634 196143
20466 44902
87028 202799
127745 151602
1598 1679
95299 126981
13367 134183
52307 66285
10136 38071

output:

17117
410126
71979
344673
74754
263
100586
342577
41870
77522

result:

ok 10 lines

Test #13:

score: 0
Accepted
time: 566ms
memory: 104176kb

input:

20
14973 29525
12674 35281
27500 64458
67431 109122
12468 19466
4606 9884
38731 44161
3212 89277
23213 37134
4392 40769
5378 7211
22586 29237
56331 81369
43924 59554
31787 34990
19949 21972
47309 65085
5666 48185
99 2714
7969 131566

output:

40882
63073
107649
129480
19975
14563
17562
238237
41056
99346
5358
20747
73854
48244
9911
6517
54866
117382
6374
347901

result:

ok 20 lines

Test #14:

score: 0
Accepted
time: 539ms
memory: 107648kb

input:

30
11101 53557
6079 6241
869 14433
6164 10602
73432 82272
15845 17496
18885 49518
12127 35037
5812 14898
12225 15757
19736 36027
34506 69210
12204 37099
642 1256
11875 12382
169453 211949
20884 26173
8302 26634
75302 79147
13938 16896
11229 13736
4753 7575
2742 17752
4443 5021
2988 5533
1042 1364
19...

output:

118619
538
35473
12392
28768
4915
88426
63728
25217
10666
47893
102086
69488
1584
1669
135374
16581
50701
12720
8517
7762
8170
40235
1798
7014
936
78143
29
32461
19423

result:

ok 30 lines

Test #15:

score: -100
Runtime Error

input:

40
1022 1378
14032 55415
12506 15792
3919 16767
12870 32763
10624 12091
1144 29449
5184 9133
4178 8021
7785 13966
3880 26749
15390 34240
2582 11246
431 4695
7020 28894
14092 27156
52666 55295
4068 22068
7392 11533
18273 31481
18854 47481
7786 39812
7341 24968
22021 54790
3221 10332
4930 37633
4407 1...

output:


result: