답안 #516148

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
516148 2022-01-20T12:57:06 Z 600Mihnea 이상적인 도시 (IOI12_city) C++17
0 / 100
9 ms 6012 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = (int) 1e5 + 7;
const int M = (int) 1e9;

int add(int a, int b) {
  a += b;
  return a - M * (a >= M) + M * (a < 0);
}

int mul(int a, int b) {
  return a * (ll) b % M;
}

void addup(int &a, int b) {
  a += b;
  a - M * (a >= M) + M * (a < 0);
}

void mulup(int a, int b) {
  a = a * (ll) b % M;
}

class Segment {
private:
public:
  int l, r, id;
  Segment(int l, int r, int id) :
    l(l),
    r(r),
    id(id) {
  }
};

vector<int> g[N];
vector<Segment> segs[N];

void addEdge(int a, int b) {
  g[a].push_back(b);
  g[b].push_back(a);
}

int sum[N], T;

void dfs1(int a, int p = -1) {
  for (auto &b : g[a]) {
    if (b == p) continue;
    dfs1(b, a);
    sum[a] += sum[b];
  }
}

void dfs2(int &sol, int a, int p = -1) {
  if (a) addup(sol, mul(sum[a], T - sum[a]));
  for (auto &b : g[a]) {
    if (b == p) continue;
    dfs2(sol, b, a);
  }
}

int compute(int n, int *x, int *y) {
  T = n;
  int sol = 0;
  for (int i = 0; i < n; i++) g[i].clear(), segs[i].clear();
  for (int i = 0; i < n; i++) g[x[i]].push_back(y[i]);
  int id = 0;
  for (int i = 0; i < n; i++) {
    sort(g[i].begin(), g[i].end());
    int l = 0, r;
    while (l < (int) g[i].size()) {
      r = l;
      while (r + 1 < (int) g[i].size() && g[i][r + 1] == g[i][r] + 1) r++;
      sum[id] = r - l + 1;
      segs[i].push_back({l, r, id++});

      l = r + 1;
    }
  }
  for (int i = 0; i < n; i++) g[i].clear();
  for (int i = 1; i < n; i++) {
    /// add the edges!
    int ant_p = 0;
    for (auto &seg : segs[i]) {
      while (true) {
        if (ant_p == (int) segs[i - 1].size()) break;
        if (segs[i - 1][ant_p].r < seg.l) ant_p++;
        else break;
      }
      while (true) {
        if (ant_p == (int) segs[i - 1].size()) break;
        if (segs[i - 1][ant_p].l > seg.r) break;
        addEdge(segs[i - 1][ant_p].id, seg.id);
        ant_p++;
      }
    }
  }
  dfs1(0);
  dfs2(sol, 0);
  return sol;
}

int DistanceSum(int n, int *x, int *y) {
  {
    int __x = x[0], __y = y[0];
    for (int i = 1; i < n; i++) {
      __x = min(__x, x[i]);
      __y = min(__y, y[i]);
    }
    for (int i = 0; i < n; i++) {
      x[i] -= __x;
      y[i] -= __y;
    }
    for (int i = 0; i < n; i++) {
      assert(0 <= x[i] && x[i] < n);
      assert(0 <= y[i] && y[i] < n);
    }
  }

  return add(compute(n, x, y), compute(n, y, x));
}

Compilation message

city.cpp: In function 'void addup(int&, int)':
city.cpp:21:20: warning: statement has no effect [-Wunused-value]
   21 |   a - M * (a >= M) + M * (a < 0);
      |   ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Incorrect 3 ms 4944 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 5456 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 6012 KB Output isn't correct
2 Halted 0 ms 0 KB -