#include<bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> tp;
const ll N = 100005, L = 17, inf = 1e18;
ll h, w, q, a[N], inv[N], prv[L][N], nxt[L][N], vis[N];
priority_queue<tp> pq[N];
vector<pll> ord, st;
ll getprv (ll P, ll V) {
if(a[P] > V) {
for(ll i=P-1;i>=1;i--) {
if(a[i] > V) return i;
}
return 0;
}
ll I = P;
for(ll i=L;i--;) {
if(a[prv[i][I]] <= V) I = prv[i][I];
}
return prv[0][I];
}
ll getnxt (ll P, ll V) {
if(a[P] > V) {
for(ll i=P+1;i<=h+w;i++) {
if(a[i] > V) return i;
}
return 0;
}
ll I = P;
for(ll i=L;i--;) {
if(a[nxt[i][I]] <= V) I = nxt[i][I];
}
return nxt[0][I];
}
void solve () {
ll P, Q, R = 0;
scanf("%lld%lld",&P,&Q);
Q += h;
pq[inv[P]].push(tp(0, P, Q));
pq[inv[Q]].push(tp(0, Q, P));
for(ll i=1;i<=h+w;i++) vis[i] = 0;
for(ll i=1;i<=h+w;i++) while(!pq[i].empty()) {
ll A, B, C; tie(A, B, C) = pq[i].top(); pq[i].pop();
if(vis[C] == i) continue;
vis[C] = i;
ll X = getprv(C, a[B]), Y = getnxt(C, a[B]);
if(X && (X<=h)==(C<=h)) pq[inv[X]].push(tp(A+C-X, X, B));
else R = max(R, A + C - (C<=h?1:h+1));
if(Y && (Y<=h)==(C<=h)) pq[inv[Y]].push(tp(A+Y-C, Y, B));
else R = max(R, A + (C<=h?h:h+w) - C);
}
printf("%lld\n",R);
}
int main()
{
scanf("%lld%lld%lld",&h,&w,&q);
ord.push_back({0, 0});
for(ll i=1;i<=h+w;i++) {
scanf("%lld",&a[i]);
ord.push_back({a[i], i});
}
sort(ord.begin(), ord.end());
for(ll i=1;i<=h+w;i++) {
inv[ord[i].Y] = i;
}
st.push_back({inf, 0});
for(ll i=1;i<=h+w;i++) {
while(st.back().X <= a[i]) st.pop_back();
prv[0][i] = st.back().Y;
st.push_back({a[i], i});
}
st.clear();
st.push_back({inf, 0});
for(ll i=h+w;i>=1;i--) {
while(st.back().X <= a[i]) st.pop_back();
nxt[0][i] = st.back().Y;
st.push_back({a[i], i});
}
a[0] = inf;
for(ll k=1;k<L;k++) for(ll i=1;i<=h+w;i++) {
nxt[k][i] = nxt[k-1][nxt[k-1][i]];
prv[k][i] = prv[k-1][prv[k-1][i]];
}
while(q--) solve();
}
Compilation message
abduction2.cpp: In function 'void solve()':
abduction2.cpp:45:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&P,&Q);
^
abduction2.cpp: In function 'int main()':
abduction2.cpp:65:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld",&h,&w,&q);
^
abduction2.cpp:68:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&a[i]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
34056 KB |
Output is correct |
2 |
Correct |
0 ms |
34056 KB |
Output is correct |
3 |
Correct |
0 ms |
34056 KB |
Output is correct |
4 |
Correct |
0 ms |
34056 KB |
Output is correct |
5 |
Correct |
0 ms |
34056 KB |
Output is correct |
6 |
Correct |
0 ms |
34056 KB |
Output is correct |
7 |
Correct |
0 ms |
34056 KB |
Output is correct |
8 |
Correct |
3 ms |
34056 KB |
Output is correct |
9 |
Correct |
0 ms |
34056 KB |
Output is correct |
10 |
Correct |
0 ms |
34056 KB |
Output is correct |
11 |
Correct |
0 ms |
34056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
34056 KB |
Output is correct |
2 |
Correct |
0 ms |
34056 KB |
Output is correct |
3 |
Correct |
0 ms |
34056 KB |
Output is correct |
4 |
Correct |
0 ms |
34056 KB |
Output is correct |
5 |
Correct |
0 ms |
34056 KB |
Output is correct |
6 |
Correct |
0 ms |
34056 KB |
Output is correct |
7 |
Correct |
0 ms |
34056 KB |
Output is correct |
8 |
Correct |
3 ms |
34056 KB |
Output is correct |
9 |
Correct |
0 ms |
34056 KB |
Output is correct |
10 |
Correct |
0 ms |
34056 KB |
Output is correct |
11 |
Correct |
0 ms |
34056 KB |
Output is correct |
12 |
Correct |
3 ms |
34196 KB |
Output is correct |
13 |
Correct |
0 ms |
34196 KB |
Output is correct |
14 |
Correct |
3 ms |
34196 KB |
Output is correct |
15 |
Correct |
3 ms |
34196 KB |
Output is correct |
16 |
Correct |
3 ms |
34196 KB |
Output is correct |
17 |
Correct |
0 ms |
34196 KB |
Output is correct |
18 |
Correct |
3 ms |
34196 KB |
Output is correct |
19 |
Correct |
0 ms |
34328 KB |
Output is correct |
20 |
Correct |
6 ms |
34460 KB |
Output is correct |
21 |
Correct |
3 ms |
34328 KB |
Output is correct |
22 |
Correct |
3 ms |
34460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
34056 KB |
Output is correct |
2 |
Correct |
0 ms |
34056 KB |
Output is correct |
3 |
Correct |
0 ms |
34056 KB |
Output is correct |
4 |
Correct |
0 ms |
34056 KB |
Output is correct |
5 |
Correct |
0 ms |
34056 KB |
Output is correct |
6 |
Correct |
0 ms |
34056 KB |
Output is correct |
7 |
Correct |
0 ms |
34056 KB |
Output is correct |
8 |
Correct |
3 ms |
34056 KB |
Output is correct |
9 |
Correct |
0 ms |
34056 KB |
Output is correct |
10 |
Correct |
0 ms |
34056 KB |
Output is correct |
11 |
Correct |
0 ms |
34056 KB |
Output is correct |
12 |
Correct |
3 ms |
34196 KB |
Output is correct |
13 |
Correct |
0 ms |
34196 KB |
Output is correct |
14 |
Correct |
3 ms |
34196 KB |
Output is correct |
15 |
Correct |
3 ms |
34196 KB |
Output is correct |
16 |
Correct |
3 ms |
34196 KB |
Output is correct |
17 |
Correct |
0 ms |
34196 KB |
Output is correct |
18 |
Correct |
3 ms |
34196 KB |
Output is correct |
19 |
Correct |
0 ms |
34328 KB |
Output is correct |
20 |
Correct |
6 ms |
34460 KB |
Output is correct |
21 |
Correct |
3 ms |
34328 KB |
Output is correct |
22 |
Correct |
3 ms |
34460 KB |
Output is correct |
23 |
Correct |
33 ms |
37212 KB |
Output is correct |
24 |
Correct |
39 ms |
37212 KB |
Output is correct |
25 |
Correct |
36 ms |
37212 KB |
Output is correct |
26 |
Correct |
39 ms |
37212 KB |
Output is correct |
27 |
Correct |
33 ms |
37212 KB |
Output is correct |
28 |
Correct |
76 ms |
38232 KB |
Output is correct |
29 |
Correct |
39 ms |
38232 KB |
Output is correct |
30 |
Correct |
99 ms |
41564 KB |
Output is correct |
31 |
Correct |
106 ms |
43016 KB |
Output is correct |
32 |
Correct |
39 ms |
37212 KB |
Output is correct |
33 |
Correct |
43 ms |
37868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
34196 KB |
Output is correct |
2 |
Correct |
3 ms |
34196 KB |
Output is correct |
3 |
Correct |
3 ms |
34196 KB |
Output is correct |
4 |
Correct |
6 ms |
34196 KB |
Output is correct |
5 |
Correct |
3 ms |
34196 KB |
Output is correct |
6 |
Correct |
33 ms |
34196 KB |
Output is correct |
7 |
Correct |
29 ms |
34196 KB |
Output is correct |
8 |
Correct |
66 ms |
34592 KB |
Output is correct |
9 |
Correct |
76 ms |
34592 KB |
Output is correct |
10 |
Correct |
76 ms |
34592 KB |
Output is correct |
11 |
Correct |
103 ms |
34592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
34056 KB |
Output is correct |
2 |
Correct |
0 ms |
34056 KB |
Output is correct |
3 |
Correct |
0 ms |
34056 KB |
Output is correct |
4 |
Correct |
0 ms |
34056 KB |
Output is correct |
5 |
Correct |
0 ms |
34056 KB |
Output is correct |
6 |
Correct |
0 ms |
34056 KB |
Output is correct |
7 |
Correct |
0 ms |
34056 KB |
Output is correct |
8 |
Correct |
3 ms |
34056 KB |
Output is correct |
9 |
Correct |
0 ms |
34056 KB |
Output is correct |
10 |
Correct |
0 ms |
34056 KB |
Output is correct |
11 |
Correct |
0 ms |
34056 KB |
Output is correct |
12 |
Correct |
3 ms |
34196 KB |
Output is correct |
13 |
Correct |
0 ms |
34196 KB |
Output is correct |
14 |
Correct |
3 ms |
34196 KB |
Output is correct |
15 |
Correct |
3 ms |
34196 KB |
Output is correct |
16 |
Correct |
3 ms |
34196 KB |
Output is correct |
17 |
Correct |
0 ms |
34196 KB |
Output is correct |
18 |
Correct |
3 ms |
34196 KB |
Output is correct |
19 |
Correct |
0 ms |
34328 KB |
Output is correct |
20 |
Correct |
6 ms |
34460 KB |
Output is correct |
21 |
Correct |
3 ms |
34328 KB |
Output is correct |
22 |
Correct |
3 ms |
34460 KB |
Output is correct |
23 |
Correct |
33 ms |
37212 KB |
Output is correct |
24 |
Correct |
39 ms |
37212 KB |
Output is correct |
25 |
Correct |
36 ms |
37212 KB |
Output is correct |
26 |
Correct |
39 ms |
37212 KB |
Output is correct |
27 |
Correct |
33 ms |
37212 KB |
Output is correct |
28 |
Correct |
76 ms |
38232 KB |
Output is correct |
29 |
Correct |
39 ms |
38232 KB |
Output is correct |
30 |
Correct |
99 ms |
41564 KB |
Output is correct |
31 |
Correct |
106 ms |
43016 KB |
Output is correct |
32 |
Correct |
39 ms |
37212 KB |
Output is correct |
33 |
Correct |
43 ms |
37868 KB |
Output is correct |
34 |
Correct |
6 ms |
34196 KB |
Output is correct |
35 |
Correct |
3 ms |
34196 KB |
Output is correct |
36 |
Correct |
3 ms |
34196 KB |
Output is correct |
37 |
Correct |
6 ms |
34196 KB |
Output is correct |
38 |
Correct |
3 ms |
34196 KB |
Output is correct |
39 |
Correct |
33 ms |
34196 KB |
Output is correct |
40 |
Correct |
29 ms |
34196 KB |
Output is correct |
41 |
Correct |
66 ms |
34592 KB |
Output is correct |
42 |
Correct |
76 ms |
34592 KB |
Output is correct |
43 |
Correct |
76 ms |
34592 KB |
Output is correct |
44 |
Correct |
103 ms |
34592 KB |
Output is correct |
45 |
Correct |
76 ms |
37212 KB |
Output is correct |
46 |
Correct |
66 ms |
37212 KB |
Output is correct |
47 |
Correct |
69 ms |
37212 KB |
Output is correct |
48 |
Correct |
69 ms |
37212 KB |
Output is correct |
49 |
Correct |
63 ms |
37212 KB |
Output is correct |
50 |
Correct |
1806 ms |
38496 KB |
Output is correct |
51 |
Correct |
1583 ms |
38628 KB |
Output is correct |
52 |
Correct |
3066 ms |
46184 KB |
Output is correct |
53 |
Correct |
2799 ms |
45920 KB |
Output is correct |
54 |
Correct |
2909 ms |
45260 KB |
Output is correct |
55 |
Correct |
3393 ms |
47504 KB |
Output is correct |
56 |
Correct |
1186 ms |
47140 KB |
Output is correct |
57 |
Correct |
446 ms |
43972 KB |
Output is correct |
58 |
Correct |
333 ms |
43840 KB |
Output is correct |
59 |
Correct |
493 ms |
44500 KB |
Output is correct |