답안 #520424

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520424 2022-01-29T23:52:00 Z new_acc 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
832 ms 68836 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(a, b) for(int a = 0; a < (b); a++)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
const int N=1e5+10;
vi f[N];
int fau[N];
ll w[N];
set<int> ini[N];
set<int> outi[N];
set<int> ini2[N];
bool vis[N];
ll res;
vi Union(int a,int b){
	res-=(ll)(f[a].size())*(ll)(f[a].size()-1);
	res-=(ll)(f[b].size())*(ll)(f[b].size()-1);
	a=fau[a],b=fau[b];
	vi p;
	if(f[a].size()>f[b].size()) swap(a,b);
	rep(i,f[a].size()){
		fau[f[a][i]]=b,f[b].push_back(f[a][i]);
		auto it=ini2[b].lower_bound(f[a][i]);
		if(it!=ini2[b].end() and (*it)==f[a][i]) ini2[b].erase(f[a][i]);
	}
	f[a].clear();
	for(auto it=ini2[a].begin();it!=ini2[a].end();it++) if(fau[(*it)]!=b) ini2[b].insert(*it);
	ini2[a].clear();
	for(auto it=ini[a].begin();it!=ini[a].end();it++){
		auto it2=outi[b].lower_bound(*it);
		if(!vis[*it2] and it2!=outi[b].end() and *it2==*it) p.push_back(*it2),vis[*it2]=1;
	}
	for(auto it=outi[a].begin();it!=outi[a].end();it++){
		auto it2=ini[b].lower_bound(*it);
		if(!vis[*it2] and it2!=ini[b].end() and *it==*it2) p.push_back(*it2),vis[*it2]=1;
	}
	for(auto it=ini[a].begin();it!=ini[a].end();it++){
		if((*it)!=b){
			outi[*it].erase(a);
			outi[*it].insert(b);
		}
	}
	for(auto it=outi[a].begin();it!=outi[a].end();it++){
		if((*it)!=b){
			ini[*it].erase(a);
			ini[*it].insert(b);
		}
	}
	for(auto it=ini[a].begin();it!=ini[a].end();it++) if(*it!=b) ini[b].insert(*it);
	for(auto it=outi[a].begin();it!=outi[a].end();it++) if(*it!=b) outi[b].insert(*it);
	outi[b].erase(a),ini[b].erase(a);
	ini[a].clear(),outi[a].clear();
	res-=w[a],res-=w[b];
	w[b]=(ll)(ini2[b].size())*(ll)(f[b].size());
	res+=w[b];
	res+=(ll)(f[b].size())*(ll)(f[b].size()-1);
	return p;
}
void solve(){
	int n,q;
	cin>>n>>q;
	for(int i=1;i<=n;i++) fau[i]=i,f[i].push_back(i);
	rep(i,q){
		int a,b;
		cin>>a>>b;
		b=fau[b];
		auto it2=ini2[b].lower_bound(a);
		if((it2!=ini2[b].end() and (*it2)==a) or fau[a]==b){cout<<res<<"\n";continue;}
		ini2[b].insert(a);
		a=fau[a];	
		res-=w[b];
		outi[a].insert(b),ini[b].insert(a);
		w[b]=(ll)(ini2[b].size())*(ll)(f[b].size());
		res+=w[b];
		auto it=outi[b].lower_bound(a);
		if(it==outi[b].end() or (*it)!=a){cout<<res<<"\n"; continue;}
		vi xd=Union(a,b);
		int v=fau[a];
		while(xd.size()){
			vi pom=Union(v,xd.back());
			vis[xd.back()]=0;
			xd.pop_back();
			rep(j,pom.size()) xd.push_back(pom[j]);
			v=fau[v];
		}
		cout<<res<<"\n";
	}
}
int main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
	solve();
}

Compilation message

joitter2.cpp: In function 'vi Union(int, int)':
joitter2.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define rep(a, b) for(int a = 0; a < (b); a++)
      |                                    ^
joitter2.cpp:24:2: note: in expansion of macro 'rep'
   24 |  rep(i,f[a].size()){
      |  ^~~
joitter2.cpp: In function 'void solve()':
joitter2.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define rep(a, b) for(int a = 0; a < (b); a++)
      |                                    ^
joitter2.cpp:86:4: note: in expansion of macro 'rep'
   86 |    rep(j,pom.size()) xd.push_back(pom[j]);
      |    ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16716 KB Output is correct
2 Correct 9 ms 16720 KB Output is correct
3 Correct 8 ms 16716 KB Output is correct
4 Correct 8 ms 16768 KB Output is correct
5 Correct 9 ms 16716 KB Output is correct
6 Correct 9 ms 16716 KB Output is correct
7 Correct 12 ms 16716 KB Output is correct
8 Correct 9 ms 16776 KB Output is correct
9 Correct 8 ms 16720 KB Output is correct
10 Correct 9 ms 16720 KB Output is correct
11 Correct 9 ms 16720 KB Output is correct
12 Correct 8 ms 16756 KB Output is correct
13 Correct 8 ms 16720 KB Output is correct
14 Correct 8 ms 16720 KB Output is correct
15 Correct 8 ms 16720 KB Output is correct
16 Correct 9 ms 16756 KB Output is correct
17 Correct 9 ms 16720 KB Output is correct
18 Correct 9 ms 16776 KB Output is correct
19 Correct 9 ms 16768 KB Output is correct
20 Correct 9 ms 16772 KB Output is correct
21 Correct 9 ms 16776 KB Output is correct
22 Correct 9 ms 16720 KB Output is correct
23 Correct 9 ms 16816 KB Output is correct
24 Correct 9 ms 16720 KB Output is correct
25 Correct 9 ms 16720 KB Output is correct
26 Correct 9 ms 16720 KB Output is correct
27 Correct 9 ms 16768 KB Output is correct
28 Correct 9 ms 16720 KB Output is correct
29 Correct 9 ms 16720 KB Output is correct
30 Correct 9 ms 16720 KB Output is correct
31 Correct 9 ms 16720 KB Output is correct
32 Correct 9 ms 16760 KB Output is correct
33 Correct 9 ms 16780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16716 KB Output is correct
2 Correct 9 ms 16720 KB Output is correct
3 Correct 8 ms 16716 KB Output is correct
4 Correct 8 ms 16768 KB Output is correct
5 Correct 9 ms 16716 KB Output is correct
6 Correct 9 ms 16716 KB Output is correct
7 Correct 12 ms 16716 KB Output is correct
8 Correct 9 ms 16776 KB Output is correct
9 Correct 8 ms 16720 KB Output is correct
10 Correct 9 ms 16720 KB Output is correct
11 Correct 9 ms 16720 KB Output is correct
12 Correct 8 ms 16756 KB Output is correct
13 Correct 8 ms 16720 KB Output is correct
14 Correct 8 ms 16720 KB Output is correct
15 Correct 8 ms 16720 KB Output is correct
16 Correct 9 ms 16756 KB Output is correct
17 Correct 9 ms 16720 KB Output is correct
18 Correct 9 ms 16776 KB Output is correct
19 Correct 9 ms 16768 KB Output is correct
20 Correct 9 ms 16772 KB Output is correct
21 Correct 9 ms 16776 KB Output is correct
22 Correct 9 ms 16720 KB Output is correct
23 Correct 9 ms 16816 KB Output is correct
24 Correct 9 ms 16720 KB Output is correct
25 Correct 9 ms 16720 KB Output is correct
26 Correct 9 ms 16720 KB Output is correct
27 Correct 9 ms 16768 KB Output is correct
28 Correct 9 ms 16720 KB Output is correct
29 Correct 9 ms 16720 KB Output is correct
30 Correct 9 ms 16720 KB Output is correct
31 Correct 9 ms 16720 KB Output is correct
32 Correct 9 ms 16760 KB Output is correct
33 Correct 9 ms 16780 KB Output is correct
34 Correct 10 ms 16848 KB Output is correct
35 Correct 76 ms 22536 KB Output is correct
36 Correct 97 ms 25048 KB Output is correct
37 Correct 99 ms 25320 KB Output is correct
38 Correct 93 ms 24904 KB Output is correct
39 Correct 10 ms 16892 KB Output is correct
40 Correct 12 ms 17104 KB Output is correct
41 Correct 14 ms 17088 KB Output is correct
42 Correct 10 ms 16840 KB Output is correct
43 Correct 12 ms 17104 KB Output is correct
44 Correct 12 ms 17032 KB Output is correct
45 Correct 9 ms 16848 KB Output is correct
46 Correct 10 ms 16840 KB Output is correct
47 Correct 11 ms 17104 KB Output is correct
48 Correct 12 ms 17104 KB Output is correct
49 Correct 17 ms 17744 KB Output is correct
50 Correct 109 ms 25424 KB Output is correct
51 Correct 15 ms 17352 KB Output is correct
52 Correct 84 ms 23744 KB Output is correct
53 Correct 18 ms 17616 KB Output is correct
54 Correct 98 ms 24452 KB Output is correct
55 Correct 14 ms 17488 KB Output is correct
56 Correct 16 ms 17464 KB Output is correct
57 Correct 14 ms 17508 KB Output is correct
58 Correct 14 ms 17488 KB Output is correct
59 Correct 11 ms 17040 KB Output is correct
60 Correct 70 ms 22028 KB Output is correct
61 Correct 13 ms 17104 KB Output is correct
62 Correct 95 ms 24648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16716 KB Output is correct
2 Correct 9 ms 16720 KB Output is correct
3 Correct 8 ms 16716 KB Output is correct
4 Correct 8 ms 16768 KB Output is correct
5 Correct 9 ms 16716 KB Output is correct
6 Correct 9 ms 16716 KB Output is correct
7 Correct 12 ms 16716 KB Output is correct
8 Correct 9 ms 16776 KB Output is correct
9 Correct 8 ms 16720 KB Output is correct
10 Correct 9 ms 16720 KB Output is correct
11 Correct 9 ms 16720 KB Output is correct
12 Correct 8 ms 16756 KB Output is correct
13 Correct 8 ms 16720 KB Output is correct
14 Correct 8 ms 16720 KB Output is correct
15 Correct 8 ms 16720 KB Output is correct
16 Correct 9 ms 16756 KB Output is correct
17 Correct 9 ms 16720 KB Output is correct
18 Correct 9 ms 16776 KB Output is correct
19 Correct 9 ms 16768 KB Output is correct
20 Correct 9 ms 16772 KB Output is correct
21 Correct 9 ms 16776 KB Output is correct
22 Correct 9 ms 16720 KB Output is correct
23 Correct 9 ms 16816 KB Output is correct
24 Correct 9 ms 16720 KB Output is correct
25 Correct 9 ms 16720 KB Output is correct
26 Correct 9 ms 16720 KB Output is correct
27 Correct 9 ms 16768 KB Output is correct
28 Correct 9 ms 16720 KB Output is correct
29 Correct 9 ms 16720 KB Output is correct
30 Correct 9 ms 16720 KB Output is correct
31 Correct 9 ms 16720 KB Output is correct
32 Correct 9 ms 16760 KB Output is correct
33 Correct 9 ms 16780 KB Output is correct
34 Correct 10 ms 16848 KB Output is correct
35 Correct 76 ms 22536 KB Output is correct
36 Correct 97 ms 25048 KB Output is correct
37 Correct 99 ms 25320 KB Output is correct
38 Correct 93 ms 24904 KB Output is correct
39 Correct 10 ms 16892 KB Output is correct
40 Correct 12 ms 17104 KB Output is correct
41 Correct 14 ms 17088 KB Output is correct
42 Correct 10 ms 16840 KB Output is correct
43 Correct 12 ms 17104 KB Output is correct
44 Correct 12 ms 17032 KB Output is correct
45 Correct 9 ms 16848 KB Output is correct
46 Correct 10 ms 16840 KB Output is correct
47 Correct 11 ms 17104 KB Output is correct
48 Correct 12 ms 17104 KB Output is correct
49 Correct 17 ms 17744 KB Output is correct
50 Correct 109 ms 25424 KB Output is correct
51 Correct 15 ms 17352 KB Output is correct
52 Correct 84 ms 23744 KB Output is correct
53 Correct 18 ms 17616 KB Output is correct
54 Correct 98 ms 24452 KB Output is correct
55 Correct 14 ms 17488 KB Output is correct
56 Correct 16 ms 17464 KB Output is correct
57 Correct 14 ms 17508 KB Output is correct
58 Correct 14 ms 17488 KB Output is correct
59 Correct 11 ms 17040 KB Output is correct
60 Correct 70 ms 22028 KB Output is correct
61 Correct 13 ms 17104 KB Output is correct
62 Correct 95 ms 24648 KB Output is correct
63 Correct 480 ms 68708 KB Output is correct
64 Correct 466 ms 68780 KB Output is correct
65 Correct 506 ms 68836 KB Output is correct
66 Correct 115 ms 26300 KB Output is correct
67 Correct 278 ms 36860 KB Output is correct
68 Correct 113 ms 26424 KB Output is correct
69 Correct 347 ms 33260 KB Output is correct
70 Correct 122 ms 26352 KB Output is correct
71 Correct 118 ms 26372 KB Output is correct
72 Correct 348 ms 34160 KB Output is correct
73 Correct 341 ms 34668 KB Output is correct
74 Correct 832 ms 45376 KB Output is correct
75 Correct 532 ms 39104 KB Output is correct
76 Correct 670 ms 44472 KB Output is correct
77 Correct 668 ms 44696 KB Output is correct
78 Correct 184 ms 34152 KB Output is correct
79 Correct 306 ms 37720 KB Output is correct
80 Correct 232 ms 34236 KB Output is correct
81 Correct 399 ms 37772 KB Output is correct
82 Correct 664 ms 54332 KB Output is correct
83 Correct 660 ms 54252 KB Output is correct
84 Correct 561 ms 54060 KB Output is correct
85 Correct 558 ms 54104 KB Output is correct
86 Correct 157 ms 36532 KB Output is correct
87 Correct 183 ms 38536 KB Output is correct
88 Correct 342 ms 34368 KB Output is correct
89 Correct 654 ms 43932 KB Output is correct