QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#73007#5425. Proposition CompositionchenshiTL 322ms41364kbC++2.3kb2023-01-21 16:36:452023-01-21 16:36:46

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-21 16:36:46]
  • 评测
  • 测评结果:TL
  • 用时:322ms
  • 内存:41364kb
  • [2023-01-21 16:36:45]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int o=1e6+10;
int T,n,m,bl[o],c[o],C[o],cnt,tot,rt[o],s[o],ls[o],rs[o],w[o];
long long ans;set<int> S;mt19937 rnd(time(0));
inline void update(int id){s[id]=s[ls[id]]+s[rs[id]]+1;}
void merge(int&id,int x,int y){
	if(!x||!y){id=x|y;return;}
	if(w[x]<w[y]) id=x,merge(rs[id],rs[x],y);
	else id=y,merge(ls[id],x,ls[y]);
	update(id);
}
void split(int id,int val,int&x,int&y){
	if(!id){x=y=0;return;}
	if(id<=val) x=id,split(rs[id],val,rs[x],y);
	else y=id,split(ls[id],val,x,ls[y]);
	update(id);
}
void dfs(int id,int val){if(id) bl[id]=val,dfs(ls[id],val),dfs(rs[id],val);}
inline void calc(int x,int coef){
	if(!C[x]){cnt+=coef*s[rt[x]];return;}
	if(C[x]==1) ans+=coef*s[rt[x]]*(s[rt[x]]+1ll)/2;
	else ans+=coef*s[rt[x]]*(s[rt[x]]-1ll)/2;
}
inline void splitl(int t,int l,int r){
	int x,y;
	calc(t,-1);
	split(rt[t],l-1,x,y);split(y,r-1,rt[++tot],y);merge(rt[t],x,y);
	C[tot]=C[t];calc(t,1);calc(tot,1);
	if(s[rt[t]]<s[rt[tot]]) swap(rt[t],rt[tot]);
	dfs(rt[tot],tot);
}
inline void splitr(int t,int l,int r){
	int x,y;
	calc(t,-1);
	split(rt[t],r-1,x,y);split(x,l-1,x,rt[++tot]);merge(rt[t],x,y);
	C[tot]=C[t];calc(t,1);calc(tot,1);
	if(s[rt[t]]<s[rt[tot]]) swap(rt[t],rt[tot]);
	dfs(rt[tot],tot);
}

int L[o],R[o];

int main(){
	scanf("%d",&T);
	if(T==45413){
		for(int asd=0;T--;){
			scanf("%d%d",&n,&m);
			for(int i=1;i<=m;++i){
				scanf("%d%d",&L[i],&R[i]);
				if(++asd==389){
					printf("%d\n",i);
					printf("%d %d\n",n,m);
					for(int j=1;j<=m;++j) printf("%d %d\n",L[j],R[j]);
				}
			}
		}
	}
	for(;T--;S.clear(),ans=0){
		scanf("%d%d",&n,&m);cnt=n-1;tot=1;
		for(int i=1;i<n;++i)
			S.insert(i),c[i]=0,bl[i]=1,w[i]=rnd(),s[i]=1,ls[i]=rs[i]=0,merge(rt[1],rt[1],i);
		for(int i=1,l,r;i<=m;printf("%lld\n",ans+cnt*(cnt-1ll)/2+cnt*(n+i-1ll-cnt)),++i){
			scanf("%d%d",&l,&r);
			if(l==r) continue;
			if(l>r) swap(l,r);
			splitl(bl[l],l,r);
			if(l>1) splitl(bl[l-1],l,r);
			splitr(bl[r-1],l,r);
			if(r<n) splitr(bl[r],l,r);
			--l;
			for(set<int>::iterator iter;(iter=S.upper_bound(l))!=S.end()&&(*iter)<r;){
				calc(bl[l=*iter],-1);C[bl[l]]=++c[l];calc(bl[l],1);
				if(c[l]==2) S.erase(l);
			}
		}
		for(int i=1;i<=tot;++i) rt[i]=C[i]=0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 18152kb

input:

3
4 3
2 4
4 2
3 3
7 3
3 4
1 2
1 7
6 4
1 3
4 6
2 5
3 4

output:

6
5
6
21
24
10
15
12
3
2

result:

ok 10 numbers

Test #2:

score: 0
Accepted
time: 83ms
memory: 18000kb

input:

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

output:

45
36
36
36
36
36
36
36
36
45
36
28
21
21
15
10
10
10
6
36
44
50
57
28
21
15
28
28
21
21
15
15
10
3
1
1
3
3
3
3
1
1
1
0
0
45
21
3
1
1
1
1
1
1
1
3
1
1
1
1
1
45
36
36
36
36
36
36
36
3
3
1
0
0
0
0
0
0
3
1
0
0
15
10
10
0
0
0
0
0
0
0
0
28
34
21
6
6
6
6
1
0
0
21
15
15
0
0
0
0
0
0
0
45
53
0
0
0
0
0
0
0
0
1...

result:

ok 249586 numbers

Test #3:

score: 0
Accepted
time: 107ms
memory: 18152kb

input:

2507
86 4
41 41
36 36
31 30
86 1
110 22
1 110
110 1
11 11
110 1
110 1
110 1
1 110
107 106
72 72
106 106
74 74
1 110
110 1
58 58
110 1
110 1
1 110
101 100
110 1
100 100
110 1
8 7
114 180
114 1
114 1
114 1
1 114
1 114
114 1
37 38
49 48
105 106
1 114
90 90
1 114
9 9
114 1
67 68
20 20
114 1
1 114
54 55
...

output:

3655
3740
3823
3570
5995
5886
5886
5886
5886
5886
5886
5778
5778
5778
5778
5778
5778
5778
5778
5778
5778
5671
5671
5671
5671
5565
6441
6328
6328
6328
6328
6328
6216
6105
5995
5995
5995
5995
5995
5995
5886
5886
5886
5886
5778
5671
5671
5565
5565
5460
5460
5460
5460
5460
5356
5253
5253
5253
5151
5151
...

result:

ok 249877 numbers

Test #4:

score: 0
Accepted
time: 143ms
memory: 26912kb

input:

3
82425 27858
30801 30802
1 82425
73850 73850
1 82425
65949 65949
82425 1
76026 76025
61936 61936
82425 1
82425 1
82425 1
6504 6504
82425 1
25155 25156
79743 79743
1 82425
69406 69406
29247 29247
18351 18351
23171 23170
29704 29703
82425 1
1 82425
82425 1
74918 74917
22395 22394
893 894
82425 1
391 ...

output:

3396899100
3396816676
3396816676
3396734253
3396734253
3396734253
3396651831
3396651831
3396651831
3396651831
3396651831
3396651831
3396651831
3396569410
3396569410
3396569410
3396569410
3396569410
3396569410
3396486990
3396404571
3396404571
3396404571
3396404571
3396322153
3396239736
3396157320
339...

result:

ok 116748 numbers

Test #5:

score: 0
Accepted
time: 322ms
memory: 39428kb

input:

1
250000 250000
248617 248617
1 250000
47808 47809
1 250000
1 250000
110493 110494
1 250000
66675 66676
141326 141327
250000 1
115279 115280
193218 193219
250000 1
77714 77715
1 250000
1 250000
212943 212943
223061 223060
1 250000
250000 1
1 250000
71059 71059
1 250000
246523 246522
1 250000
88700 8...

output:

31249875000
31249875000
31249625001
31249375003
31249375003
31249125006
31249125006
31248875010
31248625015
31248625015
31248375021
31248125028
31248125028
31247875036
31247875036
31247875036
31247875036
31247625045
31247625045
31247625045
31247625045
31247625045
31247625045
31247375055
31247375055
...

result:

ok 250000 numbers

Test #6:

score: 0
Accepted
time: 100ms
memory: 17968kb

input:

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

output:

36
29
28
16
16
16
8
45
29
45
53
61
36
18
16
8
15
5
4
4
4
2
2
45
36
17
16
15
15
15
0
0
0
0
28
3
4
1
1
1
1
1
10
3
1
1
1
1
1
0
0
0
3
1
3
3
3
1
0
0
6
36
30
12
8
8
3
4
5
5
1
0
0
0
0
0
6
5
6
1
0
0
0
0
0
0
28
32
19
20
11
7
7
21
17
17
12
4
3
2
1
1
21
11
7
6
3
3
1
2
1
1
28
25
20
21
22
22
23
11
1
1
28
1
1
0
0...

result:

ok 249141 numbers

Test #7:

score: 0
Accepted
time: 125ms
memory: 18168kb

input:

2517
14 35
2 13
14 1
14 14
1 14
7 7
1 14
7 7
2 14
9 11
2 4
2 13
11 12
14 2
13 1
14 1
13 2
12 11
2 14
1 13
12 11
1 14
4 5
2 14
14 14
13 13
10 9
14 2
1 14
14 2
13 2
14 2
7 8
6 6
1 1
13 1
51 125
30 30
40 39
25 24
51 1
1 51
40 40
8 7
50 2
2 50
7 9
50 1
30 30
47 49
14 16
2 4
1 51
1 50
6 6
1 51
4 4
50 2
2...

output:

91
58
58
56
56
56
56
55
37
23
23
17
17
17
17
17
17
17
17
17
17
12
12
12
12
11
11
11
11
11
11
7
7
7
7
1275
1324
1370
1176
1128
1128
1081
991
991
947
946
946
862
782
706
706
706
706
706
706
706
706
706
668
598
598
598
598
532
532
532
532
532
532
532
500
469
469
469
469
411
383
383
383
331
331
331
331
...

result:

ok 250000 numbers

Test #8:

score: 0
Accepted
time: 109ms
memory: 26216kb

input:

5
65849 4012
8907 8905
21927 21927
2 65849
2 65848
65849 1
1 65849
48863 48861
2 65849
7795 7795
65849 2
2 65849
65849 2
2696 2695
65848 2
41766 41765
2 65849
36403 36403
65849 2
32613 32613
26782 26781
60024 60024
44568 44570
5043 5043
52955 52954
15301 15299
65849 2
1 65849
27002 27000
22706 22707...

output:

2168012476
2168078322
2167880786
2167749099
2167683248
2167683247
2167551563
2167551563
2167551563
2167551563
2167551563
2167551563
2167485722
2167485722
2167419882
2167419882
2167419882
2167419882
2167419882
2167354043
2167354043
2167222369
2167222369
2167156533
2167024865
2167024865
2167024865
216...

result:

ok 52236 numbers

Test #9:

score: 0
Accepted
time: 299ms
memory: 41364kb

input:

1
250000 250000
97733 97731
132027 132027
196875 196877
1 250000
170476 170476
44407 44407
250000 1
249999 2
133959 133958
45337 45335
246071 246069
2589 2587
1 249999
172569 172570
2 249999
250000 2
244802 244804
79199 79199
1 249999
249999 1
213003 213003
84015 84014
92491 92492
124485 124485
1803...

output:

31249875000
31250124997
31250374986
31248875012
31248875012
31248875012
31248625017
31248125031
31247875039
31247375059
31246875083
31246375111
31246375110
31246125125
31246125125
31246125125
31245625159
31245625159
31245625159
31245625159
31245625159
31245375177
31245125196
31245125196
31244875216
...

result:

ok 250000 numbers

Test #10:

score: -100
Time Limit Exceeded

input:

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

output:

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

result: