Submission #103258

# Submission time Handle Problem Language Result Execution time Memory
103258 2019-03-29T11:20:00 Z wxh010910 Ideal city (IOI12_city) C++17
100 / 100
112 ms 13692 KB
#include <bits/stdc++.h>

using namespace std;

struct point {
  int x, y, id;

  point(int x = 0, int y = 0, int id = 0): x(x), y(y), id(id) {
  }
};

int DistanceSum(int N, int* X, int* Y) {
  vector<point> p(N);
  for (int i = 0; i < N; ++i) {
    p[i] = point(X[i], Y[i], i);
  }
  sort(p.begin(), p.end(), [&](const point &a, const point &b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y);
  });
  vector<int> to_down(N);
  vector<int> down(N, -1);
  for (int i = 0; i < N; ++i) {
    if (i && p[i].x == p[i - 1].x && p[i].y == p[i - 1].y + 1) {
      to_down[p[i].id] = to_down[p[i - 1].id];
      down[p[i].id] = p[i - 1].id;
    } else {
      to_down[p[i].id] = p[i].id;
    }
  }
  sort(p.begin(), p.end(), [&](const point &a, const point &b) {
    return a.y < b.y || (a.y == b.y && a.x < b.x);
  });
  vector<int> to_left(N);
  vector<int> left(N, -1);
  for (int i = 0; i < N; ++i) {
    if (i && p[i].y == p[i - 1].y && p[i].x == p[i - 1].x + 1) {
      to_left[p[i].id] = to_left[p[i - 1].id];
      left[p[i].id] = p[i - 1].id;
    } else {
      to_left[p[i].id] = p[i].id;
    }
  }
  vector<int> sz_down(N);
  for (int i = 0; i < N; ++i) {
    ++sz_down[to_down[i]];
  }
  vector<int> sz_left(N);
  for (int i = 0; i < N; ++i) {
    ++sz_left[to_left[i]];
  }
  long long ans = 0;
  vector<vector<int>> hor(N);
  for (int i = 0; i < N; ++i) {
    if (left[i] != -1 && (to_down[i] == i || to_down[left[i]] == left[i])) {
      hor[to_down[left[i]]].push_back(to_down[i]);
      hor[to_down[i]].push_back(to_down[left[i]]);
    }
  }
  function<void(int, int)> dfs_hor = [&](int x, int pr) {
    for (auto y : hor[x]) {
      if (y != pr) {
        dfs_hor(y, x);
        ans += (long long) sz_down[y] * (N - sz_down[y]);
        sz_down[x] += sz_down[y];
      }
    }
  };
  for (int i = 0; i < N; ++i) {
    if (sz_down[i]) {
      dfs_hor(i, -1);
      break;
    }
  }
  vector<vector<int>> ver(N);
  for (int i = 0; i < N; ++i) {
    if (down[i] != -1 && (to_left[i] == i || to_left[down[i]] == down[i])) {
      ver[to_left[down[i]]].push_back(to_left[i]);
      ver[to_left[i]].push_back(to_left[down[i]]);
    }
  }
  function<void(int, int)> dfs_ver = [&](int x, int pr) {
    for (auto y : ver[x]) {
      if (y != pr) {
        dfs_ver(y, x);
        ans += (long long) sz_left[y] * (N - sz_left[y]);
        sz_left[x] += sz_left[y];
      }
    }
  };
  for (int i = 0; i < N; ++i) {
    if (sz_left[i]) {
      dfs_ver(i, -1);
      break;
    }
  }
  return (int) (ans % (int) 1e9);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 356 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 3 ms 640 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2304 KB Output is correct
2 Correct 13 ms 2432 KB Output is correct
3 Correct 32 ms 5388 KB Output is correct
4 Correct 26 ms 5372 KB Output is correct
5 Correct 53 ms 10360 KB Output is correct
6 Correct 58 ms 10204 KB Output is correct
7 Correct 57 ms 10492 KB Output is correct
8 Correct 52 ms 10104 KB Output is correct
9 Correct 53 ms 10616 KB Output is correct
10 Correct 64 ms 13692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 2816 KB Output is correct
2 Correct 16 ms 2816 KB Output is correct
3 Correct 44 ms 6648 KB Output is correct
4 Correct 33 ms 6136 KB Output is correct
5 Correct 85 ms 12916 KB Output is correct
6 Correct 62 ms 11332 KB Output is correct
7 Correct 112 ms 13036 KB Output is correct
8 Correct 80 ms 11356 KB Output is correct
9 Correct 107 ms 10984 KB Output is correct
10 Correct 77 ms 10956 KB Output is correct