Submission #221560

# Submission time Handle Problem Language Result Execution time Memory
221560 2020-04-10T13:42:13 Z rama_pang ICC (CEOI16_icc) C++14
61 / 100
191 ms 632 KB
#include "icc.h"
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> components;

int Query(vector<int> A, vector<int> B) {
  return query(A.size(), B.size(), A.data(), B.data());
}

int QueryComponent(vector<int> A, vector<int> B) {
  vector<int> P, Q;
  for (auto i : A) {
    for (auto j : components[i]) {
      P.emplace_back(j);
    }
  }
  for (auto i : B) {
    for (auto j : components[i]) {
      Q.emplace_back(j);
    }
  }
  return Query(P, Q);
}

void Answer(int A, int B) {
  return setRoad(A, B);
}

void Split(int L, int R, vector<int> &A, vector<int> &B, int depth) {
  if (depth == 0) {
    int mid = (L + R) / 2;
    for (int i = L; i <= mid; i++) A.emplace_back(i);
    for (int i = mid + 1; i <= R; i++) B.emplace_back(i);
    return;
  }
  int mid = (L + R) / 2;
  Split(L, mid, A, B, depth - 1);
  Split(mid + 1, R, A, B, depth - 1);
}

pair<int, int> BinarySearch(vector<int> A, vector<int> B, bool onComponent) {
  pair<int, int> res = {-1, -1};

  {
    int lo = 0, hi = A.size() - 1;
    while (lo < hi) {
      int mid = (lo + hi) / 2;
      vector<int> halfA;
      for (int i = 0; i <= mid; i++) {
        halfA.emplace_back(A[i]);
      }
      int res = (onComponent ? QueryComponent(halfA, B) : Query(halfA, B));
      if (res) {
        hi = mid;
      } else {
        lo = mid + 1;
      }
    }
    res.first = A[lo];
  }

  {
    int lo = 0, hi = B.size() - 1;
    while (lo < hi) {
      int mid = (lo + hi) / 2;
      vector<int> halfB;
      for (int i = 0; i <= mid; i++) {
        halfB.emplace_back(B[i]);
      }
      int res = (onComponent ? QueryComponent(halfB, A) : Query(halfB, A));
      if (res) {
        hi = mid;
      } else {
        lo = mid + 1;
      }
    }
    res.second = B[lo];
  }

  return res;
}

void MergeComponent(int C1, int C2) {
  vector<int> Merged;
  for (auto i : components[C1]) Merged.emplace_back(i);
  for (auto i : components[C2]) Merged.emplace_back(i);
  components[C1].clear();
  components[C2].clear();
  sort(begin(components), end(components), [&](const vector<int> &a, const vector<int> &b) {
    return a.size() > b.size();
  });
  components.pop_back();
  components.pop_back();
  components.emplace_back(Merged);
}

void run(int N) {
  for (int i = 1; i <= N; i++) {
    components.emplace_back(vector<int>{i});
  }

  for (int i = 1; i < N; i++) {
    vector<int> A, B; // find two components that are connected by new edge
    for (int depth = 0; ; depth++) {
      Split(0, components.size() - 1, A, B, depth);
      if (QueryComponent(A, B)) break;
      A.clear(), B.clear();
    }

    // Binary Search to find edge's endpoint
    auto TwoComp = BinarySearch(A, B, true);
    auto Edge = BinarySearch(components[TwoComp.first], components[TwoComp.second], false);
    Answer(Edge.first, Edge.second);
    MergeComponent(TwoComp.first, TwoComp.second);
  }
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 512 KB Ok! 108 queries used.
2 Correct 11 ms 512 KB Ok! 109 queries used.
# Verdict Execution time Memory Grader output
1 Correct 48 ms 512 KB Ok! 623 queries used.
2 Correct 50 ms 512 KB Ok! 625 queries used.
3 Correct 52 ms 512 KB Ok! 683 queries used.
# Verdict Execution time Memory Grader output
1 Correct 165 ms 632 KB Ok! 1722 queries used.
2 Correct 167 ms 512 KB Ok! 1540 queries used.
3 Correct 174 ms 512 KB Ok! 1839 queries used.
4 Correct 168 ms 512 KB Ok! 1776 queries used.
# Verdict Execution time Memory Grader output
1 Correct 175 ms 632 KB Ok! 1785 queries used.
2 Correct 191 ms 512 KB Ok! 1765 queries used.
3 Correct 173 ms 512 KB Ok! 1819 queries used.
4 Correct 171 ms 512 KB Ok! 1788 queries used.
# Verdict Execution time Memory Grader output
1 Incorrect 190 ms 512 KB Too many queries! 1830 out of 1775
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 175 ms 512 KB Too many queries! 1849 out of 1625
2 Halted 0 ms 0 KB -