#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int inf=1e9;
struct ain
{
int aint[(1<<18)];
void update(int nod,int st,int dr,int poz,int val)
{
if (st==dr){
aint[nod]=val;
return ;
}
int mij=(st+dr)/2;
if (poz<=mij)
update(nod*2,st,mij,poz,val);
else
update(nod*2+1,mij+1,dr,poz,val);
aint[nod]=max(aint[nod*2],aint[nod*2+1]);
}
int query1(int nod,int st,int dr,int poz,int val)
{
if (poz<=st || aint[nod]<val)
return inf;
if (st==dr)
return st;
int mij=(st+dr)/2,ans;
ans=query1(nod*2+1,mij+1,dr,poz,val);
if (ans<poz)
return ans;
return query1(nod*2,st,mij,poz,val);
}
int query2(int nod,int st,int dr,int poz,int val)
{
if (dr<=poz || aint[nod]<val)
return -1;
if (st==dr)
return st;
int mij=(st+dr)/2,ans;
ans=query2(nod*2,st,mij,poz,val);
if (ans>poz)
return ans;
return query2(nod*2+1,mij+1,dr,poz,val);
}
}ai1,ai2;
ll quer1(int x,int y);
ll quer2(int x,int y);
int h,w,a[50005],b[50005];
ll has(int x,int y)
{
return 1LL*x*50003+y;
}
map<ll,ll>mp1,mp2;
ll quer1(int x,int y)
{
if (mp1[has(x,y)])
return mp1[has(x,y)];
ll ans1,ans2;
int poz;
poz=ai1.query1(1,1,h,x,b[y]);
if (poz<x)
ans1=quer2(poz,y)+x-poz;
else
ans1=x-1;
poz=ai1.query2(1,1,h,x,b[y]);
if (poz>x)
ans2=quer2(poz,y)+poz-x;
else
ans2=h-x;
mp1[has(x,y)]=max(ans1,ans2);
return max(ans1,ans2);
}
ll quer2(int x,int y)
{
if (mp2[has(x,y)])
return mp2[has(x,y)];
ll ans1,ans2;
int poz;
poz=ai2.query1(1,1,w,y,a[x]);
if (poz<y)
ans1=quer1(x,poz)+y-poz;
else
ans1=y-1;
poz=ai2.query2(1,1,w,y,a[x]);
if (poz>y)
ans2=quer1(x,poz)+poz-y;
else
ans2=w-y;
mp2[has(x,y)]=max(ans1,ans2);
ll aux=mp2[has(x,y)];
return aux;
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int q,i;
scanf("%d%d%d",&h,&w,&q);
for(i=1;i<=h;i++)
scanf("%d",&a[i]),ai1.update(1,1,h,i,a[i]);
for(i=1;i<=w;i++)
scanf("%d",&b[i]),ai2.update(1,1,w,i,b[i]);
for(i=1;i<=q;i++){
int x,y;
scanf("%d%d",&x,&y);
ll a=quer1(x,y);
ll b=quer2(x,y);
printf("%lld\n",max(a,b));
}
return 0;
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:98:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
98 | scanf("%d%d%d",&h,&w,&q);
| ~~~~~^~~~~~~~~~~~~~~~~~~
abduction2.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
100 | scanf("%d",&a[i]),ai1.update(1,1,h,i,a[i]);
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d",&b[i]),ai2.update(1,1,w,i,b[i]);
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
105 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
4 ms |
748 KB |
Output is correct |
21 |
Correct |
4 ms |
748 KB |
Output is correct |
22 |
Correct |
6 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
4 ms |
748 KB |
Output is correct |
21 |
Correct |
4 ms |
748 KB |
Output is correct |
22 |
Correct |
6 ms |
1004 KB |
Output is correct |
23 |
Correct |
28 ms |
2668 KB |
Output is correct |
24 |
Correct |
28 ms |
2668 KB |
Output is correct |
25 |
Correct |
28 ms |
2668 KB |
Output is correct |
26 |
Correct |
37 ms |
2668 KB |
Output is correct |
27 |
Correct |
35 ms |
2668 KB |
Output is correct |
28 |
Correct |
50 ms |
9836 KB |
Output is correct |
29 |
Correct |
31 ms |
3704 KB |
Output is correct |
30 |
Correct |
106 ms |
9964 KB |
Output is correct |
31 |
Correct |
130 ms |
12140 KB |
Output is correct |
32 |
Correct |
32 ms |
3180 KB |
Output is correct |
33 |
Correct |
50 ms |
5228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
620 KB |
Output is correct |
3 |
Correct |
4 ms |
620 KB |
Output is correct |
4 |
Correct |
4 ms |
620 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
3 ms |
748 KB |
Output is correct |
7 |
Correct |
3 ms |
748 KB |
Output is correct |
8 |
Correct |
7 ms |
876 KB |
Output is correct |
9 |
Correct |
6 ms |
1004 KB |
Output is correct |
10 |
Correct |
6 ms |
876 KB |
Output is correct |
11 |
Correct |
7 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
4 ms |
748 KB |
Output is correct |
21 |
Correct |
4 ms |
748 KB |
Output is correct |
22 |
Correct |
6 ms |
1004 KB |
Output is correct |
23 |
Correct |
28 ms |
2668 KB |
Output is correct |
24 |
Correct |
28 ms |
2668 KB |
Output is correct |
25 |
Correct |
28 ms |
2668 KB |
Output is correct |
26 |
Correct |
37 ms |
2668 KB |
Output is correct |
27 |
Correct |
35 ms |
2668 KB |
Output is correct |
28 |
Correct |
50 ms |
9836 KB |
Output is correct |
29 |
Correct |
31 ms |
3704 KB |
Output is correct |
30 |
Correct |
106 ms |
9964 KB |
Output is correct |
31 |
Correct |
130 ms |
12140 KB |
Output is correct |
32 |
Correct |
32 ms |
3180 KB |
Output is correct |
33 |
Correct |
50 ms |
5228 KB |
Output is correct |
34 |
Correct |
3 ms |
640 KB |
Output is correct |
35 |
Correct |
4 ms |
620 KB |
Output is correct |
36 |
Correct |
4 ms |
620 KB |
Output is correct |
37 |
Correct |
4 ms |
620 KB |
Output is correct |
38 |
Correct |
4 ms |
768 KB |
Output is correct |
39 |
Correct |
3 ms |
748 KB |
Output is correct |
40 |
Correct |
3 ms |
748 KB |
Output is correct |
41 |
Correct |
7 ms |
876 KB |
Output is correct |
42 |
Correct |
6 ms |
1004 KB |
Output is correct |
43 |
Correct |
6 ms |
876 KB |
Output is correct |
44 |
Correct |
7 ms |
1004 KB |
Output is correct |
45 |
Correct |
33 ms |
3052 KB |
Output is correct |
46 |
Correct |
36 ms |
3068 KB |
Output is correct |
47 |
Correct |
34 ms |
3052 KB |
Output is correct |
48 |
Correct |
32 ms |
3052 KB |
Output is correct |
49 |
Correct |
32 ms |
3052 KB |
Output is correct |
50 |
Correct |
58 ms |
8556 KB |
Output is correct |
51 |
Correct |
59 ms |
9344 KB |
Output is correct |
52 |
Correct |
164 ms |
13804 KB |
Output is correct |
53 |
Correct |
176 ms |
13420 KB |
Output is correct |
54 |
Correct |
158 ms |
13088 KB |
Output is correct |
55 |
Correct |
228 ms |
18796 KB |
Output is correct |
56 |
Correct |
2130 ms |
128364 KB |
Output is correct |
57 |
Correct |
508 ms |
35316 KB |
Output is correct |
58 |
Correct |
469 ms |
33904 KB |
Output is correct |
59 |
Correct |
494 ms |
33516 KB |
Output is correct |