ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#190160 | #3326. 很多集合(sets) | gaojieming | 100 | 3505ms | 319116kb | C++11 | 2.3kb | 2023-10-05 11:02:51 | 2023-10-05 12:47:33 |
answer
#include<bits/stdc++.h>
#define ll long long
#define il inline
#define pn putchar('\n')
#define maxint 2147483647
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
#define maxn 500005
using namespace std;
int n,q,sq=200,idx;
int a[maxn<<1],fr[maxn],ind[maxn],si[maxn],g[2501][2501];
bitset<1000001>f[2501];
signed main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%d",&si[i]);
fr[i]=fr[i-1]+si[i-1];
for(int j=0;j<si[i];j++)
{
int x;
scanf("%d",&x);
a[fr[i]+j]=x;
}
if(si[i]<=sq)
sort(a+fr[i],a+fr[i]+si[i]);
else
{
ind[i]=++idx;
for(int j=0;j<si[i];j++)
f[idx][a[fr[i]+j]]=1;
for(int j=1;j<idx;j++)
{
bool fl=0;
for(int k=0;k<si[i];k++)
{
if(f[j][a[fr[i]+k]])
{
fl=1;
g[j][idx]=g[idx][j]=a[fr[i]+k];
break;
}
}
if(!fl)
g[j][idx]=g[idx][j]=-1;
}
}
}
while(q--)
{
int x,y;
scanf("%d%d",&x,&y);
if(si[x]>si[y])
swap(x,y);
if(si[x]>sq)
printf("%d\n",g[ind[x]][ind[y]]);
else if(si[y]>sq)
{
bool fl=0;
for(int i=0;i<si[x];i++)
{
if(f[ind[y]][a[fr[x]+i]])
{
fl=1,printf("%d\n",a[fr[x]+i]);
break;
}
}
if(!fl)
puts("-1");
}
else
{
bool fl=0;
for(int i=0,j=0;i<si[x];i++)
{
while(j<si[y]&&a[fr[y]+j]<a[fr[x]+i])j++;
if(j<si[y]&&a[fr[x]+i]==a[fr[y]+j])
{
fl=1;
printf("%d\n",a[fr[x]+i]);
break;
}
}
if(!fl)
puts("-1");
}
}
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 27ms
memory: 306584kb
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: 20ms
memory: 306580kb
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: 216ms
memory: 306556kb
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: 292ms
memory: 306552kb
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: 266ms
memory: 306552kb
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: 242ms
memory: 312236kb
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: 209ms
memory: 312232kb
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: 209ms
memory: 312236kb
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: 940ms
memory: 319112kb
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: 1084ms
memory: 319116kb
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