QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#271112#7749. A Simple MST ProblemlanhfWA 384ms90728kbC++172.6kb2023-12-01 23:27:352023-12-01 23:27:36

Judging History

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

  • [2023-12-01 23:27:36]
  • 评测
  • 测评结果:WA
  • 用时:384ms
  • 内存:90728kb
  • [2023-12-01 23:27:35]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;

int lp[N], rep[N], cnt[N];
vector<int> par;
vector<int> divisors[N];
pair<int, int> fir[N];
pair<int, int> sec[N];
tuple<int, int, int> mine[N];

int find(int u) {
  if (par[u] < 0) return u;
  return par[u] = find(par[u]);
}

bool join(int u, int v) {
  u = find(u); v = find(v);
  if (u != v) {
    par[v] = u; return true;
  }
  return false;
}



void solve() {
  int l, r; cin >> l >> r;
  vector<int> v;
  for (int i = l; i <= r; i++)
    v.push_back(rep[i]);
  int n = v.size();
  par.assign(n, -1);
  int cmp = n, res = 0;
  while (cmp > 1) {
    for (int i = 1; i <= r; i++)
      fir[i] = sec[i] = {1e9, -1};
    for (int i = 0; i < n; i++)
      mine[i] = {1e9, 0, 0};
    for (int i = 0; i < n; i++)
      for (int x : divisors[v[i]]) {
        if (cnt[v[i]] < fir[x].first) {
          if (fir[x].second != find(i))
            sec[x] = fir[x];
          fir[x] = {cnt[v[i]], find(i)};
        } else if (cnt[v[i]] < sec[x].first && find(i) != sec[x].second)
          sec[x] = {cnt[v[i]], find(i)};
      }
    
    for (int i = 0; i < n; i++)
      for (int x : divisors[v[i]]) {
        auto thr = make_pair(cnt[v[i]], find(i));
        if (sec[x].second < 0) continue;
        if (thr.second != fir[x].second)
          mine[find(i)] = min(mine[find(i)],
          {thr.first + fir[x].first - cnt[x],
          thr.second, fir[x].second});
        if (thr.second != sec[x].second)
          mine[find(i)] = min(mine[find(i)],
          {thr.first + sec[x].first - cnt[x],
          thr.second, sec[x].second});
      }
    
    for (int i = 0; i < n; i++) {
      auto [w, u, v] = mine[i];
      if (join(u, v)) {
        res += w; cmp--;
      }
    }
  }
  cout << res << '\n';
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  iota(lp, lp + N, 0);
  for (int i = 2; i * i < N; i++)
    if (lp[i] == i)
      for (int j = i * i; j < N; j += i)
        if (lp[j] == j) lp[j] = i;
  for (int i = 1; i < N; i++) {
    int x = i;
    rep[i] = 1;
    bool square = false;
    vector<int> factors;
    while (x > 1) {
      int y = lp[x];
      x /= y;
      rep[i] *= y;
      cnt[i]++;
      factors.push_back(y);
      while (x % y == 0) {
        square = true; x /= y;
      }
    }
    if (square) continue;
    int n = factors.size();
    for (int mask = 0; mask < (1 << n); mask++) {
      x = 1;
      for (int j = 0; j < n; j++)
        if (mask >> j & 1) x *= factors[j];
      divisors[i].push_back(x);
    }
  }
    
  int t; cin >> t;
  while (t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 147ms
memory: 72564kb

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: 167ms
memory: 76196kb

input:

2
27 30
183704 252609

output:

8
223092

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 180ms
memory: 78356kb

input:

1
183704 252609

output:

223092

result:

ok single line: '223092'

Test #4:

score: 0
Accepted
time: 296ms
memory: 90728kb

input:

2
639898 942309
30927 34660

output:

983228
11512

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 250ms
memory: 83040kb

input:

3
21731 33468
46192 370315
1712 3753

output:

34608
948775
5299

result:

ok 3 lines

Test #6:

score: 0
Accepted
time: 163ms
memory: 78240kb

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: 384ms
memory: 87664kb

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: 311ms
memory: 81964kb

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: 225ms
memory: 83188kb

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: 309ms
memory: 84792kb

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: 292ms
memory: 79356kb

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: 294ms
memory: 79792kb

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: 286ms
memory: 78968kb

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: -100
Wrong Answer
time: 245ms
memory: 77324kb

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
30
32461
19423

result:

wrong answer 28th lines differ - expected: '29', found: '30'