#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
using ll = long long;
const int nmax = 2e5 + 5;
const ll inf = 2e10 + 5;
//#warning vectorii sa ii indexezi de la 1
int h[nmax], v[nmax]; int L, C;
int get(int a) { return (a < 0? h[-a] : v[a]); }
namespace G {
vector< vector<pii> > g;
vector<ll> dp;
vector<ll> occ;
void reset() {
g.clear();
dp.clear();
occ.clear();
}
int newnode() {
g.push_back(vector<pii>());
dp.push_back(-1);
occ.push_back(0);
return g.size() - 1;
}
void addedge(int x, int y, int c) { g[x].push_back({y, c}); }
int startdfs(int node) {
if(occ[node] != 0)
return dp[node];
occ[node] = 1;
for(auto [a, b] : g[node])
dp[node] = max((ll)startdfs(a) + b, dp[node]);
return dp[node];
}
}
int start(int x, int y, bool hor, bool dir) {
//cerr << hor << ' ' << dir << '\n';
//auto cmp = [&](tii a, tii b) { return get(get<0>(a)) > get(get<0>(b)); };
//priority_queue<tuple<int,int,int>, vector< tuple<int,int,int> >, decltype(cmp) > heap(cmp);
auto cmp = [&](int a, int b) { return get(a) < get(b); };
map<int, vector<pii>, decltype(cmp) > heap(cmp);
G::reset();
int source, sink = G::newnode(); // == 0
int l[2] = {x, y}, r[2] = {x, y};
if(hor)
heap[-x].push_back({y, source = G::newnode()}),
(dir == 0? l[1]-- : r[1]++);
else
heap[y].push_back({x, source = G::newnode()}),
(dir == 0? l[0]-- : r[0]++);
int node;
while(!heap.empty()) {
x = heap.begin()->first;
//cerr << x << '\n';
int dir = 0, target = get(x);
if(x < 0)
dir = 1;
while(l[dir] > 0 && (dir == 0? h : v)[l[dir]] < target)
l[dir]--;
while(r[dir] <= (dir == 0? L : C) && (dir == 0? h : v)[r[dir]] < target)
r[dir]++;
//cerr << " > " << x << ' ' << get(x) << ' ' << l[dir] << ' ' << r[dir] << '\n';
int lnode = sink, rnode = sink;
if(l[dir] != 0)
heap[(dir == 0? -1 : 1) * l[dir]].push_back({abs(x), lnode = G::newnode()});
if(r[dir] <= (dir == 0? L : C))
heap[(dir == 0? -1 : 1) * r[dir]].push_back({abs(x), rnode = G::newnode()});
auto &vec = heap[x];
for(auto [coord, atrnode] : vec) {
G::addedge(atrnode, lnode, abs(coord - l[dir]));
G::addedge(atrnode, rnode, abs(coord - r[dir]));
}
vec.clear();
heap.erase(heap.begin());
}
//cerr << G::startdfs(source) << '\n';
//cerr << '\n';
return G::startdfs(source);
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> L >> C;
int q;
cin >> q;
for(int i = 1; i <= L; i++)
cin >> h[i];
for(int i = 1; i <= C; i++)
cin >> v[i];
for(int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
if(get(-x) < get(y))
cout << max({start(x, y, 1, 1), start(x, y, 1, 0)}) << '\n';
else
cout << max({start(x, y, 0, 1), start(x, y, 0, 0)}) << '\n';
//cerr << "-------------------\n";
}
}
Compilation message
abduction2.cpp: In function 'int G::startdfs(int)':
abduction2.cpp:39:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | for(auto [a, b] : g[node])
| ^
abduction2.cpp: In function 'int start(int, int, bool, bool)':
abduction2.cpp:79:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
79 | for(auto [coord, atrnode] : vec) {
| ^
abduction2.cpp:60:7: warning: unused variable 'node' [-Wunused-variable]
60 | int node;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
852 KB |
Output is correct |
23 |
Correct |
10 ms |
676 KB |
Output is correct |
24 |
Correct |
10 ms |
712 KB |
Output is correct |
25 |
Correct |
11 ms |
700 KB |
Output is correct |
26 |
Correct |
11 ms |
724 KB |
Output is correct |
27 |
Correct |
11 ms |
724 KB |
Output is correct |
28 |
Correct |
26 ms |
5020 KB |
Output is correct |
29 |
Correct |
16 ms |
1364 KB |
Output is correct |
30 |
Correct |
47 ms |
7812 KB |
Output is correct |
31 |
Correct |
57 ms |
8936 KB |
Output is correct |
32 |
Correct |
12 ms |
1120 KB |
Output is correct |
33 |
Correct |
21 ms |
2700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
31 ms |
596 KB |
Output is correct |
7 |
Correct |
32 ms |
580 KB |
Output is correct |
8 |
Correct |
74 ms |
856 KB |
Output is correct |
9 |
Correct |
79 ms |
880 KB |
Output is correct |
10 |
Correct |
89 ms |
844 KB |
Output is correct |
11 |
Correct |
118 ms |
940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
852 KB |
Output is correct |
23 |
Correct |
10 ms |
676 KB |
Output is correct |
24 |
Correct |
10 ms |
712 KB |
Output is correct |
25 |
Correct |
11 ms |
700 KB |
Output is correct |
26 |
Correct |
11 ms |
724 KB |
Output is correct |
27 |
Correct |
11 ms |
724 KB |
Output is correct |
28 |
Correct |
26 ms |
5020 KB |
Output is correct |
29 |
Correct |
16 ms |
1364 KB |
Output is correct |
30 |
Correct |
47 ms |
7812 KB |
Output is correct |
31 |
Correct |
57 ms |
8936 KB |
Output is correct |
32 |
Correct |
12 ms |
1120 KB |
Output is correct |
33 |
Correct |
21 ms |
2700 KB |
Output is correct |
34 |
Correct |
3 ms |
340 KB |
Output is correct |
35 |
Correct |
3 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
340 KB |
Output is correct |
37 |
Correct |
4 ms |
340 KB |
Output is correct |
38 |
Correct |
3 ms |
340 KB |
Output is correct |
39 |
Correct |
31 ms |
596 KB |
Output is correct |
40 |
Correct |
32 ms |
580 KB |
Output is correct |
41 |
Correct |
74 ms |
856 KB |
Output is correct |
42 |
Correct |
79 ms |
880 KB |
Output is correct |
43 |
Correct |
89 ms |
844 KB |
Output is correct |
44 |
Correct |
118 ms |
940 KB |
Output is correct |
45 |
Correct |
28 ms |
740 KB |
Output is correct |
46 |
Correct |
30 ms |
692 KB |
Output is correct |
47 |
Correct |
53 ms |
724 KB |
Output is correct |
48 |
Correct |
26 ms |
736 KB |
Output is correct |
49 |
Correct |
30 ms |
712 KB |
Output is correct |
50 |
Correct |
733 ms |
6708 KB |
Output is correct |
51 |
Correct |
685 ms |
7044 KB |
Output is correct |
52 |
Correct |
2288 ms |
14636 KB |
Output is correct |
53 |
Correct |
2258 ms |
14724 KB |
Output is correct |
54 |
Correct |
2045 ms |
11608 KB |
Output is correct |
55 |
Incorrect |
2644 ms |
17836 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |