Submission #702373

#TimeUsernameProblemLanguageResultExecution timeMemory
702373Ronin13Ideal city (IOI12_city)C++14
100 / 100
172 ms61732 KiB
#include <bits/stdc++.h> #define ll long long #define ull unsigned ll #define f first #define s second #define pii pair <int, int> #define pll pair<ll,ll> #define pb push_back #define epb emplace_back using namespace std; const ll mod = 1e18; map <pll, int> mp; const int nmax = 1000001; vector <vector <int> > g(nmax); vector <ll> dp(nmax); ll ans = 0; vector <ll> sz(nmax); vector <ll> a(nmax); int cnt = 1; /* 11 2 5 2 6 3 3 4 3 4 4 4 5 4 6 5 3 5 4 5 6 3 6 */ ll add(ll a, ll b){ return (a + b) % mod; } ll subs(ll a, ll b){ return (a - b + mod) % mod; } ll mult(ll a, ll b){ return a * b % mod; } void dfs(int v, int e = -1){ sz[v] = a[v]; dp[v] = 0; for(int to : g[v]){ if(to == e) continue; dfs(to, v); dp[v] = add(dp[v], dp[to]); dp[v] = add(dp[v], sz[to]); sz[v] = add(sz[v], sz[to]); } } void DFS(int v, int e = -1){ ans = add(ans, mult(dp[v], a[v])); for(int to : g[v]){ if(to == e) continue; ll pr1 = dp[to], pr2 = sz[to]; sz[v] = subs(sz[v], sz[to]); dp[v] = subs(dp[v], add(dp[to], sz[to])); dp[to] = add(dp[to], add(dp[v], sz[v])); sz[to] = add(sz[v], sz[to]); DFS(to, v); sz[to] = pr2; dp[to] = pr1; sz[v] = add(sz[v], sz[to]); dp[v] = add(dp[v], add(dp[to],sz[to])); } } int DistanceSum(int N, int *X, int *Y) { vector <pii > vec; int n = N; for(int i = 0; i < n ;i++){ vec.pb({X[i], Y[i]}); } sort(vec.begin(), vec.end()); for(int i = 0; i < vec.size(); i++){ if(i == 0){ mp[vec[i]] = cnt++; a[cnt - 1] = 1; continue; } ll x = vec[i].f, y = vec[i].s; if(!mp[{x, y - 1}]) mp[{x, y}] = cnt++, a[cnt - 1]++; else mp[{x, y}] = cnt - 1, a[cnt - 1]++; if(mp[{x - 1, y}]){ if((!mp[{x - 1, y - 1}]) || (!mp[{x, y - 1}])){ int u = mp[{x, y}]; int v = mp[{x - 1, y}]; g[u].pb(v); g[v].pb(u); } } } //cout << 1; dfs(1); DFS(1); for(int i = 0; i < cnt; i++){ g[i].clear(); a[i] = 0; } vec.clear(); mp.clear(); for(int i = 0; i < n ;i++){ vec.pb({Y[i], X[i]}); } cnt = 1; sort(vec.begin(), vec.end()); for(int i = 0; i < vec.size(); i++){ if(i == 0){ mp[vec[i]] = cnt++; a[cnt - 1] =1; continue; } ll x = vec[i].f, y = vec[i].s; if(!mp[{x, y - 1}]) mp[{x, y}] = cnt++, a[cnt - 1]++; else mp[{x, y}] = cnt- 1, a[cnt - 1]++; if(mp[{x - 1, y}]){ if((!mp[{x - 1, y - 1}]) || (!mp[{x, y - 1}])){ int u = mp[{x, y}]; int v = mp[{x - 1, y}]; g[u].pb(v); g[v].pb(u); } } } dfs(1); DFS(1); return (ans / 2) % (ll)(1e9); }

Compilation message (stderr)

city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for(int i = 0; i < vec.size(); i++){
      |                    ~~^~~~~~~~~~~~
city.cpp:118:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |     for(int i = 0; i < vec.size(); i++){
      |                    ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...