#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
os << '{';
for(int i=0;i<sz(a);i++)
{
if(i>0&&i<sz(a)-1)
os << ", ";
os << a[i];
}
os << '}';
return os;
}
int h,w;
const int N=5e4+5;
int sparce[2][2][N][20];
vector<int> ver(N),hor(N);
map<int,int> memo[2][N];///0-up,down, 1-levo,desno;
///0,0 up 0,1 down 1,0 left
int up(int tr,int v)
{
for(int j=0;j<20;j++)
{
if(sparce[0][0][tr][j]>v)
{
if(j==0)
return tr-1;
else
return up(tr-(1<<(j-1)),v);
}
}
return -1;
}
int down(int tr,int v)
{
for(int j=0;j<20;j++)
{
if(sparce[0][1][tr][j]>v)
{
if(j==0)
return tr+1;
else
return down(tr+(1<<(j-1)),v);
}
}
return -1;
}
int left(int tr,int v)
{
for(int j=0;j<20;j++)
{
if(sparce[1][0][tr][j]>v)
{
if(j==0)
return tr-1;
else
return left(tr-(1<<(j-1)),v);
}
}
return -1;
}
int right(int tr,int v)
{
for(int j=0;j<20;j++)
{
if(sparce[1][1][tr][j]>v)
{
if(j==0)
return tr+1;
else
return right(tr+(1<<(j-1)),v);
}
}
return -1;
}
int calc(int x,int y,int br)
{
if(memo[br][x][y]!=0)
return memo[br][x][y];
//printf("Usao za %i %i %i\n",x,y,br);
if(br==0)
{
int upp=up(x,ver[y]);
int downn=down(x,ver[y]);
//printf("Up: %i, down: %i\n",upp,downn);
if(upp==-1)
{
memo[br][x][y]=x;
}
else
{
memo[br][x][y]=calc(upp,y,1)+x-upp;
}
if(downn==-1)
{
memo[br][x][y]=max(memo[br][x][y],h-1-x);
}
else
{
memo[br][x][y]=max(memo[br][x][y],calc(downn,y,1)+downn-x);
}
}
else
{
int levo=left(y,hor[x]);
int desno=right(y,hor[x]);
//printf("Levo: %i, desno: %i\n",levo,desno);
if(levo==-1)
{
memo[br][x][y]=y;
}
else
{
memo[br][x][y]=calc(x,levo,0)+y-levo;
}
if(desno==-1)
{
memo[br][x][y]=max(memo[br][x][y],w-1-y);
}
else
{
memo[br][x][y]=max(memo[br][x][y],calc(x,desno,0)+desno-y);
}
}
return memo[br][x][y];
}
int main()
{
int q;
scanf("%i %i %i",&h,&w,&q);
for(int i=0;i<h;i++)
scanf("%i",&hor[i]);
sparce[0][0][0][0]=-1;
for(int i=1;i<h;i++)
{
sparce[0][0][i][0]=hor[i-1];
}
for(int j=1;j<20;j++)
{
int m=1<<(j-1);
for(int i=0;i<h;i++)
{
if(i-m<0)
sparce[0][0][i][j]=sparce[0][0][i][j-1];
else
sparce[0][0][i][j]=max(sparce[0][0][i-m][j-1],sparce[0][0][i][j-1]);
}
}
sparce[0][1][h-1][0]=-1;
for(int i=0;i<h-1;i++)
{
sparce[0][1][i][0]=hor[i+1];
}
for(int j=1;j<20;j++)
{
int m=1<<(j-1);
for(int i=0;i<h;i++)
{
if(i+m>h-1)
sparce[0][1][i][j]=sparce[0][1][i][j-1];
else
sparce[0][1][i][j]=max(sparce[0][1][i+m][j-1],sparce[0][1][i][j-1]);
}
}
for(int i=0;i<w;i++)
scanf("%i",&ver[i]);
sparce[1][0][0][0]=-1;
for(int i=1;i<w;i++)
{
sparce[1][0][i][0]=ver[i-1];
}
for(int j=1;j<20;j++)
{
int m=1<<(j-1);
for(int i=0;i<w;i++)
{
if(i-m<0)
sparce[1][0][i][j]=sparce[1][0][i][j-1];
else
sparce[1][0][i][j]=max(sparce[1][0][i-m][j-1],sparce[1][0][i][j-1]);
}
}
sparce[1][1][w-1][0]=-1;
for(int i=0;i<w-1;i++)
{
sparce[1][1][i][0]=ver[i+1];
}
for(int j=1;j<20;j++)
{
int m=1<<(j-1);
for(int i=0;i<w;i++)
{
if(i+m>w-1)
sparce[1][1][i][j]=sparce[1][1][i][j-1];
else
sparce[1][1][i][j]=max(sparce[1][1][i+m][j-1],sparce[1][1][i][j-1]);
}
}
for(int i=0;i<q;i++)
{
int x,y;
scanf("%i %i",&x,&y);
x--;
y--;
printf("%i\n",max(calc(x,y,0),calc(x,y,1)));
}
return 0;
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:149:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i",&h,&w,&q);
~~~~~^~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:151:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&hor[i]);
~~~~~^~~~~~~~~~~~~~
abduction2.cpp:185:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&ver[i]);
~~~~~^~~~~~~~~~~~~~
abduction2.cpp:221:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&x,&y);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5500 KB |
Output is correct |
2 |
Correct |
7 ms |
5628 KB |
Output is correct |
3 |
Correct |
7 ms |
5644 KB |
Output is correct |
4 |
Correct |
7 ms |
5644 KB |
Output is correct |
5 |
Correct |
7 ms |
5644 KB |
Output is correct |
6 |
Correct |
6 ms |
5756 KB |
Output is correct |
7 |
Correct |
6 ms |
5756 KB |
Output is correct |
8 |
Correct |
7 ms |
5756 KB |
Output is correct |
9 |
Correct |
6 ms |
5756 KB |
Output is correct |
10 |
Correct |
6 ms |
5756 KB |
Output is correct |
11 |
Correct |
6 ms |
5756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5500 KB |
Output is correct |
2 |
Correct |
7 ms |
5628 KB |
Output is correct |
3 |
Correct |
7 ms |
5644 KB |
Output is correct |
4 |
Correct |
7 ms |
5644 KB |
Output is correct |
5 |
Correct |
7 ms |
5644 KB |
Output is correct |
6 |
Correct |
6 ms |
5756 KB |
Output is correct |
7 |
Correct |
6 ms |
5756 KB |
Output is correct |
8 |
Correct |
7 ms |
5756 KB |
Output is correct |
9 |
Correct |
6 ms |
5756 KB |
Output is correct |
10 |
Correct |
6 ms |
5756 KB |
Output is correct |
11 |
Correct |
6 ms |
5756 KB |
Output is correct |
12 |
Correct |
8 ms |
6144 KB |
Output is correct |
13 |
Correct |
8 ms |
6144 KB |
Output is correct |
14 |
Correct |
8 ms |
6284 KB |
Output is correct |
15 |
Correct |
8 ms |
6324 KB |
Output is correct |
16 |
Correct |
8 ms |
6324 KB |
Output is correct |
17 |
Correct |
8 ms |
6324 KB |
Output is correct |
18 |
Correct |
8 ms |
6340 KB |
Output is correct |
19 |
Correct |
9 ms |
6468 KB |
Output is correct |
20 |
Correct |
9 ms |
6596 KB |
Output is correct |
21 |
Correct |
9 ms |
6596 KB |
Output is correct |
22 |
Correct |
10 ms |
6864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5500 KB |
Output is correct |
2 |
Correct |
7 ms |
5628 KB |
Output is correct |
3 |
Correct |
7 ms |
5644 KB |
Output is correct |
4 |
Correct |
7 ms |
5644 KB |
Output is correct |
5 |
Correct |
7 ms |
5644 KB |
Output is correct |
6 |
Correct |
6 ms |
5756 KB |
Output is correct |
7 |
Correct |
6 ms |
5756 KB |
Output is correct |
8 |
Correct |
7 ms |
5756 KB |
Output is correct |
9 |
Correct |
6 ms |
5756 KB |
Output is correct |
10 |
Correct |
6 ms |
5756 KB |
Output is correct |
11 |
Correct |
6 ms |
5756 KB |
Output is correct |
12 |
Correct |
8 ms |
6144 KB |
Output is correct |
13 |
Correct |
8 ms |
6144 KB |
Output is correct |
14 |
Correct |
8 ms |
6284 KB |
Output is correct |
15 |
Correct |
8 ms |
6324 KB |
Output is correct |
16 |
Correct |
8 ms |
6324 KB |
Output is correct |
17 |
Correct |
8 ms |
6324 KB |
Output is correct |
18 |
Correct |
8 ms |
6340 KB |
Output is correct |
19 |
Correct |
9 ms |
6468 KB |
Output is correct |
20 |
Correct |
9 ms |
6596 KB |
Output is correct |
21 |
Correct |
9 ms |
6596 KB |
Output is correct |
22 |
Correct |
10 ms |
6864 KB |
Output is correct |
23 |
Correct |
63 ms |
22404 KB |
Output is correct |
24 |
Correct |
54 ms |
23332 KB |
Output is correct |
25 |
Correct |
66 ms |
24468 KB |
Output is correct |
26 |
Correct |
55 ms |
25368 KB |
Output is correct |
27 |
Correct |
69 ms |
26284 KB |
Output is correct |
28 |
Correct |
63 ms |
32372 KB |
Output is correct |
29 |
Correct |
54 ms |
32372 KB |
Output is correct |
30 |
Correct |
90 ms |
34592 KB |
Output is correct |
31 |
Correct |
94 ms |
37044 KB |
Output is correct |
32 |
Correct |
54 ms |
37044 KB |
Output is correct |
33 |
Correct |
62 ms |
37044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
37044 KB |
Output is correct |
2 |
Correct |
9 ms |
37044 KB |
Output is correct |
3 |
Correct |
9 ms |
37044 KB |
Output is correct |
4 |
Correct |
10 ms |
37044 KB |
Output is correct |
5 |
Correct |
10 ms |
37044 KB |
Output is correct |
6 |
Correct |
11 ms |
37044 KB |
Output is correct |
7 |
Correct |
9 ms |
37044 KB |
Output is correct |
8 |
Correct |
10 ms |
37044 KB |
Output is correct |
9 |
Correct |
10 ms |
37044 KB |
Output is correct |
10 |
Correct |
10 ms |
37044 KB |
Output is correct |
11 |
Correct |
12 ms |
37044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5500 KB |
Output is correct |
2 |
Correct |
7 ms |
5628 KB |
Output is correct |
3 |
Correct |
7 ms |
5644 KB |
Output is correct |
4 |
Correct |
7 ms |
5644 KB |
Output is correct |
5 |
Correct |
7 ms |
5644 KB |
Output is correct |
6 |
Correct |
6 ms |
5756 KB |
Output is correct |
7 |
Correct |
6 ms |
5756 KB |
Output is correct |
8 |
Correct |
7 ms |
5756 KB |
Output is correct |
9 |
Correct |
6 ms |
5756 KB |
Output is correct |
10 |
Correct |
6 ms |
5756 KB |
Output is correct |
11 |
Correct |
6 ms |
5756 KB |
Output is correct |
12 |
Correct |
8 ms |
6144 KB |
Output is correct |
13 |
Correct |
8 ms |
6144 KB |
Output is correct |
14 |
Correct |
8 ms |
6284 KB |
Output is correct |
15 |
Correct |
8 ms |
6324 KB |
Output is correct |
16 |
Correct |
8 ms |
6324 KB |
Output is correct |
17 |
Correct |
8 ms |
6324 KB |
Output is correct |
18 |
Correct |
8 ms |
6340 KB |
Output is correct |
19 |
Correct |
9 ms |
6468 KB |
Output is correct |
20 |
Correct |
9 ms |
6596 KB |
Output is correct |
21 |
Correct |
9 ms |
6596 KB |
Output is correct |
22 |
Correct |
10 ms |
6864 KB |
Output is correct |
23 |
Correct |
63 ms |
22404 KB |
Output is correct |
24 |
Correct |
54 ms |
23332 KB |
Output is correct |
25 |
Correct |
66 ms |
24468 KB |
Output is correct |
26 |
Correct |
55 ms |
25368 KB |
Output is correct |
27 |
Correct |
69 ms |
26284 KB |
Output is correct |
28 |
Correct |
63 ms |
32372 KB |
Output is correct |
29 |
Correct |
54 ms |
32372 KB |
Output is correct |
30 |
Correct |
90 ms |
34592 KB |
Output is correct |
31 |
Correct |
94 ms |
37044 KB |
Output is correct |
32 |
Correct |
54 ms |
37044 KB |
Output is correct |
33 |
Correct |
62 ms |
37044 KB |
Output is correct |
34 |
Correct |
9 ms |
37044 KB |
Output is correct |
35 |
Correct |
9 ms |
37044 KB |
Output is correct |
36 |
Correct |
9 ms |
37044 KB |
Output is correct |
37 |
Correct |
10 ms |
37044 KB |
Output is correct |
38 |
Correct |
10 ms |
37044 KB |
Output is correct |
39 |
Correct |
11 ms |
37044 KB |
Output is correct |
40 |
Correct |
9 ms |
37044 KB |
Output is correct |
41 |
Correct |
10 ms |
37044 KB |
Output is correct |
42 |
Correct |
10 ms |
37044 KB |
Output is correct |
43 |
Correct |
10 ms |
37044 KB |
Output is correct |
44 |
Correct |
12 ms |
37044 KB |
Output is correct |
45 |
Correct |
59 ms |
37044 KB |
Output is correct |
46 |
Correct |
60 ms |
37044 KB |
Output is correct |
47 |
Correct |
57 ms |
37044 KB |
Output is correct |
48 |
Correct |
70 ms |
37044 KB |
Output is correct |
49 |
Correct |
56 ms |
37700 KB |
Output is correct |
50 |
Correct |
63 ms |
42616 KB |
Output is correct |
51 |
Correct |
67 ms |
44120 KB |
Output is correct |
52 |
Correct |
109 ms |
48536 KB |
Output is correct |
53 |
Correct |
114 ms |
49076 KB |
Output is correct |
54 |
Correct |
125 ms |
49816 KB |
Output is correct |
55 |
Incorrect |
122 ms |
54996 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |