Skip to content

Instantly share code, notes, and snippets.

@godmar
Created October 17, 2017 19:43
Show Gist options
  • Save godmar/1ddef54330647bb901c1861d410b0cc6 to your computer and use it in GitHub Desktop.
Save godmar/1ddef54330647bb901c1861d410b0cc6 to your computer and use it in GitHub Desktop.
/*
* Can we really implement anything, no matter how inefficient, and have it pass
* just because it's frigging C++???
*/
#include <bits/stdc++.h>
using namespace std;
int
main()
{
int n;
cin >> n;
vector<int> nums;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
nums.push_back(x);
}
int q;
cin >> q;
for (int k = 0; k < q; k++) {
int i, m;
cin >> i >> m;
set<int> numzz;
for (int j = 0; j < m; j++) {
int x;
cin >> x;
numzz.insert(x);
}
auto isok = [i, nums, numzz](int mid) -> bool {
for (auto it = (nums.begin() + i - 1); it < nums.begin() + mid; it++)
if (numzz.find(*it) == numzz.end())
return false;
return true;
};
int lo = i - 1;
int hi = n;
while (lo + 1 < hi) {
int mid = (lo + hi) / 2;
if (isok(mid)) {
lo = mid;
} else
hi = mid;
}
if (lo == i - 1 && !isok(lo))
cout << 0 << endl;
else {
if (isok(hi))
cout << (2 + lo - i) << endl;
else
cout << (1 + lo - i) << endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment