Submission #444370

#TimeUsernameProblemLanguageResultExecution timeMemory
444370cheetoseMaking Friends on Joitter is Fun (JOI20_joitter2)C++17
17 / 100
5051 ms44100 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...