Submission #203567

# Submission time Handle Problem Language Result Execution time Memory
203567 2020-02-21T10:38:32 Z EntityIT Wild Boar (JOI18_wild_boar) C++14
100 / 100
11598 ms 558660 KB
#include<bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;

template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }

const LL infLL = 1e18;
mt19937 rng( (int)chrono::steady_clock::now().time_since_epoch().count() );

int n, nDay, nSta;

struct Edge {
  int from, to, w;
  Edge(int _from, int _to, int _w) : from(_from), to(_to), w(_w) {}
};
vector<Edge> edge;
vector<vector<int> > gr;

vector<vector<LL> > d;
void dijkstra(int s) {
  d[s][s] = edge[s].w;
  priority_queue<pair<LL, int> > pq;
  pq.emplace(-d[s][s], s);
  while (sz(pq) ) {
    LL curD = -pq.top().first; int u = pq.top().second; pq.pop();

    if (curD ^ d[s][u]) continue ;

    for (int v : gr[edge[u].to]) if ( (u >> 1) ^ (v >> 1) ) {
      if (asMn(d[s][v], curD + edge[v].w) ) pq.emplace(-d[s][v], v);
    }
  }
}

void optimize(vector<tuple<LL, int, int> > &a) {
  vector<tuple<LL, int, int> > nA;
  if (sz(a) ) nA.emplace_back(*min_element(all(a) ) );

  tuple<LL, int, int> mn{ infLL, -1, -1 };
  for (auto &i : a) if (get<1>(i) ^ get<1>(nA[0]) && get<2>(i) ^ get<2>(nA[0])) asMn(mn, i);
  nA.emplace_back(mn);

  mn = { infLL, -1, -1 };
  for (auto &i : a) if (get<1>(i) ^ get<1>(nA[0]) && get<2>(i) ^ get<2>(nA[1])) asMn(mn, i);
  nA.emplace_back(mn);

  mn = { infLL, -1, -1 };
  for (auto &i : a) if (get<1>(i) ^ get<1>(nA[1]) && get<2>(i) ^ get<2>(nA[0])) asMn(mn, i);
  nA.emplace_back(mn);

  auto tmp = find(all(nA), tuple<LL, int, int>{ infLL, -1, -1 });
  while (tmp != nA.end() ) {
    nA.erase(tmp);
    tmp = find(all(nA), tuple<LL, int, int>{ infLL, -1, -1 });
  }

  a.swap(nA);
}
vector<vector<vector<tuple<LL, int, int> > > > optimalE;
vector<int> x;
struct It {
  #define lC (i << 1)
  #define rC (i << 1 | 1)
  #define Mid ( (Left + Right) >> 1)
  #define rt int i = 1, int Left = 0, int Right = nSta - 2

  struct Node {
    vector<tuple<LL, int, int> > val;
    Node() { val.clear(); }

    Node operator + (const Node &_) const {
      Node ret;
      for (auto i : val) {
        for (auto j : _.val) if ( (get<2>(i) >> 1) ^ (get<1>(j) >> 1) ) {
          ret.val.emplace_back(get<0>(i) + get<0>(j), get<1>(i), get<2>(j) );
        }
      }
      optimize(ret.val);

      return ret;
    }
  };
  vector<Node> node;
  It(int nNode) { node.assign(nNode, Node() ); }

  void build(rt) {
    if (!(Left ^ Right) ) {
      node[i].val = optimalE[ x[Left] ][ x[Left + 1] ];
      return ;
    }
    build(lC, Left, Mid);
    build(rC, Mid + 1, Right);
    node[i] = node[lC] + node[rC];
  }

  void upd(int pos, rt) {
    if (Left == Right) {
      node[i].val = optimalE[ x[Left] ][ x[Left + 1] ];
      return ;
    }
    if (pos <= Mid) upd(pos, lC, Left, Mid);
    else upd(pos, rC, Mid + 1, Right);
    node[i] = node[lC] + node[rC];
  }
};

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  #ifdef FourLeafClover
  freopen("input", "r", stdin);
  #endif // FourLeafClover

  int m; cin >> n >> m >> nDay >> nSta;

  gr.assign(n, {} );
  while (m--) {
    int u, v, w; cin >> u >> v >> w; --u; --v;
    gr[u].emplace_back(sz(edge) ); edge.emplace_back(u, v, w);
    gr[v].emplace_back(sz(edge) ); edge.emplace_back(v, u, w);
  }

  d.assign(sz(edge), vector<LL>(sz(edge), infLL) );
  for (int s = 0; s < sz(edge); ++s) dijkstra(s);

  optimalE.assign(n, vector<vector<tuple<LL, int, int> > >(n) );
  for (int from = 0; from < n; ++from) {
    for (int to = 0; to < n; ++to) if (from ^ to) {
      for (int s : gr[from]) {
        for (int t : gr[to]) optimalE[from][to].emplace_back(d[s][t ^ 1], s, t ^ 1);
      }
      optimize(optimalE[from][to]);
    }
  }

  x.assign(nSta, 0);
  for (auto &i : x) cin >> i, --i;
  It it( (nSta + 5) << 2); it.build();

  while (nDay--) {
    int p, v; cin >> p >> v; --p; --v;
    x[p] = v;
    if (p) it.upd(p - 1);
    if (p + 1 < nSta) it.upd(p);

    tuple<LL, int, int> ans{ infLL, -1, -1 };
    for (auto _ : it.node[1].val) asMn(ans, _);
    cout << (get<0>(ans) ^ infLL ? get<0>(ans) : -1) << '\n';
  }

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 6 ms 504 KB Output is correct
27 Correct 82 ms 23288 KB Output is correct
28 Correct 80 ms 23032 KB Output is correct
29 Correct 199 ms 29944 KB Output is correct
30 Correct 201 ms 29944 KB Output is correct
31 Correct 212 ms 29828 KB Output is correct
32 Correct 212 ms 29864 KB Output is correct
33 Correct 211 ms 31352 KB Output is correct
34 Correct 208 ms 31224 KB Output is correct
35 Correct 199 ms 31116 KB Output is correct
36 Correct 204 ms 31096 KB Output is correct
37 Correct 205 ms 31224 KB Output is correct
38 Correct 208 ms 33136 KB Output is correct
39 Correct 206 ms 33016 KB Output is correct
40 Correct 211 ms 33016 KB Output is correct
41 Correct 214 ms 33016 KB Output is correct
42 Correct 190 ms 34168 KB Output is correct
43 Correct 200 ms 35320 KB Output is correct
44 Correct 208 ms 35368 KB Output is correct
45 Correct 145 ms 35032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 6 ms 504 KB Output is correct
27 Correct 82 ms 23288 KB Output is correct
28 Correct 80 ms 23032 KB Output is correct
29 Correct 199 ms 29944 KB Output is correct
30 Correct 201 ms 29944 KB Output is correct
31 Correct 212 ms 29828 KB Output is correct
32 Correct 212 ms 29864 KB Output is correct
33 Correct 211 ms 31352 KB Output is correct
34 Correct 208 ms 31224 KB Output is correct
35 Correct 199 ms 31116 KB Output is correct
36 Correct 204 ms 31096 KB Output is correct
37 Correct 205 ms 31224 KB Output is correct
38 Correct 208 ms 33136 KB Output is correct
39 Correct 206 ms 33016 KB Output is correct
40 Correct 211 ms 33016 KB Output is correct
41 Correct 214 ms 33016 KB Output is correct
42 Correct 190 ms 34168 KB Output is correct
43 Correct 200 ms 35320 KB Output is correct
44 Correct 208 ms 35368 KB Output is correct
45 Correct 145 ms 35032 KB Output is correct
46 Correct 226 ms 12280 KB Output is correct
47 Correct 8378 ms 176948 KB Output is correct
48 Correct 7453 ms 216844 KB Output is correct
49 Correct 5783 ms 253420 KB Output is correct
50 Correct 6277 ms 253224 KB Output is correct
51 Correct 7453 ms 253452 KB Output is correct
52 Correct 4001 ms 253560 KB Output is correct
53 Correct 3942 ms 253440 KB Output is correct
54 Correct 3964 ms 253480 KB Output is correct
55 Correct 3889 ms 253432 KB Output is correct
56 Correct 4024 ms 275076 KB Output is correct
57 Correct 4006 ms 298256 KB Output is correct
58 Correct 3972 ms 323840 KB Output is correct
59 Correct 3975 ms 351120 KB Output is correct
60 Correct 4008 ms 380660 KB Output is correct
61 Correct 3951 ms 412412 KB Output is correct
62 Correct 3851 ms 445944 KB Output is correct
63 Correct 3725 ms 481488 KB Output is correct
64 Correct 2291 ms 555816 KB Output is correct
65 Correct 2299 ms 555768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 6 ms 504 KB Output is correct
27 Correct 82 ms 23288 KB Output is correct
28 Correct 80 ms 23032 KB Output is correct
29 Correct 199 ms 29944 KB Output is correct
30 Correct 201 ms 29944 KB Output is correct
31 Correct 212 ms 29828 KB Output is correct
32 Correct 212 ms 29864 KB Output is correct
33 Correct 211 ms 31352 KB Output is correct
34 Correct 208 ms 31224 KB Output is correct
35 Correct 199 ms 31116 KB Output is correct
36 Correct 204 ms 31096 KB Output is correct
37 Correct 205 ms 31224 KB Output is correct
38 Correct 208 ms 33136 KB Output is correct
39 Correct 206 ms 33016 KB Output is correct
40 Correct 211 ms 33016 KB Output is correct
41 Correct 214 ms 33016 KB Output is correct
42 Correct 190 ms 34168 KB Output is correct
43 Correct 200 ms 35320 KB Output is correct
44 Correct 208 ms 35368 KB Output is correct
45 Correct 145 ms 35032 KB Output is correct
46 Correct 226 ms 12280 KB Output is correct
47 Correct 8378 ms 176948 KB Output is correct
48 Correct 7453 ms 216844 KB Output is correct
49 Correct 5783 ms 253420 KB Output is correct
50 Correct 6277 ms 253224 KB Output is correct
51 Correct 7453 ms 253452 KB Output is correct
52 Correct 4001 ms 253560 KB Output is correct
53 Correct 3942 ms 253440 KB Output is correct
54 Correct 3964 ms 253480 KB Output is correct
55 Correct 3889 ms 253432 KB Output is correct
56 Correct 4024 ms 275076 KB Output is correct
57 Correct 4006 ms 298256 KB Output is correct
58 Correct 3972 ms 323840 KB Output is correct
59 Correct 3975 ms 351120 KB Output is correct
60 Correct 4008 ms 380660 KB Output is correct
61 Correct 3951 ms 412412 KB Output is correct
62 Correct 3851 ms 445944 KB Output is correct
63 Correct 3725 ms 481488 KB Output is correct
64 Correct 2291 ms 555816 KB Output is correct
65 Correct 2299 ms 555768 KB Output is correct
66 Correct 136 ms 26104 KB Output is correct
67 Correct 962 ms 134704 KB Output is correct
68 Correct 2481 ms 533052 KB Output is correct
69 Correct 2835 ms 534332 KB Output is correct
70 Correct 3417 ms 556020 KB Output is correct
71 Correct 11081 ms 179544 KB Output is correct
72 Correct 10270 ms 219132 KB Output is correct
73 Correct 6465 ms 256504 KB Output is correct
74 Correct 6500 ms 256208 KB Output is correct
75 Correct 6509 ms 256236 KB Output is correct
76 Correct 8424 ms 255736 KB Output is correct
77 Correct 9860 ms 255696 KB Output is correct
78 Correct 11598 ms 255636 KB Output is correct
79 Correct 6263 ms 301068 KB Output is correct
80 Correct 6447 ms 326492 KB Output is correct
81 Correct 7747 ms 353276 KB Output is correct
82 Correct 6373 ms 383536 KB Output is correct
83 Correct 6977 ms 414508 KB Output is correct
84 Correct 5825 ms 484264 KB Output is correct
85 Correct 3876 ms 558660 KB Output is correct