QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#761308#9533. Classical Counting Problemscallionsong0 357ms32772kbC++145.8kb2024-11-18 21:58:172024-11-18 21:58:18

Judging History

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

  • [2024-11-18 21:58:18]
  • 评测
  • 测评结果:0
  • 用时:357ms
  • 内存:32772kb
  • [2024-11-18 21:58:17]
  • 提交

answer

bool M1;
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define LL __int128
#define db double
#define LD long double
#define Pii pair<int,int>
#define Pll pair<ll,ll>
#define Pull pair<ull,ull>
#define Pdb pair<db,db>
#define fir first
#define sec second
#define vec vector<int>
#define pb push_back
#define qlr cerr<<"qlr\n"
#define dyh cerr<<"dyh\n"
#define pc(x) __builtin_popcount(x)
#define uni(x,y) uniform_int_distribution<int>(x,y)(rng)
#define unl(x,y) uniform_int_distribution<ll>(x,y)(rng)
#define unr(x,y) uniform_real_distribution<double>(x,y)(rng)
#define F(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define UF(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define look_memory cerr<<'\n'<<abs(&M1-&M2)/1024.0/1024<<'\n'
#define look_time cerr<<'\n'<<clock()*1.0/CLOCKS_PER_SEC<<'\n'
mt19937 rng(time(0)^(*new int));
const int INF=0x3f3f3f3f;
const int Mod=998244353;
template<typename T>
inline void inc(T &a,T b){
	if(b<0) b+=Mod;
	a+=b;
	if(a>=Mod) a-=Mod;
}
template<typename T>
inline void dec(T &a,T b){
	if(b<0) b+=Mod;
	a-=b;
	if(a<0) a+=Mod;
}
template<typename T>
inline void muc(T &a,T b){
	a=a*b%Mod;
}
template<typename T>
inline bool chkmin(T &a,T b){
	if(a<=b) return false;
	a=b;
	return true;
}
template<typename T>
inline bool chkmax(T &a,T b){
	if(a>=b) return false;
	a=b;
	return true;
}
int T,n;
uint ans;
vec e[100010];
struct Dsu{
	#define N 100000
	int fa[N+10];
	void init(int n){
		F(i,1,n) fa[i]=i;
	}
	int fd(int x){
		return fa[x]==x?x:fa[x]=fd(fa[x]);
	}
	void merge(int x,int y){
		x=fd(x),y=fd(y);
		fa[x]=y;
	}
	#undef N
}dsu;
struct Tree{
	#define N 100000
	int n,rt;
	int fth[N+10],siz[N+10],son[N+10],top[N+10],dfn[N+10];
	vec e[N+10];
	void dfs1(int u,int fa){
		fth[u]=fa;
		siz[u]=1;
		son[u]=0;
		for(auto v:e[u]){
			if(v==fa) continue;
			dfs1(v,u);
			siz[u]+=siz[v];
			if(siz[v]>siz[son[u]]) son[u]=v;
		}
	}
	void dfs2(int u,int fa,int topf){
		top[u]=topf;
		dfn[u]=++dfn[0];
		if(son[u]) dfs2(son[u],u,topf);
		for(auto v:e[u]){
			if(v==fa||v==son[u]) continue;
			dfs2(v,u,v);
		}
	}
	void build(){
		dfs1(rt,0);
		dfs2(rt,0,rt);
		dfn[0]=0;
	}
	vec get_chain(int x){
		vec res;
		while(x){
			res.pb(x);
			x=son[x];
		}
		return res;
	}
	#undef N
}T1,T2;
struct Seg{
	#define N 100000
	#define ls k*2
	#define rs k*2+1
	uint a[N+10];
	uint w1[N*4+10],w2[N*4+10],w3[N*4+10],tag[N*4+10];//b,a*c,a*b*c
	void update(int k){
		w1[k]=w1[ls]+w1[rs];
		w2[k]=w2[ls]+w2[rs];
		w3[k]=w3[ls]+w3[rs];
	}
	void down(int k){
		if(tag[k]){
			w1[ls]+=tag[k];
			w3[ls]+=w2[ls]*tag[k];
			tag[ls]+=tag[k];
			w1[rs]+=tag[k];
			w3[rs]+=w2[rs]*tag[k];
			tag[rs]+=tag[k];
			tag[k]=0;
		}
	}
	void build(int k,int L,int R){
		w1[k]=0,w2[k]=0,tag[k]=0;
		if(L==R) return;
		int mid=(L+R)>>1;
		build(ls,L,mid);
		build(rs,mid+1,R);
	}
	void change1(int k,int L,int R,int x,int y){
		if(L==R){
			w2[k]=a[L]*y;
			w3[k]=w1[k]*w2[k];
			return;
		}
		down(k);
		int mid=(L+R)>>1;
		if(x<=mid) change1(ls,L,mid,x,y);
		else change1(rs,mid+1,R,x,y);
		update(k);
	}
	void change2(int k,int L,int R,int x,int y,int z){
		if(L>=x&&R<=y){
			w3[k]+=w2[k]*z;
			tag[k]+=z;
			return;
		}
		down(k);
		int mid=(L+R)>>1;
		if(x<=mid) change2(ls,L,mid,x,y,z);
		if(y>mid) change2(rs,mid+1,R,x,y,z);
		update(k);
	}
	uint query(int k,int L,int R,int x,int y){
		if(L>=x&&R<=y) return w3[k];
		down(k);
		int mid=(L+R)>>1;
		if(y<=mid) return query(ls,L,mid,x,y);
		else if(x>mid) return query(rs,mid+1,R,x,y);
		else return query(ls,L,mid,x,y)+query(rs,mid+1,R,x,y);
	}
	#undef N
	#undef ls
	#undef rs
}seg;
void add(int x){
	seg.change1(1,1,n,T2.dfn[x],1);
	while(x){
		seg.change2(1,1,n,T2.dfn[T2.top[x]],T2.dfn[x],1);
		x=T2.fth[T2.top[x]];
	}
}
void del(int x){
	seg.change1(1,1,n,T2.dfn[x],0);
	while(x){
		seg.change2(1,1,n,T2.dfn[T2.top[x]],T2.dfn[x],-1);
		x=T2.fth[T2.top[x]];
	}
}
void dfs1(int u,int fa){
	add(u);
	for(auto v:T1.e[u]){
		if(v==fa) continue;
		dfs1(v,u);
	}
}
void dfs2(int u,int fa){
	del(u);
	for(auto v:T1.e[u]){
		if(v==fa) continue;
		dfs2(v,u);
	}
}
uint query(int x){
	uint res=0;
	while(x){
		res+=seg.query(1,1,n,T2.dfn[T2.top[x]],T2.dfn[x]);
		x=T2.fth[T2.top[x]];
	}
	return res;
}
void solve(){
	cin>>n;
	F(i,1,n) e[i].clear();
	F(i,1,n-1){
		int x,y;
		cin>>x>>y;
		e[x].pb(y);
		e[y].pb(x);
	}
	T1.n=n,T1.rt=1;
	F(i,1,n) T1.e[i].clear();
	dsu.init(n);
	UF(i,n,1){
		for(auto j:e[i]){
			if(j<i) continue;
			T1.e[i].pb(dsu.fd(j));
			dsu.merge(j,i);
		}
	}
	T2.n=n,T2.rt=n;
	F(i,1,n) T2.e[i].clear();
	dsu.init(n);
	F(i,1,n){
		for(auto j:e[i]){
			if(j>i) continue;
			T2.e[i].pb(dsu.fd(j));
			dsu.merge(j,i);
		}
	}
	T1.build();
	T2.build();
	ans=0;
	F(i,1,n) seg.a[T2.dfn[i]]=i;
	seg.build(1,1,n);
	F(i,1,n){
		if(T1.top[i]!=i) continue;
		vec V=T1.get_chain(i);
		reverse(V.begin(),V.end());
		for(auto j:V){
			add(j);
			for(auto k:T1.e[j]){
				if(k!=T1.fth[j]&&k!=T1.son[j]) dfs1(k,j);
			}
			ans+=j*query(j);
		}
		for(auto j:V){
			del(j);
			for(auto k:T1.e[j]){
				if(k!=T1.fth[j]&&k!=T1.son[j]) dfs2(k,j);
			}
		}
	}
	cout<<ans<<'\n';
}
bool M2;
int main(){
//	freopen("ex_a4.in","r",stdin);
//	freopen("monotone.out","w",stdout);
	srand(time(0)^(*new int));
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>T;
	while(T--) solve();
	look_memory;
	look_time;
	return 0;
}
/*
1
3
1 3
2 3

6
3
1 2
2 3
3
1 3
2 3
7
2 1
3 1
4 1
5 1
6 5
7 6
6
2 1
3 1
4 1
5 4
6 1
9
2 1
3 2
4 3
5 1
6 4
7 5
8 2
9 3
9
2 1
3 2
4 3
5 4
6 5
7 2
8 3
9 5
*/

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 34ms
memory: 20496kb

input:

10000
10
10 2
5 2
7 2
1 7
4 10
6 2
3 7
9 7
8 5
10
6 5
1 6
9 5
4 6
3 1
7 5
2 4
8 4
10 5
10
7 6
1 6
4 1
3 4
5 1
9 1
8 7
10 6
2 8
10
8 7
5 7
9 7
3 5
4 3
2 4
6 8
1 7
10 4
10
10 3
2 3
9 2
1 2
4 2
6 4
5 4
7 4
8 4
10
6 1
3 6
7 6
5 6
9 7
2 5
4 1
10 9
8 6
10
3 5
6 5
1 6
10 3
4 1
7 1
8 1
9 3
2 10
10
9 10
3 9
...

output:

1562
2672
1502
3164
1869
3983
1211
2628
1540
4137
957
2441
1865
2959
1500
1701
2261
2554
1747
2229
2323
3480
2199
1375
1648
4365
2377
1544
1912
1114
1697
2814
2201
2397
1350
1764
1747
2362
1388
1707
2343
2284
2467
1480
1650
1998
937
1363
1779
1976
3003
979
3453
1656
2002
2302
3450
2682
2393
2994
170...

result:

wrong answer 1st lines differ - expected: '1592', found: '1562'

Subtask #2:

score: 0
Wrong Answer

Test #2:

score: 0
Wrong Answer
time: 50ms
memory: 20128kb

input:

5000
20
20 19
7 20
16 19
2 7
15 16
13 2
11 7
6 2
14 2
12 15
5 14
17 2
4 20
3 6
18 20
10 5
1 16
9 14
8 15
20
7 17
15 7
10 17
13 7
18 10
9 17
6 18
16 18
14 16
1 10
19 17
3 18
2 13
8 19
5 1
11 14
20 8
4 18
12 11
20
20 1
17 1
18 20
3 1
9 18
16 18
10 18
12 18
6 9
5 16
19 18
4 5
11 17
14 18
15 17
7 6
13 7...

output:

20671
37541
34222
22781
17434
27322
46022
44933
16476
27164
28409
25003
26254
21067
13075
28074
17799
35960
12568
53187
11178
31418
11697
37685
13246
15133
40860
35046
24535
27080
33364
35531
20950
18051
15008
16237
23062
23181
22077
17325
14569
26289
22612
53411
26286
17001
29557
13130
18671
16176
...

result:

wrong answer 1st lines differ - expected: '20721', found: '20671'

Subtask #3:

score: 0
Wrong Answer

Test #4:

score: 0
Wrong Answer
time: 61ms
memory: 20812kb

input:

2500
40
29 30
24 29
36 29
7 24
18 36
4 18
21 24
17 21
1 17
14 30
19 24
34 19
12 34
11 30
23 30
16 34
38 14
35 24
27 29
20 34
3 17
22 17
31 21
26 14
5 14
2 20
15 16
39 4
37 38
10 3
25 22
33 29
32 10
13 35
9 18
8 2
6 23
40 34
28 7
40
32 10
33 32
2 32
7 10
22 7
21 10
31 22
40 32
37 32
25 22
16 22
39 10...

output:

806281
403373
116571
239692
293003
472570
645719
170171
307957
112018
234763
359508
467474
223573
609752
128902
267443
391019
238384
233945
363568
128218
153310
351218
424563
278883
358577
134046
382661
133985
271208
529918
240050
208093
412019
458364
430353
459388
227813
254174
457594
434789
242981...

result:

wrong answer 1st lines differ - expected: '810633', found: '806281'

Subtask #4:

score: 0
Wrong Answer

Test #8:

score: 0
Wrong Answer
time: 97ms
memory: 21044kb

input:

500
200
63 7
145 63
78 145
103 63
163 7
97 163
57 7
186 78
30 145
25 57
56 97
112 25
50 7
162 50
105 112
126 57
32 126
95 105
188 105
124 112
86 186
82 162
143 162
194 95
183 97
101 82
197 82
200 186
96 143
146 124
164 197
54 95
195 57
131 143
130 146
193 112
36 97
16 163
62 193
93 124
121 96
1 96
1...

output:

125533806
97894130
51948042
288335810
248319528
161144569
131793209
159815616
328011257
107220110
295533151
117387506
119104223
297774525
232519758
123153419
163788511
334115184
156986097
152286987
197309321
123293093
152368842
147258380
327191945
198496928
213442711
117195317
68831361
186222022
107...

result:

wrong answer 1st lines differ - expected: '126443395', found: '125533806'

Subtask #5:

score: 0
Wrong Answer

Test #13:

score: 0
Wrong Answer
time: 163ms
memory: 21840kb

input:

33
3000
1322 1797
2442 1797
626 2442
1979 2442
485 1979
1428 1979
2196 1979
1499 1797
1936 2442
2921 1979
751 1499
2182 2921
979 1499
935 1979
1136 485
1880 1797
1084 2921
349 751
482 349
1009 979
1003 349
2056 482
2959 1136
1288 751
496 2442
1693 935
2045 1499
868 1009
1264 1428
2891 868
1045 1288
...

output:

1340596598
391345800
656433078
2928068299
4282689561
2986602840
3671261820
3107830383
2405365299
2422289240
4266978692
4271911366
331701944
2068257429
3380588513
3790006554
3918807957
369686567
1132686287
4247829218
1203646515
372943322
2360269972
2619396805
3085699893
564286838
3153986657
411756661...

result:

wrong answer 1st lines differ - expected: '3914500827', found: '1340596598'

Subtask #6:

score: 0
Wrong Answer

Test #18:

score: 0
Wrong Answer
time: 335ms
memory: 32772kb

input:

1
98303
65520 65521
65521 65519
65519 65522
65522 65517
65517 65518
65518 65516
65516 65523
65523 65513
65513 65514
65514 65512
65512 65515
65515 65510
65510 65511
65511 65509
65509 65524
65524 65505
65505 65506
65506 65504
65504 65507
65507 65502
65502 65503
65503 65501
65501 65508
65508 65498
6549...

output:

1591688611

result:

wrong answer 1st lines differ - expected: '2387240282', found: '1591688611'

Subtask #7:

score: 0
Wrong Answer

Test #22:

score: 0
Wrong Answer
time: 357ms
memory: 30140kb

input:

1
100000
16150 88283
9425 88283
87988 88283
52935 88283
69816 88283
51311 88283
6910 9425
59991 87988
54743 6910
19614 52935
22926 69816
91163 88283
17233 19614
64177 19614
92097 91163
57414 9425
96321 6910
17859 54743
59810 69816
66565 17859
9948 96321
84506 59810
3928 64177
63031 54743
6214 87988
...

output:

4056914491

result:

wrong answer 1st lines differ - expected: '1787575884', found: '4056914491'