QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#311876 | #7749. A Simple MST Problem | iliaaaaa | TL | 599ms | 110552kb | C++20 | 1.4kb | 2024-01-22 22:18:34 | 2024-01-22 22:18:35 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define F first
#define S second
#define All(x) (x).begin(), (x).end()
#define len(x) (int) (x).size()
#define pb push_back
const int N = 1e6 + 7;
int n, par[N], w[N];
bool mark[N];
int get_par(int u) {
return par[u] < 0? u: par[u] = get_par(par[u]);
}
bool merge(int u, int v) {
u = get_par(u), v = get_par(v);
if (u == v)
return false;
if (par[u] < par[v])
swap(u, v);
par[v] += par[u];
par[u] = v;
return true;
}
int32_t main() {
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
for (int i = 2; i < N; i++)
if (!mark[i])
for (int j = i; j < N; j += i)
mark[j] = true, w[j]++;
int q;
cin >> q;
memset(par, -1, sizeof par);
while (q--) {
int l, r;
cin >> l >> r;
vector<pair<int, pii>> ed;
for (int i = 1; i <= r; i++) {
int who, mn;
mn = INT_MAX;
for (int j = i; j <= r; j += i)
if (j >= l && w[j] <= mn)
who = j, mn = w[j];
for (int j = i; j <= r; j += i)
if (j >= l)
ed.pb({w[who] + w[j] - w[i], {j, who}});
}
int ans = 0;
sort(All(ed));
for (auto e: ed)
if (merge(e.S.F, e.S.S))
ans += e.F;
for (int i = l; i <= r; i++)
par[i] = -1;
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 13ms
memory: 12448kb
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: 117ms
memory: 24288kb
input:
2 27 30 183704 252609
output:
8 223092
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 112ms
memory: 24212kb
input:
1 183704 252609
output:
223092
result:
ok single line: '223092'
Test #4:
score: 0
Accepted
time: 599ms
memory: 110348kb
input:
2 639898 942309 30927 34660
output:
983228 11512
result:
ok 2 lines
Test #5:
score: 0
Accepted
time: 551ms
memory: 110552kb
input:
3 21731 33468 46192 370315 1712 3753
output:
34608 948775 5299
result:
ok 3 lines
Test #6:
score: 0
Accepted
time: 208ms
memory: 34668kb
input:
4 15237 67700 10918 86104 98287 116980 17432 23592
output:
148811 210927 60893 18687
result:
ok 4 lines
Test #7:
score: -100
Time Limit Exceeded
input:
5 5594 70302 19202 69588 5634 7877 59821 469300 97439 261121
output:
179735 145706 6565 1203684 488669