QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#260228#5425. Proposition Compositiongrass8cowWA 146ms27024kbC++174.5kb2023-11-21 22:33:262023-11-21 22:33:27

Judging History

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

  • [2023-11-21 22:33:27]
  • 评测
  • 测评结果:WA
  • 用时:146ms
  • 内存:27024kb
  • [2023-11-21 22:33:26]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,U[301000],V[300100];ll ans[301000];
int fa[300100],sg[300100];
	int F(int x){if(x==fa[x])return x;return fa[x]=F(fa[x]);}
	void sol__(){
		for(int i=1;i<=n;i++)fa[i]=i;for(int i=1;i<n;i++)sg[i]=0;
		int c0=n-1,c1=0;
		for(int i=1;i<=m;i++){
			if(U[i]>V[i])swap(U[i],V[i]);
			int l=F(U[i]),r=F(V[i]);
			while(l!=r){
				if(sg[l]==0)c0--,c1++;else c1--;
				sg[l]++,l=F(l+1);
			}
			l=F(U[i]);
			while(l!=r){int fp=F(l+1);if(sg[l]==2)fa[l]=fp;l=fp;}
			ans[i]=1ll*c0*i+c1+1ll*c0*(n-1-c0);
		}
	}
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int col[301000],pre[301000],nxt[301000],sz[1200100],sc;
	int k1[1201000],k2[1200100];
	ll ns;
	void build(int p,int l,int r){
		if(l==r){k1[p]=k2[p]=l;return;}
		int mid=(l+r)>>1;build(p<<1,l,mid),build(p<<1|1,mid+1,r); 
		k1[p]=(pre[k1[p<<1]]<pre[k1[p<<1|1]])?k1[p<<1]:k1[p<<1|1];
		k2[p]=(nxt[k2[p<<1]]>nxt[k2[p<<1|1]])?k2[p<<1]:k2[p<<1|1];
	}
	int ask1(int p,int l,int r,int x,int y){
		if(x<=l&&r<=y)return k1[p];
		int mid=(l+r)>>1;
		if(y<=mid)return ask1(p<<1,l,mid,x,y);
		if(x>mid)return ask1(p<<1|1,mid+1,r,x,y);
		int o1=ask1(p<<1,l,mid,x,y),o2=ask1(p<<1|1,mid+1,r,x,y);
		return (pre[o1]<pre[o2])?o1:o2;
	}
	int ask2(int p,int l,int r,int x,int y){
		if(x<=l&&r<=y)return k2[p];
		int mid=(l+r)>>1;
		if(y<=mid)return ask2(p<<1,l,mid,x,y);
		if(x>mid)return ask2(p<<1|1,mid+1,r,x,y);
		int o1=ask2(p<<1,l,mid,x,y),o2=ask2(p<<1|1,mid+1,r,x,y);
		return (nxt[o1]>nxt[o2])?o1:o2;
	}
	void up(int p,int l,int r,int x){
		if(l==r){k1[p]=k2[p]=l;return;}
		int mid=(l+r)>>1;if(x<=mid)up(p<<1,l,mid,x);else up(p<<1|1,mid+1,r,x);
		k1[p]=(pre[k1[p<<1]]<pre[k1[p<<1|1]])?k1[p<<1]:k1[p<<1|1];
		k2[p]=(nxt[k2[p<<1]]>nxt[k2[p<<1|1]])?k2[p<<1]:k2[p<<1|1];
	}
	ll C2(ll x){return x*(x-1)/2;}
	void cut1(pi l,pi r){
		nxt[l.se]=r.se,pre[r.se]=l.se;
		up(1,1,n,l.se),up(1,1,n,r.se);
		int co=col[l.fi],S=sz[co],s2=0;
		for(int i=l.fi;i!=r.fi;i=nxt[i]){s2++;if(s2*2>S)break;}
		s2++,sc++;
		if(s2*2<=S){
			sz[co]=S-s2,sz[sc]=s2;
			for(int i=l.fi;i!=r.fi;i=nxt[i])col[i]=sc;col[r.fi]=sc;
		}
		else{
			s2=0;
			for(int i=l.se;i!=n+1;i=pre[i])col[i]=sc,s2++;
			for(int i=r.se;i;i=nxt[i])col[i]=sc,s2++;
			sz[co]=S-s2,sz[sc]=s2;
		}
		ns-=C2(S),ns+=C2(sz[co]),ns+=C2(sz[sc]);
	}
	void cut2(pi x){
		nxt[x.se]=0,up(1,1,n,x.se);
		int co=col[x.fi],S=sz[co],s2=0;
		for(int i=x.fi;i;i=nxt[i]){s2++;if(s2*2>S)break;}
		sc++;
		if(s2*2<=S){
			sz[co]=S-s2,sz[sc]=s2;
			for(int i=x.fi;i;i=nxt[i])col[i]=sc;
		}
		else{
			s2=0;for(int i=x.se;i!=n+1;i=pre[i])col[i]=sc,s2++;
			sz[co]=S-s2,sz[sc]=s2;
		}
		ns-=C2(S),ns+=C2(sz[co]),ns+=C2(sz[sc]);
	}
	void cut3(pi x){
		pre[x.se]=n+1,up(1,1,n,x.se);
		int co=col[x.fi],S=sz[co],s2=0;
		for(int i=x.fi;i!=n+1;i=pre[i]){s2++;if(s2*2>S)break;}
		sc++;
		if(s2*2<=S){
			sz[co]=S-s2,sz[sc]=s2;
			for(int i=x.fi;i!=n+1;i=pre[i])col[i]=sc;
		}
		else{
			s2=0;for(int i=x.se;i;i=nxt[i])col[i]=sc,s2++;
			sz[co]=S-s2,sz[sc]=s2;
		}
		ns-=C2(S),ns+=C2(sz[co]),ns+=C2(sz[sc]);
	}
	int ob[300100];
	void sol_(){
		n--;
		ns=C2(n);
		for(int i=1;i<=n;i++)col[i]=1;sz[1]=n;
		sc=1;
		for(int i=1;i<n;i++)pre[i+1]=i,nxt[i]=i+1;pre[1]=n+1,nxt[n]=0;
		build(1,1,n);
		for(int i=1;i<=m;i++){
			if(U[i]>V[i])swap(U[i],V[i]);if(U[i]==V[i]){
			ans[i]+=ns;continue;};V[i]--;
			vector<pi>o1,o2;
			while(1){
				int sb=ask1(1,1,n,U[i],V[i]),pr=pre[sb];
				up(1,1,n,sb);
				if(pr>=U[i])break;
				o1.pb(mp(sb,pr)),pre[sb]=n+1;
			}
			while(1){
				int sb=ask2(1,1,n,U[i],V[i]),nx=nxt[sb];
				up(1,1,n,sb);
				if(nx<=V[i])break;
				o2.pb(mp(sb,nx)),nxt[sb]=0;
			}
			sort(o1.begin(),o1.end(),[&](pi x,pi y){return col[x.fi]<col[y.fi];}),
			sort(o2.begin(),o2.end(),[&](pi x,pi y){return col[x.fi]<col[y.fi];});
			int i1=0,i2=0;
			while(i1<(int)o1.size()&&i2<(int)o2.size()){
				if(col[o1[i1].fi]==col[o2[i2].fi])cut1(o1[i1],o2[i2]),i1++,i2++;
				else if(col[o1[i1].fi]<col[o2[i2].fi])cut2(o1[i1]),i1++;
				else cut3(o2[i2]),i2++;
			}
			while(i1<(int)o1.size())cut2(o1[i1]),i1++;
			while(i2<(int)o2.size())cut3(o2[i2]),i2++;
			ans[i]+=ns;
		}
	}
void sol(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)scanf("%d%d",&U[i],&V[i]),ans[i]=0;
	if(n==1){for(int i=1;i<=m;i++)puts("0");return;}
	sol__();
	sol_();
	for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
}
int main(){
	int T;scanf("%d",&T);while(T--)sol();
	return 0;
}

詳細信息

Test #1:

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

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: 57ms
memory: 18036kb

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: 67ms
memory: 20080kb

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: 57ms
memory: 21808kb

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: 146ms
memory: 27024kb

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: -100
Wrong Answer
time: 60ms
memory: 17904kb

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:

wrong answer 230th numbers differ - expected: '1', found: '2'