답안 #283080

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283080 2020-08-25T09:40:26 Z kdh9949 Iqea (innopolis2018_final_C) C++17
36 / 100
2000 ms 49576 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define x first
#define y second
#define all(v) v.begin(),v.end()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  
  vpii v;
  map<pii, int> ord;
  vector<vint> e(n + 1);
  const static int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  for(int i = 1; i <= n; i++) {
    pii p;
    cin >> p.x >> p.y;
    int cur = int(ord.size()) + 1;
    ord[p] = cur;
    for(int j = 0; j < 4; j++) {
      pii t = {p.x + dx[j], p.y + dy[j]};
      if(ord.count(t)) {
        e[i].push_back(ord[t]);
        e[ord[t]].push_back(i);
      }
    }
    v.push_back(p);
  }

  vint gn(n + 1), sn(n + 1), id(n);
  iota(all(id), 0);
  sort(all(id), [&](int x, int y){ return v[x] < v[y]; });
  for(int i = 0, j, k = 1; i < n; i = j, k++) {
    for(j = i; j < n; j++) {
      if(v[id[i]].x != v[id[j]].x || v[id[j]].y - v[id[i]].y != j - i) break;
      sn[id[j] + 1] = k;
    } 
  }
  sort(all(id), [&](int x, int y){
    return pii(v[x].y, v[x].x) < pii(v[y].y, v[y].x);
  });
  for(int i = 0, j, k = 1; i < n; i = j, k++) {
    for(j = i; j < n; j++) {
      if(v[id[i]].y != v[id[j]].y || v[id[j]].x - v[id[i]].x != j - i) break;
      gn[id[j] + 1] = k;
    }
  }

  const static int L = 17;
  vector<vint> ge(n + 1), se(n + 1), gsp(L, vint(n + 1)), ssp(L, vint(n + 1));
  vint gdep(n + 1), sdep(n + 1);

  auto build_edge = [&](const vint &tn, vector<vint> &te) {
    for(int i = 1; i <= n; i++) for(int j : e[i]) {
      if(tn[i] != tn[j]) te[tn[i]].push_back(tn[j]);
    }
    for(int i = 1; i <= n; i++) {
      sort(all(te[i]));
      te[i].erase(unique(all(te[i])), te[i].end());
    }
  };
  build_edge(gn, ge);
  build_edge(sn, se);

  function<void(const vector<vint>&, vint&, vector<vint>&, int, int, int)> init_spt = [&](
    const vector<vint> &te, vint &dep, vector<vint> &sp,
    int x, int y, int z) {
    dep[x] = z;
    sp[0][x] = y;
    for(int i = 1; i < L; i++) sp[i][x] = sp[i - 1][sp[i - 1][x]];
    for(int i : te[x]) if(i != y) init_spt(te, dep, sp, i, x, z + 1);
  };
  init_spt(ge, gdep, gsp, 1, 0, 1);
  init_spt(se, sdep, ssp, 1, 0, 1);

  auto dist = [&](const vector<vint> &sp, const vint &dep, int x, int y) {
    int r = dep[x] + dep[y];
    if(dep[x] > dep[y]) swap(x, y);
    for(int i = L - 1; i >= 0; i--) {
      if(((dep[y] - dep[x]) >> i) & 1) y = sp[i][y];
    }
    if(x == y) return r - 2 * dep[x];
    for(int i = L - 1; i >= 0; i--) {
      if(sp[i][y] != sp[i][x]) {
        y = sp[i][y];
        x = sp[i][x];
      }
    }
    return r - 2 * (dep[x] - 1);
  };

  vint d(n + 1, n);
  auto upd = [&](vint &v) {
    queue<int> q;
    for(int x : v) { d[x] = 0; q.push(x); }
    while(!q.empty()) {
      int x = q.front();
      q.pop();
      for(int i : e[x]) {
        if(d[i] > d[x] + 1) {
          d[i] = d[x] + 1;
          q.push(i);
        }
      }
    }
    v.clear();
  };

  int q;
  cin >> q;
  vint qv;
  const static int thr = 50;
  for(int i = 0; i < q; i++) {
    int t;
    pii p;
    cin >> t >> p.x >> p.y;
    if(t == 1) qv.push_back(ord[p]);
    else {
      if(qv.size() > thr) upd(qv);
      int x = ord[p];
      int ans = d[x];
      for(int y : qv) {
        ans = min(
          ans,
          dist(gsp, gdep, gn[x], gn[y]) + dist(ssp, sdep, sn[x], sn[y])
        );
      }
      cout << (ans == n ? -1 : ans) << '\n';
    }
  }

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 32 ms 1920 KB Output is correct
5 Correct 29 ms 2048 KB Output is correct
6 Correct 32 ms 2304 KB Output is correct
7 Correct 37 ms 2176 KB Output is correct
8 Correct 37 ms 2304 KB Output is correct
9 Correct 37 ms 2424 KB Output is correct
10 Correct 45 ms 2208 KB Output is correct
11 Correct 33 ms 2300 KB Output is correct
12 Correct 44 ms 2304 KB Output is correct
13 Correct 36 ms 2308 KB Output is correct
14 Correct 40 ms 2304 KB Output is correct
15 Correct 39 ms 2304 KB Output is correct
16 Correct 37 ms 2304 KB Output is correct
17 Correct 40 ms 2336 KB Output is correct
18 Correct 36 ms 2176 KB Output is correct
19 Correct 35 ms 2176 KB Output is correct
20 Correct 40 ms 2344 KB Output is correct
21 Correct 36 ms 2304 KB Output is correct
22 Correct 40 ms 2304 KB Output is correct
23 Correct 31 ms 2432 KB Output is correct
24 Correct 32 ms 2688 KB Output is correct
25 Correct 28 ms 2816 KB Output is correct
26 Correct 28 ms 2432 KB Output is correct
27 Correct 26 ms 2816 KB Output is correct
28 Correct 30 ms 2584 KB Output is correct
29 Correct 30 ms 2560 KB Output is correct
30 Correct 30 ms 2688 KB Output is correct
31 Correct 28 ms 2560 KB Output is correct
32 Correct 30 ms 2560 KB Output is correct
33 Correct 39 ms 2688 KB Output is correct
34 Correct 31 ms 2560 KB Output is correct
35 Correct 35 ms 2688 KB Output is correct
36 Correct 25 ms 2816 KB Output is correct
37 Correct 30 ms 2560 KB Output is correct
38 Correct 29 ms 2560 KB Output is correct
39 Correct 32 ms 2568 KB Output is correct
40 Correct 30 ms 2808 KB Output is correct
41 Correct 29 ms 2560 KB Output is correct
42 Correct 29 ms 2520 KB Output is correct
43 Correct 30 ms 2688 KB Output is correct
44 Correct 32 ms 2560 KB Output is correct
45 Correct 30 ms 2560 KB Output is correct
46 Correct 25 ms 2688 KB Output is correct
47 Correct 36 ms 2304 KB Output is correct
48 Correct 43 ms 2176 KB Output is correct
49 Correct 42 ms 2176 KB Output is correct
50 Correct 37 ms 2276 KB Output is correct
51 Correct 41 ms 2188 KB Output is correct
52 Correct 31 ms 2424 KB Output is correct
53 Correct 30 ms 2432 KB Output is correct
54 Correct 33 ms 2304 KB Output is correct
55 Correct 31 ms 2304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 32 ms 1920 KB Output is correct
5 Correct 29 ms 2048 KB Output is correct
6 Correct 32 ms 2304 KB Output is correct
7 Correct 37 ms 2176 KB Output is correct
8 Correct 37 ms 2304 KB Output is correct
9 Correct 37 ms 2424 KB Output is correct
10 Correct 45 ms 2208 KB Output is correct
11 Correct 33 ms 2300 KB Output is correct
12 Correct 44 ms 2304 KB Output is correct
13 Correct 36 ms 2308 KB Output is correct
14 Correct 40 ms 2304 KB Output is correct
15 Correct 39 ms 2304 KB Output is correct
16 Correct 37 ms 2304 KB Output is correct
17 Correct 40 ms 2336 KB Output is correct
18 Correct 36 ms 2176 KB Output is correct
19 Correct 35 ms 2176 KB Output is correct
20 Correct 40 ms 2344 KB Output is correct
21 Correct 36 ms 2304 KB Output is correct
22 Correct 40 ms 2304 KB Output is correct
23 Correct 31 ms 2432 KB Output is correct
24 Correct 32 ms 2688 KB Output is correct
25 Correct 28 ms 2816 KB Output is correct
26 Correct 28 ms 2432 KB Output is correct
27 Correct 26 ms 2816 KB Output is correct
28 Correct 30 ms 2584 KB Output is correct
29 Correct 30 ms 2560 KB Output is correct
30 Correct 30 ms 2688 KB Output is correct
31 Correct 28 ms 2560 KB Output is correct
32 Correct 30 ms 2560 KB Output is correct
33 Correct 39 ms 2688 KB Output is correct
34 Correct 31 ms 2560 KB Output is correct
35 Correct 35 ms 2688 KB Output is correct
36 Correct 25 ms 2816 KB Output is correct
37 Correct 30 ms 2560 KB Output is correct
38 Correct 29 ms 2560 KB Output is correct
39 Correct 32 ms 2568 KB Output is correct
40 Correct 30 ms 2808 KB Output is correct
41 Correct 29 ms 2560 KB Output is correct
42 Correct 29 ms 2520 KB Output is correct
43 Correct 30 ms 2688 KB Output is correct
44 Correct 32 ms 2560 KB Output is correct
45 Correct 30 ms 2560 KB Output is correct
46 Correct 25 ms 2688 KB Output is correct
47 Correct 36 ms 2304 KB Output is correct
48 Correct 43 ms 2176 KB Output is correct
49 Correct 42 ms 2176 KB Output is correct
50 Correct 37 ms 2276 KB Output is correct
51 Correct 41 ms 2188 KB Output is correct
52 Correct 31 ms 2424 KB Output is correct
53 Correct 30 ms 2432 KB Output is correct
54 Correct 33 ms 2304 KB Output is correct
55 Correct 31 ms 2304 KB Output is correct
56 Correct 426 ms 2952 KB Output is correct
57 Correct 428 ms 2808 KB Output is correct
58 Correct 414 ms 2808 KB Output is correct
59 Correct 615 ms 2936 KB Output is correct
60 Correct 667 ms 2808 KB Output is correct
61 Correct 678 ms 2896 KB Output is correct
62 Correct 654 ms 2808 KB Output is correct
63 Correct 705 ms 2860 KB Output is correct
64 Correct 710 ms 3064 KB Output is correct
65 Correct 659 ms 3016 KB Output is correct
66 Correct 668 ms 3064 KB Output is correct
67 Correct 673 ms 3064 KB Output is correct
68 Correct 703 ms 2936 KB Output is correct
69 Correct 671 ms 2936 KB Output is correct
70 Correct 607 ms 2936 KB Output is correct
71 Correct 619 ms 2552 KB Output is correct
72 Correct 669 ms 2988 KB Output is correct
73 Correct 628 ms 3020 KB Output is correct
74 Correct 335 ms 3320 KB Output is correct
75 Correct 424 ms 3192 KB Output is correct
76 Correct 338 ms 3576 KB Output is correct
77 Correct 436 ms 3448 KB Output is correct
78 Correct 428 ms 3324 KB Output is correct
79 Correct 317 ms 3540 KB Output is correct
80 Correct 439 ms 3320 KB Output is correct
81 Correct 515 ms 3288 KB Output is correct
82 Correct 495 ms 3324 KB Output is correct
83 Correct 520 ms 3320 KB Output is correct
84 Correct 336 ms 3328 KB Output is correct
85 Correct 471 ms 3192 KB Output is correct
86 Correct 462 ms 3192 KB Output is correct
87 Correct 336 ms 3588 KB Output is correct
88 Correct 635 ms 2808 KB Output is correct
89 Correct 623 ms 2840 KB Output is correct
90 Correct 559 ms 2948 KB Output is correct
91 Correct 534 ms 2884 KB Output is correct
92 Correct 519 ms 2936 KB Output is correct
93 Correct 492 ms 3128 KB Output is correct
94 Correct 458 ms 3068 KB Output is correct
95 Correct 526 ms 3064 KB Output is correct
96 Correct 475 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2087 ms 49576 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2085 ms 37228 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 32 ms 1920 KB Output is correct
5 Correct 29 ms 2048 KB Output is correct
6 Correct 32 ms 2304 KB Output is correct
7 Correct 37 ms 2176 KB Output is correct
8 Correct 37 ms 2304 KB Output is correct
9 Correct 37 ms 2424 KB Output is correct
10 Correct 45 ms 2208 KB Output is correct
11 Correct 33 ms 2300 KB Output is correct
12 Correct 44 ms 2304 KB Output is correct
13 Correct 36 ms 2308 KB Output is correct
14 Correct 40 ms 2304 KB Output is correct
15 Correct 39 ms 2304 KB Output is correct
16 Correct 37 ms 2304 KB Output is correct
17 Correct 40 ms 2336 KB Output is correct
18 Correct 36 ms 2176 KB Output is correct
19 Correct 35 ms 2176 KB Output is correct
20 Correct 40 ms 2344 KB Output is correct
21 Correct 36 ms 2304 KB Output is correct
22 Correct 40 ms 2304 KB Output is correct
23 Correct 31 ms 2432 KB Output is correct
24 Correct 32 ms 2688 KB Output is correct
25 Correct 28 ms 2816 KB Output is correct
26 Correct 28 ms 2432 KB Output is correct
27 Correct 26 ms 2816 KB Output is correct
28 Correct 30 ms 2584 KB Output is correct
29 Correct 30 ms 2560 KB Output is correct
30 Correct 30 ms 2688 KB Output is correct
31 Correct 28 ms 2560 KB Output is correct
32 Correct 30 ms 2560 KB Output is correct
33 Correct 39 ms 2688 KB Output is correct
34 Correct 31 ms 2560 KB Output is correct
35 Correct 35 ms 2688 KB Output is correct
36 Correct 25 ms 2816 KB Output is correct
37 Correct 30 ms 2560 KB Output is correct
38 Correct 29 ms 2560 KB Output is correct
39 Correct 32 ms 2568 KB Output is correct
40 Correct 30 ms 2808 KB Output is correct
41 Correct 29 ms 2560 KB Output is correct
42 Correct 29 ms 2520 KB Output is correct
43 Correct 30 ms 2688 KB Output is correct
44 Correct 32 ms 2560 KB Output is correct
45 Correct 30 ms 2560 KB Output is correct
46 Correct 25 ms 2688 KB Output is correct
47 Correct 36 ms 2304 KB Output is correct
48 Correct 43 ms 2176 KB Output is correct
49 Correct 42 ms 2176 KB Output is correct
50 Correct 37 ms 2276 KB Output is correct
51 Correct 41 ms 2188 KB Output is correct
52 Correct 31 ms 2424 KB Output is correct
53 Correct 30 ms 2432 KB Output is correct
54 Correct 33 ms 2304 KB Output is correct
55 Correct 31 ms 2304 KB Output is correct
56 Correct 426 ms 2952 KB Output is correct
57 Correct 428 ms 2808 KB Output is correct
58 Correct 414 ms 2808 KB Output is correct
59 Correct 615 ms 2936 KB Output is correct
60 Correct 667 ms 2808 KB Output is correct
61 Correct 678 ms 2896 KB Output is correct
62 Correct 654 ms 2808 KB Output is correct
63 Correct 705 ms 2860 KB Output is correct
64 Correct 710 ms 3064 KB Output is correct
65 Correct 659 ms 3016 KB Output is correct
66 Correct 668 ms 3064 KB Output is correct
67 Correct 673 ms 3064 KB Output is correct
68 Correct 703 ms 2936 KB Output is correct
69 Correct 671 ms 2936 KB Output is correct
70 Correct 607 ms 2936 KB Output is correct
71 Correct 619 ms 2552 KB Output is correct
72 Correct 669 ms 2988 KB Output is correct
73 Correct 628 ms 3020 KB Output is correct
74 Correct 335 ms 3320 KB Output is correct
75 Correct 424 ms 3192 KB Output is correct
76 Correct 338 ms 3576 KB Output is correct
77 Correct 436 ms 3448 KB Output is correct
78 Correct 428 ms 3324 KB Output is correct
79 Correct 317 ms 3540 KB Output is correct
80 Correct 439 ms 3320 KB Output is correct
81 Correct 515 ms 3288 KB Output is correct
82 Correct 495 ms 3324 KB Output is correct
83 Correct 520 ms 3320 KB Output is correct
84 Correct 336 ms 3328 KB Output is correct
85 Correct 471 ms 3192 KB Output is correct
86 Correct 462 ms 3192 KB Output is correct
87 Correct 336 ms 3588 KB Output is correct
88 Correct 635 ms 2808 KB Output is correct
89 Correct 623 ms 2840 KB Output is correct
90 Correct 559 ms 2948 KB Output is correct
91 Correct 534 ms 2884 KB Output is correct
92 Correct 519 ms 2936 KB Output is correct
93 Correct 492 ms 3128 KB Output is correct
94 Correct 458 ms 3068 KB Output is correct
95 Correct 526 ms 3064 KB Output is correct
96 Correct 475 ms 3064 KB Output is correct
97 Execution timed out 2087 ms 49576 KB Time limit exceeded
98 Halted 0 ms 0 KB -