Submission #1020816


Source Code Expand

Copy
#include <bits/stdc++.h>

using namespace std;

struct UnionFind
{
  vector< int > data;

  UnionFind(int sz)
  {
    data.assign(sz, -1);
  }

  void unite(int x, int y)
  {
    x = find(x), y = find(y);
    if(x != y) {
      if(data[x] > data[y]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }

  int find(int k)
  {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k)
  {
    return (-data[find(k)]);
  }
};

typedef long long int64;

int64 N, R, s, t;
int64 a[100000], b[100000];
UnionFind tree(100000);
int64 ret[100000];

void ask(int v)
{
  map< int, vector< pair< int64, int > > > backet;
  for(int i = 0; i < N; i++) {
    backet[a[i]].push_back(make_pair(b[i], i));
  }
  for(auto &obj : backet) {
    sort(begin(obj.second), end(obj.second));
  }
  for(int i = 0; i < N; i++) {
    auto &cc = backet[a[i] - R];
    auto p = lower_bound(begin(cc), end(cc), make_pair(b[i] - R - v, +INF));
    auto q = lower_bound(begin(cc), end(cc), make_pair(b[i] + R + v, -INF));
    ret[i] += q - p;
    if(p != q) tree.unite(i, p->second);
  }
  for(int i = 0; i < N; i++) {
    auto &cc = backet[a[i] + R];
    auto p = lower_bound(begin(cc), end(cc), make_pair(b[i] - R - v, +INF));
    auto q = lower_bound(begin(cc), end(cc), make_pair(b[i] + R + v, -INF));
    ret[i] += q - p;
    if(p != q) tree.unite(i, p->second);
  }
}

int main()
{
  cin >> N >> s >> t;
  for(int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    a[i] = x + y;
    b[i] = x - y;
  }
  --s, --t;
  R = max(abs(a[s] - a[t]), abs(b[s] - b[t]));
  ask(false);
  for(int i = 0; i < N; i++) swap(a[i], b[i]);
  ask(true);
  long long res = 0;
  for(int i = 0; i < N; i++) {
    if(tree.find(i) == tree.find(s)) res += ret[i];
  }
  cout << res / 2 << endl;
}

Submission Info

Submission Time
Task E - Manhattan Compass
User ei13333
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1885 Byte
Status

Compile Error

./Main.cpp: In function ‘void ask(int)’:
./Main.cpp:54:71: error: ‘INF’ was not declared in this scope
     auto p = lower_bound(begin(cc), end(cc), make_pair(b[i] - R - v, +INF));
                                                                       ^
./Main.cpp:61:71: error: ‘INF’ was not declared in this scope
     auto p = lower_bound(begin(cc), end(cc), make_pair(b[i] - R - v, +INF));
                                                                       ^