Submission #753040

#TimeUsernameProblemLanguageResultExecution timeMemory
753040marvinthangIdeal city (IOI12_city)C++17
100 / 100
62 ms8520 KiB
/*************************************
*    author: marvinthang             *
*    created: 04.06.2023 20:21:54    *
*************************************/

#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// end of template

const int MOD = 1e9;

int DistanceSum(int N, int *X, int *Y) {
    auto add = [] (int &a, int b) {
        a += b;
        if (a >= MOD) a -= MOD;
    };
    auto solve = [&] (void) {
        vector <pair <int, int>> points;
        REP(i, N) points.emplace_back(X[i], Y[i]);
        sort(ALL(points));
        vector <vector <int>> ver;
        REP(i, N) {
            int j = i;
            while (j + 1 < N && points[j].fi == points[j + 1].fi && points[j].se + 1 == points[j + 1].se) ++j;
            ver.push_back(vector<int>({points[i].fi, points[i].se, points[j].se, j - i + 1}));
            i = j;
        }
        int n = ver.size();
        vector <vector <int>> adj(n);
        int j = 0;
        REP(i, n) {
            while (ver[j][0] + 1 < ver[i][0] || (ver[j][0] + 1 == ver[i][0] && ver[j][2] < ver[i][1])) ++j;
            while (ver[j][0] + 1 == ver[i][0] && ver[j][1] <= ver[i][2]) {
                adj[i].push_back(j);
                adj[j].push_back(i);
                ++j;
            }
            j = max(0, j - 1);
        }
        int res = 0;
        function <int(int, int)> dfs = [&] (int u, int par) {
            int sz_u = ver[u][3];
            for (int v: adj[u]) if (v != par) {
                int sz_v = dfs(v, u);
                sz_u += sz_v;
                add(res, 1LL * (N - sz_v) * sz_v % MOD);
            }
            return sz_u;
        };
        dfs(0, 0);
        return res;
    };
    int res = solve();
    REP(i, N) swap(X[i], Y[i]);
    add(res, solve());
    return res;
}

#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;
  file("city");
  /* Set input and output buffering */
  char *inbuf, *outbuf;
  inbuf = (char*) malloc(inbuf_len * sizeof(char));
  outbuf = (char*) malloc(outbuf_len * sizeof(char));
  tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
  assert(tmp == 0);
  tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
  assert(tmp == 0);

  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
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...