Problem p02343 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

struct Item {
  int value;
  Item *prev;
  Item *next;
  Item() : value(-1), prev(nullptr), next(nullptr) {}

  Item *first() {
    Item *ret = this;
    while (ret->prev) {
      ret = ret->prev;
    }
    return ret;
  }
  Item *last() {
    Item *ret = this;
    while (ret->next) {
      ret = ret->next;
    }
    return ret;
  }

  bool isSame(int v) const {
    const Item *next_ = this;
    while (next_) {
      if (next_->value == v) {
        return true;
      }
      next_ = next_->next;
    }
    const Item *prev_ = this->prev;
    while (prev_) {
      if (prev_->value == v) {
        return true;
      }
      prev_ = prev_->prev;
    }
    return false;
  }
};

std::vector<int> calc(int n, const std::vector<bool> &qs,
                      const std::vector<int> &xs, const std::vector<int> &ys) {
  std::vector<Item> items(n, Item());
  for (int i = 0; i < n; ++i) {
    items[i].value = i;
  }

  std::vector<int> ret;
  for (int i = 0; i < qs.size(); ++i) {
    bool const query = qs[i];
    int const x = xs[i];
    int const y = ys[i];
    bool const isSame = items[x].isSame(y);
    if (query) {
      ret.push_back(isSame ? 1 : 0);
    } else {
      // merge
      if (isSame) {
        continue;
      }
      Item *const lastItem = items[x].last();
      Item *const firstItem = items[y].first();
      lastItem->next = firstItem;
      firstItem->prev = lastItem;
    }
  }
  return ret;
}

int main() {
  int n, q;
  std::cin >> n >> q;
  std::vector<bool> qs(q, false);
  std::vector<int> xs(q, 0);
  std::vector<int> ys(q, 0);
  for (int i = 0; i < q; ++i) {
    int query;
    std::cin >> query;
    qs[i] = query == 1;
    std::cin >> xs[i];
    std::cin >> ys[i];
  }
  auto ret = calc(n, qs, xs, ys);
  for (int v : ret) {
    std::cout << v << std::endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Item {
  int value;
  Item *prev;
  Item *next;
  Item() : value(-1), prev(nullptr), next(nullptr) {}

  Item *first() {
    Item *ret = this;
    while (ret->prev) {
      ret = ret->prev;
    }
    return ret;
  }
  Item *last() {
    Item *ret = this;
    while (ret->next) {
      ret = ret->next;
    }
    return ret;
  }

  bool isSame(int v) const {
    const Item *next_ = this;
    while (next_) {
      if (next_->value == v) {
        return true;
      }
      next_ = next_->next;
    }
    const Item *prev_ = this->prev;
    while (prev_) {
      if (prev_->value == v) {
        return true;
      }
      prev_ = prev_->prev;
    }
    return false;
  }
};

std::vector<int> calc(int n, const std::vector<bool> &qs,
                      const std::vector<int> &xs, const std::vector<int> &ys) {
  std::vector<Item> items(n, Item());
  for (int i = 0; i < n; ++i) {
    items[i].value = i;
  }

  std::vector<int> ret;
  for (int i = 0; i < qs.size(); ++i) {
    bool const query = qs[i];
    int const x = xs[i];
    int const y = ys[i];
    if (query) {
      ret.push_back(items[x].isSame(y) ? 1 : 0);
    } else {
      // merge
      Item *const lastItem = items[x].last();
      Item *const firstItem = items[y].first();
      lastItem->next = firstItem;
      firstItem->prev = lastItem;
    }
  }
  return ret;
}

int main() {
  int n, q;
  std::cin >> n >> q;
  std::vector<bool> qs(q, false);
  std::vector<int> xs(q, 0);
  std::vector<int> ys(q, 0);
  for (int i = 0; i < q; ++i) {
    int query;
    std::cin >> query;
    qs[i] = query == 1;
    std::cin >> xs[i];
    std::cin >> ys[i];
  }
  auto ret = calc(n, qs, xs, ys);
  for (int v : ret) {
    std::cout << v << std::endl;
  }
  return 0;
}