Submission #227479

#TimeUsernameProblemLanguageResultExecution timeMemory
227479MercenaryIdeal city (IOI12_city)C++14
100 / 100
76 ms12400 KiB
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/trie_policy.hpp> #define pb push_back #define mp make_pair #define taskname "A" using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef pair<int,int> ii; typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; const int maxn = 1e5 + 5; int n; vector<ii> a; ll ans = 0; int gr[maxn]; int sz[maxn]; set<int> adj[maxn]; bool vis[maxn]; void dfs(int u){ vis[u] = 1; for(auto c : adj[u]){ if(!vis[c]){ dfs(c); sz[u] += sz[c]; } } ans += 1ll * sz[u] * (n - sz[u]); } void solve(){ for(int i = 0 ; i < n ; ++i)adj[i].clear(); sort(a.begin(),a.end()); int m = 0; for(int i = 0 ; i < n ; ){ int j = i + 1; gr[i] = m; while(j < n && a[j] == mp(a[j - 1].first , a[j - 1].second + 1))gr[j++] = m; sz[m] = j - i; i = j; ++m; } for(int i = 0 , j = 0 ; i < n ; ++i){ while(j < n && a[j] < mp(a[i].first - 1 , a[i].second))++j; if(j < n && a[j] == mp(a[i].first - 1 , a[i].second)){ adj[gr[i]].insert(gr[j]); adj[gr[j]].insert(gr[i]); // cout << gr[i] << " " << gr[j] << endl; } } memset(vis,0,sizeof vis); dfs(0); } int DistanceSum(int N, int *X, int *Y) { n = N; for(int i = 0 ; i < n ; ++i)a.pb(mp(X[i],Y[i])); solve(); for(int i = 0 ; i < n ; ++i)swap(a[i].first,a[i].second); solve(); return ans % (int)1e9; } #ifdef LOCAL #include <stdio.h> #include <stdlib.h> #include <assert.h> #define inbuf_len 1 << 16 #define outbuf_len 1 << 16 int DistanceSum(int N, int *X, int *Y); int main() { int tmp; /* Set input and output buffering */ freopen("A.INP","r",stdin); freopen("A.OUT","w",stdout); int N, i; tmp = scanf("%d", &N); assert(tmp == 1); int *sq_x, *sq_y; sq_x = (int*) malloc(N * sizeof(int)); sq_y = (int*) malloc(N * sizeof(int)); for (i = 0; i < N; i++) { tmp = scanf("%d %d", &sq_x[i], &sq_y[i]); assert(tmp == 2); } int ds = DistanceSum(N, sq_x, sq_y); printf("%d\n", ds); return 0; } #endif // LOCAL
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...