Submission #444370

# Submission time Handle Problem Language Result Execution time Memory
444370 2021-07-13T19:17:52 Z cheetose Making Friends on Joitter is Fun (JOI20_joitter2) C++17
17 / 100
5000 ms 44100 KB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };

int n,m;
ll ans;
set<Pi> v[100001],rev[100001];
int p[100001],sz[100001];
inline ll nP2(int x){return 1ll*x*(x-1);}
int find(int x){
	if(x==p[x])return x;
	return find(p[x]);
}
void merge(int x,int y){
	if(x==y)return;
	ans-=nP2(sz[x])+nP2(sz[y]);
	auto it=v[y].lower_bound(mp(x,-1));
	while(it!=v[y].end() && (it->X)==x){
		ans-=sz[y];
		rev[it->X].erase(mp(y,it->Y));
		it=v[y].erase(it);
	}
	it=v[x].lower_bound(mp(y,-1));
	while(it!=v[x].end() && (it->X)==y){
		ans-=sz[x];
		rev[it->X].erase(mp(x,it->Y));
		it=v[x].erase(it);
	}
	if(v[x].size()+rev[x].size()>v[y].size()+rev[y].size())swap(x,y);
	ans+=1ll*v[y].size()*sz[x]+1ll*v[x].size()*sz[y];
	sz[y]+=sz[x];
	p[x]=y;
	ans+=nP2(sz[y]);
	VPi vv;
	for(it=v[x].begin();it!=v[x].end();it=v[x].erase(it)){
		if(v[y].find(*it)!=v[y].end())ans-=sz[y];
		else v[y].insert(*it);
		rev[it->X].erase(mp(x,it->Y));
		rev[it->X].insert(mp(y,it->Y));
		auto it2=v[it->X].lower_bound(mp(y,-1));
		if(it2!=v[it->X].end() && it2->X==y){
			vv.pb(mp(y,it->X));
		}
	}
	for(it=rev[x].begin();it!=rev[x].end();it=rev[x].erase(it)){
		rev[y].insert(*it);
		v[it->X].erase(mp(x,it->Y));
		v[it->X].insert(mp(y,it->Y));
		auto it2=v[y].lower_bound(mp(it->X,-1));
		if(it2!=v[y].end() && it2->X==it->X){
			vv.pb(mp(x,it->X));
		}
	}
	for(Pi P:vv){
		merge(find(P.X),find(P.Y));
	}
}
int main(){
	scanf("%d%d",&n,&m);
	fup(i,1,n,1){
		sz[i]=1;
		p[i]=i;
	}
	fup(i,1,m,1){
		int x,y;
		scanf("%d%d",&y,&x);
		int xx=find(x),yy=find(y);
		if(xx==yy || v[xx].find(mp(yy,y))!=v[xx].end()){
			printf("%lld\n",ans);
			continue;
		}
		auto it=v[yy].lower_bound(mp(xx,-1));
		if(it==v[yy].end() || (it->X)!=xx){
			ans+=sz[xx];
			v[xx].insert(mp(yy,y));
			rev[yy].insert(mp(xx,y));
			printf("%lld\n",ans);
			continue;
		}else{
			merge(xx,yy);
			printf("%lld\n",ans);
		}
	}
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
joitter2.cpp:98:2: note: in expansion of macro 'fup'
   98 |  fup(i,1,n,1){
      |  ^~~
joitter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
joitter2.cpp:102:2: note: in expansion of macro 'fup'
  102 |  fup(i,1,m,1){
      |  ^~~
joitter2.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |  scanf("%d%d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~
joitter2.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |   scanf("%d%d",&y,&x);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9700 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 6 ms 9596 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 6 ms 9708 KB Output is correct
8 Correct 6 ms 9656 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9688 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9692 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 5 ms 9676 KB Output is correct
16 Correct 5 ms 9696 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
22 Correct 5 ms 9692 KB Output is correct
23 Correct 6 ms 9676 KB Output is correct
24 Correct 6 ms 9676 KB Output is correct
25 Correct 6 ms 9676 KB Output is correct
26 Correct 5 ms 9676 KB Output is correct
27 Correct 6 ms 9676 KB Output is correct
28 Correct 5 ms 9676 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 5 ms 9676 KB Output is correct
31 Correct 6 ms 9616 KB Output is correct
32 Correct 7 ms 9676 KB Output is correct
33 Correct 6 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9700 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 6 ms 9596 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 6 ms 9708 KB Output is correct
8 Correct 6 ms 9656 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9688 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9692 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 5 ms 9676 KB Output is correct
16 Correct 5 ms 9696 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
22 Correct 5 ms 9692 KB Output is correct
23 Correct 6 ms 9676 KB Output is correct
24 Correct 6 ms 9676 KB Output is correct
25 Correct 6 ms 9676 KB Output is correct
26 Correct 5 ms 9676 KB Output is correct
27 Correct 6 ms 9676 KB Output is correct
28 Correct 5 ms 9676 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 5 ms 9676 KB Output is correct
31 Correct 6 ms 9616 KB Output is correct
32 Correct 7 ms 9676 KB Output is correct
33 Correct 6 ms 9676 KB Output is correct
34 Correct 8 ms 9828 KB Output is correct
35 Correct 144 ms 15176 KB Output is correct
36 Correct 140 ms 17348 KB Output is correct
37 Correct 136 ms 17348 KB Output is correct
38 Correct 141 ms 17152 KB Output is correct
39 Correct 8 ms 9700 KB Output is correct
40 Correct 8 ms 9804 KB Output is correct
41 Correct 8 ms 9836 KB Output is correct
42 Correct 17 ms 9776 KB Output is correct
43 Correct 8 ms 9804 KB Output is correct
44 Correct 8 ms 9832 KB Output is correct
45 Correct 12 ms 9704 KB Output is correct
46 Correct 12 ms 9700 KB Output is correct
47 Correct 8 ms 9828 KB Output is correct
48 Correct 9 ms 9804 KB Output is correct
49 Correct 16 ms 10444 KB Output is correct
50 Correct 142 ms 17424 KB Output is correct
51 Correct 12 ms 10060 KB Output is correct
52 Correct 141 ms 16356 KB Output is correct
53 Correct 15 ms 10344 KB Output is correct
54 Correct 130 ms 16852 KB Output is correct
55 Correct 10 ms 10024 KB Output is correct
56 Correct 10 ms 10144 KB Output is correct
57 Correct 11 ms 10564 KB Output is correct
58 Correct 10 ms 10600 KB Output is correct
59 Correct 7 ms 9676 KB Output is correct
60 Correct 123 ms 14644 KB Output is correct
61 Correct 9 ms 9804 KB Output is correct
62 Correct 156 ms 17020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9700 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 6 ms 9596 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 6 ms 9708 KB Output is correct
8 Correct 6 ms 9656 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9688 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9692 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 5 ms 9676 KB Output is correct
16 Correct 5 ms 9696 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
22 Correct 5 ms 9692 KB Output is correct
23 Correct 6 ms 9676 KB Output is correct
24 Correct 6 ms 9676 KB Output is correct
25 Correct 6 ms 9676 KB Output is correct
26 Correct 5 ms 9676 KB Output is correct
27 Correct 6 ms 9676 KB Output is correct
28 Correct 5 ms 9676 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 5 ms 9676 KB Output is correct
31 Correct 6 ms 9616 KB Output is correct
32 Correct 7 ms 9676 KB Output is correct
33 Correct 6 ms 9676 KB Output is correct
34 Correct 8 ms 9828 KB Output is correct
35 Correct 144 ms 15176 KB Output is correct
36 Correct 140 ms 17348 KB Output is correct
37 Correct 136 ms 17348 KB Output is correct
38 Correct 141 ms 17152 KB Output is correct
39 Correct 8 ms 9700 KB Output is correct
40 Correct 8 ms 9804 KB Output is correct
41 Correct 8 ms 9836 KB Output is correct
42 Correct 17 ms 9776 KB Output is correct
43 Correct 8 ms 9804 KB Output is correct
44 Correct 8 ms 9832 KB Output is correct
45 Correct 12 ms 9704 KB Output is correct
46 Correct 12 ms 9700 KB Output is correct
47 Correct 8 ms 9828 KB Output is correct
48 Correct 9 ms 9804 KB Output is correct
49 Correct 16 ms 10444 KB Output is correct
50 Correct 142 ms 17424 KB Output is correct
51 Correct 12 ms 10060 KB Output is correct
52 Correct 141 ms 16356 KB Output is correct
53 Correct 15 ms 10344 KB Output is correct
54 Correct 130 ms 16852 KB Output is correct
55 Correct 10 ms 10024 KB Output is correct
56 Correct 10 ms 10144 KB Output is correct
57 Correct 11 ms 10564 KB Output is correct
58 Correct 10 ms 10600 KB Output is correct
59 Correct 7 ms 9676 KB Output is correct
60 Correct 123 ms 14644 KB Output is correct
61 Correct 9 ms 9804 KB Output is correct
62 Correct 156 ms 17020 KB Output is correct
63 Correct 394 ms 43996 KB Output is correct
64 Correct 443 ms 44100 KB Output is correct
65 Correct 397 ms 44088 KB Output is correct
66 Correct 114 ms 14788 KB Output is correct
67 Correct 208 ms 18872 KB Output is correct
68 Execution timed out 5051 ms 11716 KB Time limit exceeded
69 Halted 0 ms 0 KB -