This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h>
#include<memory.h>
#include<algorithm>
#include<map>
using namespace std;
typedef long long lld;
struct seg {
int j2, itr[150000];
seg(){ j2 = 1<<16; memset(itr, 0x7f, sizeof(itr)); }
const int& operator[] (const int ix){ return itr[j2+ix]; }
void add(int ix, int val){
ix+=j2; itr[ix]=val;
for(ix>>=1; ix; ix>>=1) itr[ix]=max(itr[ix*2],itr[ix*2+1]);
}
int getup(int ix, int val){
ix+=j2;
while(1){
if(itr[ix]>val){
if(ix>=j2) break;
if(itr[ix*2]>val) ix*=2;
else ix*=2, ix++;
}
else{
if(ix%2) ix++;
else ix>>=1;
}
}
return ix-j2;
}
int getdown(int ix, int val){
ix+=j2;
while(1){
if(itr[ix]>val){
if(ix>=j2) break;
if(itr[ix*2+1]>val) ix*=2, ix++;
else ix*=2;
}
else{
if(ix%2 == 0) ix--;
else ix>>=1;
}
}
return ix-j2;
}
} ho, ver;
int H, W, Q;
map<pair<int,int>, lld> vd, hd;
lld verti(int, int);
lld hori(int, int);
lld verti(int h, int v){
int gap = vd[make_pair(h,v)];
if(gap) return gap;
lld up=0, down=0;
up = ho.getup(h+1, ver[v]);
if(up<=H) up = up-h + hori(v, up);
else up = H-h;
down = ho.getdown(h-1, ver[v]);
if(down>=1) down = h-down + hori(v, down);
else down = h-1;
return vd[make_pair(h,v)] = max(up, down);
}
lld hori(int v, int h){
int gap = hd[make_pair(h,v)];
if(gap) return gap;
lld up=0, down=0;
up = ver.getup(v+1, ho[h]);
if(up<=W) up = up-v + verti(h, up);
else up = W-v;
down = ver.getdown(v-1, ho[h]);
if(down>=1) down = v-down + verti(h, down);
else down = v-1;
return hd[make_pair(h,v)] = max(up, down);
}
int main(){
scanf("%d%d%d", &H, &W, &Q);
for(int i=1; i<=H; i++){
int a; scanf("%d", &a);
ho.add(i, a);
}
for(int i=1; i<=W; i++){
int a; scanf("%d", &a);
ver.add(i, a);
}
while(Q--){
int h, v;
scanf("%d%d", &h, &v);
printf("%lld\n", max(verti(h, v), hori(v, h)));
}
return 0;
}
Compilation message (stderr)
abduction2.cpp: In function 'int main()':
abduction2.cpp:85:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &H, &W, &Q);
^
abduction2.cpp:87:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a; scanf("%d", &a);
^
abduction2.cpp:91:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a; scanf("%d", &a);
^
abduction2.cpp:96:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &h, &v);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |