Submission #363721

# Submission time Handle Problem Language Result Execution time Memory
363721 2021-02-07T04:06:50 Z fhvirus Ideal city (IOI12_city) C++14
Compilation error
0 ms 0 KB
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define FOR(i,n) for(int i=0;i<(n);++i)
#define FOO(i,a,b) for(int i=(a);i<=int(b);++i)
#define OOF(i,a,b) for(int i=(a);i>=int(b);--i)
#define AI(x) (x).begin(),(x).end()
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;} 
template<class V>void lisan(V&v){sort(AI(v));v.erase(unique(AI(v)),v.end());}
template<class V,class T>int lspos(const V&v,T x){return lower_bound(AI(v),x)-v.begin();}
template<class...T>void RI(T&...t){(...,(cin>>t));}
template<class...T>void PL(T...t){int c=sizeof...(T);if(!c){cout<<'\n';return;}(...,(cout<<t<<(--c?' ':'\n')));}
constexpr inline ll cdiv(ll x,ll m){return x/m+(x%m?(x<0)^(m>0):0);}
constexpr inline ll mpow(ll x,ll e,ll m){ll r=1;for(x%=m;e;e/=2,x=x*x%m)if(e&1)r=r*x%m;return r;}
#ifdef OWO
#define safe cerr<<"\033[1;32m"<<__PRETTY_FUNCTION__<<" - "<<__LINE__<<" JIZZ\033[0m\n"
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}
template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
#else
#pragma GCC optimize("Ofast")
#define safe ((void)0)
#define debug(...) ((void)0)
#endif
constexpr ll inf = 1e9, INF = 4e18;

#ifdef OWO
const int N = 1e5 + 225;
int n, x[N], y[N];
int DistanceSum(int N, int *X, int *Y);
int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n;
	FOR(i,n) cin >> x[i] >> y[i];
	cout << DistanceSum(n, x, y) << endl;
	return 0;
}
#endif

struct DSU{
	vector<int> g, s;
	DSU(int n){
		g.resize(n), s.resize(n, 1), iota(AI(g), 0);
	}
	int F(int a){ return a == g[a] ? a : g[a] = F(g[a]);}
	bool M(int a, int b){
		a = F(a), b = F(b);
		if(a == b) return false;
		if(s[a] < s[b]) swap(a, b);
		s[a] += s[b], g[b] = a;
		return true;
	}
	int S(int a){ return s[F(a)];}
	int operator()(int a){ return F(a);}
};

ll subsz[N];
vector<int> adj[N];
ll dfs(int u, ll allsz, int p = -1){
	ll ans = 0;
	for(int v: adj[u]){
		if(v != p){
			ans += dfs(v, allsz, u);
			subsz[u] += subsz[v];
		}
	}
	return ans + subsz[u] * (allsz - subsz[u]);
}
ll cal(int N, int *X, int *Y){
	DSU D(N);
	vector<pii> edges;
	// make vertex
	{
		map<int, vector<pii>> col;
		FOR(i,N) col[X[i]].pb(Y[i], i);
		for(auto &[_, vec]: col){
			sort(AI(vec));
			FOO(i,1,vec.size()-1)
				if(vec[i-1].ff + 1 == vec[i].ff)
					D.M(vec[i-1].ss, vec[i].ss);
		}
	}
	// make edges
	{
		map<pii, int> pos;
		auto upd = [&pos, &D, &edges](int i, int x, int y){
			if(!pos.count({x, y})) return;
			int j = pos[{x, y}];
			i = D(i), j = D(j);
			if(i > j) swap(i, j);
			edges.pb(i, j);
		};
		FOR(i,N){
			int x = X[i], y = Y[i];
			upd(i, x + 1, y), upd(i, x - 1, y);
			pos[{x, y}] = i;
		}
	}
	lisan(edges);
	// make graph
	FOR(i,n) adj[i].clear(), subsz[i] = D.S(i);
	for(auto [u, v]: edges) adj[u].pb(v), adj[v].pb(u);
	// dfs
	return dfs(D(0), N);
}

int DistanceSum(int N, int *X, int *Y){
	return (cal(N, X, Y) + cal(N, Y, X)) % 1'000'000'000ll;
}

Compilation message

city.cpp: In function 'void RI(T& ...)':
city.cpp:17:48: warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17'
   17 | template<class...T>void RI(T&...t){(...,(cin>>t));}
      |                                                ^
city.cpp: In function 'void PL(T ...)':
city.cpp:18:109: warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17'
   18 | template<class...T>void PL(T...t){int c=sizeof...(T);if(!c){cout<<'\n';return;}(...,(cout<<t<<(--c?' ':'\n')));}
      |                                                                                                             ^
city.cpp: At global scope:
city.cpp:68:10: error: 'N' was not declared in this scope
   68 | ll subsz[N];
      |          ^
city.cpp:69:17: error: 'N' was not declared in this scope
   69 | vector<int> adj[N];
      |                 ^
city.cpp: In function 'll dfs(int, ll, int)':
city.cpp:72:13: error: 'adj' was not declared in this scope
   72 |  for(int v: adj[u]){
      |             ^~~
city.cpp:75:4: error: 'subsz' was not declared in this scope
   75 |    subsz[u] += subsz[v];
      |    ^~~~~
city.cpp:78:15: error: 'subsz' was not declared in this scope
   78 |  return ans + subsz[u] * (allsz - subsz[u]);
      |               ^~~~~
city.cpp: In function 'll cal(int, int*, int*)':
city.cpp:87:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   87 |   for(auto &[_, vec]: col){
      |             ^
city.cpp:112:8: error: 'n' was not declared in this scope
  112 |  FOR(i,n) adj[i].clear(), subsz[i] = D.S(i);
      |        ^
city.cpp:9:33: note: in definition of macro 'FOR'
    9 | #define FOR(i,n) for(int i=0;i<(n);++i)
      |                                 ^
city.cpp:112:11: error: 'adj' was not declared in this scope
  112 |  FOR(i,n) adj[i].clear(), subsz[i] = D.S(i);
      |           ^~~
city.cpp:112:27: error: 'subsz' was not declared in this scope
  112 |  FOR(i,n) adj[i].clear(), subsz[i] = D.S(i);
      |                           ^~~~~
city.cpp:113:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  113 |  for(auto [u, v]: edges) adj[u].pb(v), adj[v].pb(u);
      |           ^
city.cpp:113:26: error: 'adj' was not declared in this scope
  113 |  for(auto [u, v]: edges) adj[u].pb(v), adj[v].pb(u);
      |                          ^~~