CF Gym 100685A Ariel

时间:2022-12-15 19:45:49

传送门

A. Ariel
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

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.

Examples
Input
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
Output
1
1
1
1
2
1
1
1
3
1
2
2
Input
3 2
100 0
10 0
100 0
3
1 0
2 0
3 0
Output
1
3
1

Solution:
(1)(看起来)比较暴力的做法:

预处理:将属性相同的选手的score放在一个vector里,排序.这样共得到$2^{k}$个vector.

查询:在每个合法属性对应的vector里二分查找比该score大的score的数目.

Implememtation:

 #include <bits/stdc++.h>
using namespace std; const int N=<<, M=1e4+; vector<int> a[N];
int s[M], st[M]; int main(){
int n, k, m;
cin>>n>>k;
for(int i=, c; i<=n; i++){
cin>>s[i]>>c;
for(int t; c--; cin>>t, st[i]|=<<t-);
a[st[i]].push_back(s[i]);
}
for(int i=; i<<<k; i++)
sort(a[i].begin(), a[i].end());
cin>>m;
for(int id, c, t, mask; m--; ){
cin>>id>>c;
mask=;
for(int i=, x; i<c; i++)
cin>>x, mask|=<<x-;
int ans=;
for(int i=; i<<<k; i++)
if((st[id]&mask)==(i&mask))
ans+=a[i].end()-upper_bound(a[i].begin(), a[i].end(), s[id]);
cout<<ans+<<endl;
}
return ;
}

事实证明,这个做法真的没那么暴力.

(2)将所有可能的查询的结果算出来(打表),这样查询的复杂度是$O(1)$的.

把查询查询中要求和id相同的那些属性压缩到一个$k$位整数$s$中(将$s$的对应位置1),存在$res[id][s]$中.

用$st[i]$表示第i个选手的属性,不难看出$res[id][s]$对应的比赛的选手集合为:

\[C_{id, s} =\{i: st[i] \& s = st[id]\& s\}\]

由此,我们将$C_{id, s}$,写成$C_{st[id]\&s}$

考虑在s固定的情况下,如何计算res[1..n][s].

我们用(id, score)表示一个选手, 先将各选手按score从大到小排序。然后逐个放到$C_{st[id]\&s}$中,其实这样是将选手照其$st[id]\&s$分成了若干等价类.这样便可在某个等价类内O(1)地计算res[i][s]。总复杂度是$O(N\log(N)+2^{k}N)$。

Implementation:

#include <bits/stdc++.h>
using namespace std; const int N(<<), M(1e4+);
vector<pair<int,int>> a, b[N];
int res[M][N], st[M]; int main(){
int n, k;
cin>>n>>k;
for(int i=, c, t, sc; i<=n; i++){
cin>>sc>>c;
for(; c--; cin>>t, st[i]|=<<t-); //error-prone
a.push_back({sc, i});
} sort(a.begin(), a.end(), greater<pair<int,int>>()); for(int i=; i<<<k; i++){
for(int i=; i<<<k; i++)
b[i].clear();
for(auto x: a)
b[st[x.second]&i].push_back(x);
for(int j=; j<<<k; j++)
for(int k=; k<b[j].size(); k++)
if(b[j][k].first==b[j][k-].first)
res[b[j][k].second][i]=res[b[j][k-].second][i];
else
res[b[j][k].second][i]=k;
}
int m;
cin>>m;
for(; m--; ){
int mask=, c, id, t;
cin>>id>>c;
for(; c--; cin>>t, mask|=<<t-);
cout<<res[id][mask]+<<endl;
}
return ;
}