UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#200159#2699. 树Anonyme1008895ms83240kbC++113.3kb2023-12-30 09:36:482023-12-30 12:03:43

answer

#include<bits/stdc++.h>
using namespace std;

#define QwQ330AwA return 0
#define ll long long

const int N = 1e5 + 330;
const int V = 1e6 + 6;
int p[V], mu[V];
bool vis[V];
int cnt;
struct Edge {
	int u, v, w;
} e[N];
int now[N];
int L[N], R[N];
int n, m;
vector <int> d[V];

void init(int s) {
	mu[1] = 1;
	for (int i = 2; i <= s; i++) {
		if (!vis[i]) p[++cnt] = i, mu[i] = -1;
		for (int j = 1; j <= cnt && 1ll * i * p[j] <= s; j++) {
			vis[i * p[j]] = 1;
			if (i % p[j] == 0) {
				mu[i * p[j]] = 0;
				break;
			} else mu[i * p[j]] = -mu[i];
		}
	}
}

vector <int> t[N << 2];
int tim[N << 2];
#define ls (p << 1)
#define rs ((p << 1) | 1)
int T;
void upd(int p, int l, int r, int L, int R, int x) {
	if (tim[p] != T) t[p].clear(); 
	tim[p] = T;
	if (L <= l && r <= R) {
		t[p].push_back(x);
		return ;
	}
	int mid = (l + r) / 2;
	if (L <= mid) upd(ls, l, mid, L, R, x);
	if (mid < R) upd(rs, mid + 1, r, L, R, x);
}

ll ans[N];
ll sum;
int siz[N];
int fa[N];
int find(int x) {
	return x == fa[x] ? x : find(fa[x]);
}
int stk[N][2], top;

void merge(int u, int v) {
	u = find(u), v = find(v);
	assert(u != v);
	if (siz[u] < siz[v]) swap(u, v);
	fa[v] = u;
	sum += 1ll * siz[u] * siz[v] * mu[T];
	siz[u] += siz[v];
	top++;
	stk[top][0] = u;
	stk[top][1] = v;
}

void del(int lst) {
	while (top > lst) {
		int u = stk[top][0], v = stk[top][1];
		fa[v] = v;
		siz[u] -= siz[v];
		sum -= 1ll * siz[u] * siz[v] * mu[T];
		top--;
	}
}

void dfs(int p, int l, int r) {
	if (tim[p] != T) {
		ans[l] += sum;
		ans[r + 1] -= sum;
		return ;
	}
	int lst = top;
	for (auto i : t[p]) merge(e[i].u, e[i].v);
	if (l == r) {
		ans[l] += sum;
		ans[r + 1] -= sum;
	} else {
		int mid = (l + r) / 2;
		dfs(ls, l, mid);
		dfs(rs, mid + 1, r);
	}
	del(lst);
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	init(1e6);
	cin >> n;
	for (int i = 1, u, v, w; i < n; i++) {
		cin >> u >> v >> w;
		e[i] = {u, v, w};
		now[i] = i;
	}
	cin >> m;
	for (int i = 1; i < n; i++) L[i] = 0, R[i] = m;
	for (int i = 1; i <= m; i++) {
		int x, y;
		cin >> x >> y;
		int id = now[x];
		R[id] = i - 1;
		L[n + i - 1] = i;
		R[n + i - 1] = m;
		e[n + i - 1] = e[id];
		e[n + i - 1].w = y;
		now[x] = n + i - 1;
	}
	for (int i = 1; i <= n + m - 1; i++) {
		int w = e[i].w;
		for (int j = 1; 1ll * j * j <= w; j++) {
			if (w % j == 0) {
				if (mu[j] != 0) d[j].push_back(i);
				if (j * j != w && mu[w / j] != 0) d[w / j].push_back(i);
			} 
		}
	}
	for (int i = 1; i <= n; i++) siz[i] = 1, fa[i] = i;
	for (int i = 1; i <= 1e6; i++) {
		if (mu[i] == 0) continue;
		T = i;
		for (auto id : d[i]) upd(1, 0, m, L[id], R[id], id);
		dfs(1, 0, m);
	}
	for (int i = 1; i <= m; i++) ans[i] += ans[i - 1];
	for (int i = 0; i <= m; i++) cout << ans[i] << '\n';
	QwQ330AwA;
}

/*
  枚举 $d$,开放为 $d$ 的倍数路径,计算连通块贡献即可,乘个 mu
  多测怎么做啊
  我靠就100组
  感觉可以吧1e6*100*log
  不行啊
  直接开V颗LCT每次断边
  线段树分治换可撤销并查集
  总共nlogV条边
  每条边修改logn个结点
  3个log卧槽
  应该跑不满吧
  那这个题q可以开到1e6啊
  哦可以每条边都拍下来然后直接撤销
  那我觉得挺弱智的
  感觉赢了
*/

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 40ms
memory: 41048kb

input:

3000
2 1 865830
3 1 690690
4 2 870870
5 2 870870
6 5 881790
7 5 728910
8 4 726180
9 7 930930
10 3 82...

output:

1287

result:

ok single line: '1287'

Test #2:

score: 0
Accepted
time: 34ms
memory: 41072kb

input:

3000
2 1 709632
3 1 888420
4 1 510510
5 3 934668
6 3 930930
7 1 881790
8 1 679098
9 1 870870
10 7 88...

output:

8634

result:

ok single line: '8634'

Test #3:

score: 0
Accepted
time: 34ms
memory: 41076kb

input:

3000
2 1 870870
3 2 864690
4 2 821100
5 3 627198
6 3 800760
7 5 930930
8 4 631890
9 1 641520
10 8 84...

output:

506

result:

ok single line: '506'

Test #4:

score: 0
Accepted
time: 54ms
memory: 41060kb

input:

3000
2 1 935340
3 1 649926
4 3 876096
5 3 992310
6 3 746130
7 5 587202
8 2 691530
9 2 961620
10 8 57...

output:

110545

result:

ok single line: '110545'

Subtask #2:

score: 26
Accepted

Test #5:

score: 26
Accepted
time: 49ms
memory: 41064kb

input:

3000
2 1 827310
3 1 870870
4 3 511476
5 3 903210
6 4 672210
7 5 677016
8 1 545790
9 5 881790
10 4 67...

output:

5646
4844
4862
4862
10812
10541
15889
15876
21227
21227
15285
15285
15552
15535
21307
27071
29878
29...

result:

ok 101 lines

Test #6:

score: 0
Accepted
time: 43ms
memory: 41080kb

input:

3000
2 1 930930
3 2 570570
4 3 756000
5 4 881790
6 3 711942
7 4 909510
8 2 870870
9 1 870870
10 1 64...

output:

4760
4825
4370
4280
4585
3625
3865
3541
3541
3685
3655
3655
3655
3690
3795
3871
3863
593
783
720
720...

result:

ok 101 lines

Test #7:

score: 0
Accepted
time: 42ms
memory: 41096kb

input:

3000
2 1 844998
3 1 956670
4 2 881790
5 3 930930
6 1 839808
7 1 870870
8 7 843030
9 6 939510
10 7 90...

output:

2940
2964
2914
2934
3006
3006
3114
6062
6044
5936
5936
3340
14296
14278
14258
14222
14222
25770
1428...

result:

ok 101 lines

Test #8:

score: 0
Accepted
time: 44ms
memory: 41092kb

input:

3000
2 1 690690
3 1 644910
4 2 510510
5 1 832524
6 4 551196
7 5 870870
8 6 722670
9 5 791010
10 4 83...

output:

7816
8200
8136
8136
8136
8100
7716
7908
8031
8155
8221
8155
8155
7957
7473
653
839
839
777
764
487
5...

result:

ok 101 lines

Subtask #3:

score: 31
Accepted

Test #9:

score: 31
Accepted
time: 347ms
memory: 61764kb

input:

50000
1 2 930930
1 3 745710
1 4 930930
1 5 746130
1 6 528990
1 7 598890
1 8 510510
1 9 995328
1 10 8...

output:

26

result:

ok single line: '26'

Test #10:

score: 0
Accepted
time: 375ms
memory: 61740kb

input:

50000
1 2 881790
2 3 746130
3 4 510510
4 5 846846
5 6 903210
6 7 870870
7 8 903210
8 9 831570
9 10 6...

output:

908421

result:

ok single line: '908421'

Test #11:

score: 0
Accepted
time: 365ms
memory: 61760kb

input:

50000
1 2 881790
2 3 538560
3 4 836940
4 5 928290
5 6 738990
6 7 966042
7 8 627270
8 9 961170
9 10 8...

output:

811036260

result:

ok single line: '811036260'

Test #12:

score: 0
Accepted
time: 372ms
memory: 61752kb

input:

50000
1 2 881790
1 3 570570
1 4 545490
1 5 603438
1 6 850668
1 7 705870
1 8 734400
1 9 903210
1 10 5...

output:

3556

result:

ok single line: '3556'

Subtask #4:

score: 38
Accepted

Test #13:

score: 38
Accepted
time: 938ms
memory: 82660kb

input:

100000
1 2 767910
2 3 849090
3 4 734580
4 5 690690
5 6 616170
6 7 570570
7 8 739656
8 9 706302
9 10 ...

output:

2276755482
2276755482
2276755482
2276755482
2276755482
2276755482
2276755482
2276755482
2276755482
2...

result:

ok 101 lines

Test #14:

score: 0
Accepted
time: 766ms
memory: 83008kb

input:

100000
1 2 746130
2 3 881790
3 4 585390
4 5 969990
5 6 612612
6 7 513216
7 8 720090
8 9 930930
9 10 ...

output:

3591691
3591691
3591691
3591691
3591691
3591691
3591691
3591691
3591691
3591691
3591691
3591691
3591...

result:

ok 101 lines

Test #15:

score: 0
Accepted
time: 750ms
memory: 82868kb

input:

100000
1 2 783870
2 3 930930
3 4 881790
4 5 956670
5 6 957390
6 7 930930
7 8 570570
8 9 897666
9 10 ...

output:

3309547050
3621011824
3621011824
3621011824
3621011824
3621046503
3621046503
3621046503
3621064083
3...

result:

ok 101 lines

Test #16:

score: 0
Accepted
time: 739ms
memory: 83176kb

input:

100000
1 2 690270
2 3 909792
3 4 811410
4 5 665280
5 6 881790
6 7 903210
7 8 930930
8 9 600990
9 10 ...

output:

2828161
2828161
2828161
554666800
2533462534
2731396440
2731396440
2731397082
2731462240
2731462240
...

result:

ok 101 lines

Test #17:

score: 0
Accepted
time: 774ms
memory: 82964kb

input:

100000
1 2 816270
2 3 690690
3 4 658350
4 5 869820
5 6 570570
6 7 903210
7 8 935130
8 9 776490
9 10 ...

output:

1561025
1561025
1561025
1661007
1661007
1661007
1661007
1661007
1661007
1661007
1661011
1661011
1661...

result:

ok 101 lines

Test #18:

score: 0
Accepted
time: 899ms
memory: 83240kb

input:

100000
1 2 997458
2 3 985530
3 4 932190
4 5 746130
5 6 955890
6 7 686166
7 8 956670
8 9 510510
9 10 ...

output:

871963763
871963753
872144383
872144383
872144233
872153906
872153902
872153904
872244241
872244241
...

result:

ok 101 lines

Test #19:

score: 0
Accepted
time: 750ms
memory: 83024kb

input:

100000
1 2 570570
2 3 785862
3 4 991380
4 5 976140
5 6 510510
6 7 746130
7 8 510510
8 9 905982
9 10 ...

output:

2067232770
2067280972
2067342259
2067342277
2067342277
2067342263
2067342257
2067342237
2066999869
2...

result:

ok 101 lines

Test #20:

score: 0
Accepted
time: 743ms
memory: 82972kb

input:

100000
1 2 930930
2 3 746130
3 4 870870
4 5 723690
5 6 690690
6 7 814506
7 8 930930
8 9 690690
9 10 ...

output:

2054515460
2054493794
2054493794
2054493794
2054468684
2054543793
2054442685
2054442683
2054442675
2...

result:

ok 101 lines

Test #21:

score: 0
Accepted
time: 737ms
memory: 82944kb

input:

100000
1 2 930930
2 3 946050
3 4 870870
4 5 887040
5 6 803670
6 7 870870
7 8 606528
8 9 965274
9 10 ...

output:

1178987598
1178987598
1178971505
1178971503
1178971503
1179003689
1179003683
1179003683
1179003680
1...

result:

ok 101 lines

Extra Test:

score: 0
Extra Test Passed