Submission #227479

# Submission time Handle Problem Language Result Execution time Memory
227479 2020-04-27T14:32:10 Z Mercenary Ideal city (IOI12_city) C++14
100 / 100
76 ms 12400 KB
#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 time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5248 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 8 ms 5248 KB Output is correct
6 Correct 8 ms 5248 KB Output is correct
7 Correct 8 ms 5248 KB Output is correct
8 Correct 8 ms 5248 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 9 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 5884 KB Output is correct
2 Correct 16 ms 5884 KB Output is correct
3 Correct 30 ms 6780 KB Output is correct
4 Correct 32 ms 6644 KB Output is correct
5 Correct 54 ms 8048 KB Output is correct
6 Correct 53 ms 8048 KB Output is correct
7 Correct 55 ms 8432 KB Output is correct
8 Correct 54 ms 8048 KB Output is correct
9 Correct 54 ms 8432 KB Output is correct
10 Correct 58 ms 12400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 6524 KB Output is correct
2 Correct 17 ms 6268 KB Output is correct
3 Correct 38 ms 8572 KB Output is correct
4 Correct 43 ms 7804 KB Output is correct
5 Correct 71 ms 11888 KB Output is correct
6 Correct 59 ms 9584 KB Output is correct
7 Correct 76 ms 12016 KB Output is correct
8 Correct 61 ms 9712 KB Output is correct
9 Correct 61 ms 9200 KB Output is correct
10 Correct 58 ms 8944 KB Output is correct