Submission #571124

# Submission time Handle Problem Language Result Execution time Memory
571124 2022-06-01T09:45:27 Z cadmiumsky Abduction 2 (JOI17_abduction2) C++14
44 / 100
5000 ms 18096 KB
#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;
    cout << max({start(x, y, 1, 1), start(x, y, 0, 0)
              , start(x, y, 1, 0), start(x, y, 0, 1)}) << '\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 332 KB Output is correct
3 Correct 1 ms 328 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 1 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 328 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 1 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 4 ms 596 KB Output is correct
20 Correct 5 ms 596 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 5 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 332 KB Output is correct
3 Correct 1 ms 328 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 1 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 4 ms 596 KB Output is correct
20 Correct 5 ms 596 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 5 ms 852 KB Output is correct
23 Correct 11 ms 936 KB Output is correct
24 Correct 14 ms 936 KB Output is correct
25 Correct 13 ms 888 KB Output is correct
26 Correct 14 ms 724 KB Output is correct
27 Correct 11 ms 852 KB Output is correct
28 Correct 42 ms 5092 KB Output is correct
29 Correct 18 ms 1492 KB Output is correct
30 Correct 78 ms 7864 KB Output is correct
31 Correct 109 ms 9088 KB Output is correct
32 Correct 20 ms 1212 KB Output is correct
33 Correct 30 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 352 KB Output is correct
2 Correct 5 ms 340 KB Output is correct
3 Correct 5 ms 392 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 5 ms 340 KB Output is correct
6 Correct 64 ms 596 KB Output is correct
7 Correct 69 ms 608 KB Output is correct
8 Correct 143 ms 856 KB Output is correct
9 Correct 171 ms 856 KB Output is correct
10 Correct 184 ms 820 KB Output is correct
11 Correct 215 ms 956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 328 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 1 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 4 ms 596 KB Output is correct
20 Correct 5 ms 596 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 5 ms 852 KB Output is correct
23 Correct 11 ms 936 KB Output is correct
24 Correct 14 ms 936 KB Output is correct
25 Correct 13 ms 888 KB Output is correct
26 Correct 14 ms 724 KB Output is correct
27 Correct 11 ms 852 KB Output is correct
28 Correct 42 ms 5092 KB Output is correct
29 Correct 18 ms 1492 KB Output is correct
30 Correct 78 ms 7864 KB Output is correct
31 Correct 109 ms 9088 KB Output is correct
32 Correct 20 ms 1212 KB Output is correct
33 Correct 30 ms 2816 KB Output is correct
34 Correct 4 ms 352 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 5 ms 392 KB Output is correct
37 Correct 5 ms 340 KB Output is correct
38 Correct 5 ms 340 KB Output is correct
39 Correct 64 ms 596 KB Output is correct
40 Correct 69 ms 608 KB Output is correct
41 Correct 143 ms 856 KB Output is correct
42 Correct 171 ms 856 KB Output is correct
43 Correct 184 ms 820 KB Output is correct
44 Correct 215 ms 956 KB Output is correct
45 Correct 35 ms 880 KB Output is correct
46 Correct 50 ms 864 KB Output is correct
47 Correct 53 ms 788 KB Output is correct
48 Correct 49 ms 1056 KB Output is correct
49 Correct 59 ms 940 KB Output is correct
50 Correct 1426 ms 7016 KB Output is correct
51 Correct 1279 ms 7316 KB Output is correct
52 Correct 4453 ms 14960 KB Output is correct
53 Correct 4551 ms 14916 KB Output is correct
54 Correct 4586 ms 11752 KB Output is correct
55 Execution timed out 5097 ms 18096 KB Time limit exceeded
56 Halted 0 ms 0 KB -