QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#256596#7749. A Simple MST Problemucup-team191#TL 551ms113700kbC++142.5kb2023-11-18 20:27:072023-11-18 20:27:08

Judging History

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

  • [2023-11-18 20:27:08]
  • 评测
  • 测评结果:TL
  • 用时:551ms
  • 内存:113700kb
  • [2023-11-18 20:27:07]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;

const int N = 1e6 + 500;

int par[N];

int pr(int x) {
	if(x == par[x]) return x;
	return par[x] = pr(par[x]);
}

void mrg(int x, int y) {
	par[pr(x)] = pr(y);
}

int w[N], t[N];
vector < int > dv[N];

void precompute(){
	for(int i = 1;i < N;i++) t[i] = 1, dv[i].PB(1);
	for(int i = 2;i < N;i++) {
		if(w[i] == 0) {
			for(int j = i;j < N;j += i) w[j]++, t[j] *= i;
		}
		if(t[i] == i) {
			for(int j = i;j < N;j += i) {
				dv[j].PB(i);
			}
		}
	}
	for(int i = 1;i < N;i++) dv[i].shrink_to_fit();
}

int l, r;
vector < int > p, d;
int naj[N], dr[N], un[N], cookie;
int grp[N], veza[N], kol[N];

ll boruvka_iter() {
	for(int x : p) grp[x] = pr(x), veza[grp[x]] = -1;
	for(int y : d) naj[y] = -1, dr[y] = -1;
	for(int x : p) {
		for(int y : dv[x]) {
			if(!un[y]) break;
			if(naj[y] != -1 && grp[naj[y]] == grp[x] && w[x / y] < w[naj[y] / y]){
				naj[y] = x;
			} else if(naj[y] == -1 || (grp[naj[y]] != grp[x] && w[x / y] < w[naj[y] / y])) {
				dr[y] = naj[y]; naj[y] = x;
			} else if(dr[y] == -1 || w[x / y] < w[dr[y] / y]) {
				dr[y] = x;
			}
		}
	}
	for(int y : d) if(dr[y] == -1) un[y] = 0;
	for(int x : p) {
		for(int y : dv[x]) {
			if(!un[y]) break;
			int cst = -1, tko = -1;
			if(grp[naj[y]] != grp[x])
				cst = w[x / y] + w[naj[y] / y] + w[y], tko = naj[y];
			else if(dr[y] != -1)
				cst = w[x / y] + w[dr[y] / y] + w[y], tko = dr[y];
			if(cst != -1 && (veza[grp[x]] == -1 || cst < kol[grp[x]])) {
				kol[grp[x]] = cst; veza[grp[x]] = grp[tko]; 
			}
		}
	}
	ll ret = 0;
	for(int x : p) {
		if(par[x] != x) continue;
		if(pr(x) != pr(veza[x])) {
			mrg(x, veza[x]);
			ret += kol[x];
		}
	}
	return ret;
}


void solve(){
	p.clear(); d.clear();
	scanf("%d%d", &l, &r);
	cookie++;
	for(int i = 1;i <= r - l;i++)
		if(t[i] == i) un[i] = 1, d.PB(i);
	for(int i = l;i <= r;i++) {
		p.PB(t[i]);
	}
	sort(p.begin(), p.end());
	ll dod = 0;
	for(int i = 1;i < (int)p.size();i++)
		if(p[i] == p[i - 1]) dod += w[p[i]];
	p.erase(unique(p.begin(), p.end()), p.end());
	for(int x : p) par[x] = x;
	ll ans = 0;
	for(;;) {
		int poc = p[0], sve = 1;
		for(int x : p) {
			sve &= pr(x) == pr(poc);
		}
		if(sve) break;
		ans += boruvka_iter();
	}
	printf("%lld\n", ans + dod);
}

int main(){
	precompute();
	int T; scanf("%d", &T);
	for(;T--;) solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 525ms
memory: 106696kb

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: 551ms
memory: 111740kb

input:

2
27 30
183704 252609

output:

8
223092

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 544ms
memory: 113700kb

input:

1
183704 252609

output:

223092

result:

ok single line: '223092'

Test #4:

score: -100
Time Limit Exceeded

input:

2
639898 942309
30927 34660

output:


result: