Skip to content

Instantly share code, notes, and snippets.

@godmar
Created October 18, 2017 17:05
Show Gist options
  • Save godmar/4eda6cc099a450e8f5bbd397f9a4626b to your computer and use it in GitHub Desktop.
Save godmar/4eda6cc099a450e8f5bbd397f9a4626b to your computer and use it in GitHub Desktop.
# @author godmar
from math import sqrt, ceil
n = int(raw_input())
a = map(int, raw_input().split())
sn = int(ceil(sqrt(n)))
buckets = [set(a[sn*i:sn*(i+1)]) for i in range(n/sn)]
q = int(raw_input())
for _ in range(q):
l = map(int, raw_input().split())
j = idx = l[0] - 1
query = set(l[2:])
# count up to sqrt(n) before reaching first bucket
while j < n and j % sn != 0 and a[j] in query:
j += 1
count = j - idx
# count full buckets
while j % sn == 0 and j + sn < n and buckets[j/sn].issubset(query):
count += sn
j += sn
# count remainder
while j < n and a[j] in query:
j += 1
count += 1
print count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment