King Triton really likes watching sport competitions on TV. But much more Triton likes watching live competitions. So Triton decides to set up a swimming competition in the kingdom Merfolk. Thousands of creatures come to take part in competition, that's why it is too difficult to take the first place.
For the King's beloved daughter Ariel this competition is the first in her life. Ariel is very kind, so she wants to give a lot of gold medals. Ariel says, that it is unfair to make a single ranking list for creatures that are so different. It is really a good result to be the fastest small fish without tail in Merfolk!
Ariel chooses k important traits (such as size, tailness, rapacity and so on). A creature can either possess a trait or not (there are no intermediate options).
A score is given for each creature (it doesn't matter how it was calculated) and the list of possessed traits f1, ..., fy is also given.
Ariel wants to know the place occupied by creature a in a competition among creatures, who have the same traits h1, ..., ht. So if creature a doesn't have a trait hi, then all creatures in the competition are without this trait. If creature a has a trait hi, then all creatures in the competition have this trait. Other traits doesn't matter. The winner of the competition is a creature with the maximum score.
The first line contains n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 10). The next n lines contain information about creatures: score(1 ≤ score ≤ 109), y (0 ≤ y ≤ k) — the number of possessed traits, and y numbers fi (1 ≤ fi ≤ k) — ids of possessed traits. All fi in one line are different.
The next line contains m (1 ≤ m ≤ 105) — the number of queries from Ariel. The next m lines describe queries: a (1 ≤ a ≤ n) — the id of a creature, then t — the number of traits, then t numbers hi. All hi in one line are different.
For each query output the place of a creature a in ranking list amount the corresponded creatures. If several creatures have the same score all of them take the same place.
3 2 100 1 1 50 1 2 30 2 1 2 12 1 2 1 2 1 1 1 1 1 2 1 0 2 0 2 1 1 2 1 2 2 2 2 1 3 0 3 2 1 2 3 1 2 3 1 1
1 1 1 1 2 1 1 1 3 1 2 2
3 2 100 0 10 0 100 0 3 1 0 2 0 3 0
1 3 1
题目链接:http://codeforces.com/gym/100685/problem/A
看似1e9,实际约1e8左右的复杂度,然而服务器快,AC= =,没什么好说的。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
#define pll pair<LL, LL>
#define pii pair<int, int>
#define X first
#define Y second
#define MAXN 1050//1e3
#define lson l, mid, (rt << 1)
#define rson mid + 1, r, (rt << 1 | 1)
const double eps = 1e-10;
int n, k, Q;
struct node {
int score, flag;
node(int score, int flag) : score(score), flag(flag) {}
node() {}
} input[10010];
vector<int> vec[MAXN];
void init() {
for(int i = 0; i < MAXN; i++) {
vec[i].clear();
}
}
int get_index(int x, int id) {
return upper_bound(vec[id].begin(), vec[id].end(), x) - vec[id].begin();
}
int main() {
while(~scanf("%d%d", &n, &k)) {
init();
int cnt, num;
for(int i = 1; i <= n; i++) {
scanf("%d%d", &input[i].score, &cnt);
for(int j = 0; j < cnt; j++) {
scanf("%d", &num);
input[i].flag = input[i].flag | (1 << (num - 1));
}
vec[input[i].flag].push_back(input[i].score);
}
for(int i = 0; i < MAXN; i++) {
sort(vec[i].begin(), vec[i].end());
}
scanf("%d", &Q);
int id;
while(Q--) {
scanf("%d%d", &id, &cnt);
int flag_sum = 0;
for(int i = 0; i < cnt; i++) {
scanf("%d", &num);
flag_sum = flag_sum | (1 << (num - 1));
}
int ans = 0;
int res = (flag_sum & input[id].flag);
for(int i = 0; i < MAXN; i++) {
if((i & flag_sum) == res) {
int len = vec[i].size();
ans += len - get_index(input[id].score, i);
}
}
printf("%d\n", ans + 1);
}
}
return 0;
}