UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#190131#3326. 很多集合(sets)Alan_Zhaoyz1002728ms789364kbC++111.4kb2023-10-05 10:35:122023-10-05 12:44:39

answer

#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
const int N=1e6+5,B=200;
int n,q,id[N],tot,ans[N/B][N/B];
vector<int> vec[N],occ[N];
bitset<N> bs[N/B];
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin>>n>>q;
	For(i,1,n){
		int len;
		cin>>len;
		vec[i].resize(len);
		For(j,0,len-1) cin>>vec[i][j];
		sort(range(vec[i]));
		if(len>=B){
			id[i]=tot++;
			for(int x:vec[i]){
				bs[id[i]].set(x);
				occ[x].push_back(i);
			}
		}
	}
	memset(ans,-1,sizeof(ans));
	For(x,1,int(1e6)){
		for(int i:occ[x]){
			for(int j:occ[x]){
				ans[id[i]][id[j]]=x;
			}
		}
	}
	For(i,1,q){
		int x,y;
		cin>>x>>y;
		if(vec[x].size()<vec[y].size()){
			swap(x,y);
		}
		if(int(vec[y].size())>=B){
			cout<<ans[id[x]][id[y]]<<'\n';
			continue;
		}
		if(int(vec[x].size())<B){
			int ans=-1;
			for(int i=0,j=0;i<int(vec[x].size())&&j<int(vec[y].size());++i){
				while(j<int(vec[y].size())&&vec[y][j]<vec[x][i]){
					++j;
				}
				if(j<int(vec[y].size())&&vec[y][j]==vec[x][i]){
					ans=vec[x][i];
					break;
				}
			}
			cout<<ans<<'\n';
			continue;
		}
		int ans=-1;
		for(int z:vec[y]){
			if(bs[id[x]].test(z)){
				ans=z;
				break;
			}
		}
		cout<<ans<<'\n';
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 58ms
memory: 756296kb

input:

2000 2000
1 37
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 1
1 2
1 2
1 1
1 39
1 31
1 7
1 7
1 2
1 31
1 26
1 8
1 41
...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1...

result:

ok 2000 lines

Test #2:

score: 10
Accepted
time: 82ms
memory: 756292kb

input:

2000 2000
1 34
1 2
1 2
1 1
1 2
1 1
1 1
1 2
1 2
1 2
1 2
1 1
1 12
1 13
1 44
1 27
1 2
1 42
1 31
1 4
1 5...

output:

-1
2
-1
-1
7
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
1
-1
-1
-1
-1
-1
-1
...

result:

ok 2000 lines

Test #3:

score: 10
Accepted
time: 306ms
memory: 756244kb

input:

1000 1000000
1 2
1 35
1 30
1 12
1 6
1 2
1 43
1 38
1 24
1 29
1 1
1 2
1 2
1 1
1 1
1 1
1 26
1 2
1 2
1 1...

output:

-1
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
2
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-...

result:

ok 1000000 lines

Test #4:

score: 10
Accepted
time: 304ms
memory: 756228kb

input:

1000 1000000
1 1
1 4
1 22
1 22
1 37
1 11
1 9
1 9
1 16
1 14
1 2
1 1
1 1
1 2
1 2
1 2
1 19
1 2
1 2
1 1
...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
13
-1
-1
-...

result:

ok 1000000 lines

Test #5:

score: 10
Accepted
time: 330ms
memory: 756228kb

input:

1000 1000000
1 2
1 25
1 16
1 17
1 5
1 2
1 23
1 6
1 12
1 26
1 1
1 2
1 2
1 1
1 2
1 1
1 3
1 1
1 2
1 2
1...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
7
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
43
-1
-1
-1
...

result:

ok 1000000 lines

Test #6:

score: 10
Accepted
time: 199ms
memory: 769340kb

input:

200000 200000
1 2
1 446
1 285
1 229
1 45
1 19
1 222
1 292
1 1
1 1
1 2
1 2
1 2
1 19
1 2
1 1
1 2
1 2
1...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
-1
-1
-1
-1
-1
-1
-1...

result:

ok 200000 lines

Test #7:

score: 10
Accepted
time: 217ms
memory: 769336kb

input:

200000 200000
1 2
1 301
1 312
1 441
1 218
1 138
1 378
1 232
1 2
1 1
1 2
1 1
1 1
1 257
1 1
1 1
1 2
1 ...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-...

result:

ok 200000 lines

Test #8:

score: 10
Accepted
time: 188ms
memory: 769336kb

input:

200000 200000
1 1
1 6
1 264
1 274
1 283
1 192
1 78
1 313
1 1
1 2
1 1
1 2
1 1
1 126
1 2
1 1
1 1
1 2
1...

output:

-1
-1
-1
-1
-1
-1
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1...

result:

ok 200000 lines

Test #9:

score: 10
Accepted
time: 515ms
memory: 789360kb

input:

500000 1000000
1 448
1 1
1 2
1 2
1 1
1 1
1 1
1 2
1 1
1 2
1 2
1 1
1 573
1 533
1 554
1 534
1 2
1 404
1...

output:

-1
-1
-1
-1
-1
-1
-1
1
-1
-1
-1
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
2
-1
1
-1
-1
-1
-1...

result:

ok 1000000 lines

Test #10:

score: 10
Accepted
time: 529ms
memory: 789364kb

input:

500000 1000000
1 649
1 1
1 1
1 1
1 2
1 2
1 2
1 2
1 1
1 2
1 2
1 2
1 428
1 499
1 308
1 129
1 1
1 375
1...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 1000000 lines

Extra Test:

score: 0
Extra Test Passed