Submission #784831

# Submission time Handle Problem Language Result Execution time Memory
784831 2023-07-16T15:04:10 Z cadmiumsky Ancient Books (IOI17_books) C++17
22 / 100
2000 ms 709312 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 1e6 + 5, inf = 1e18 + 5;

int n;

namespace G {
  vector<vector<pii>> g;
  void init(int dim) { g.resize(dim, vector<pii>()); }
  int newnode() { g.emplace_back(); return sz(g) - 1; }
  void addedge(int x, int y, int z) {
    g[x].emplace_back(y, z);
  }
  
  namespace AINT {
    int aint[(nmax + nmax / 2) * 2];
    void init(int node = 1, int cl = 0, int cr = n - 1) {
      if(cl == cr) {
        aint[node] = cl;
        return;
      }
      int mid = cl + cr >> 1;
      init(2 * node, cl, mid);
      init(2 * node + 1, mid + 1, cr);
      aint[node] = newnode();
      addedge(aint[node], aint[2 * node], 0);
      addedge(aint[node], aint[2 * node + 1], 0);
      return;
    }
    void pushto(int val, int l, int r, int node = 1, int cl = 0, int cr = n - 1) {
      if(r < cl || cr < l) return;;
      if(l <= cl && cr <= r) {
        addedge(val, aint[node], 0);
        return;
      }
      int mid = cl + cr >> 1;
      pushto(val, l, r, 2 * node, cl, mid);
      pushto(val, l, r, 2 * node + 1, mid + 1, cr);
    }
  }
  
  vector<int> arriv, occ;
  vector<int> heap[nmax];
  vector<int> dijkstra(int s = -1) {
    arriv.resize(sz(g));
    occ.resize(sz(g));
    for(int i = 0; i <= n + 2; i++) heap[i].clear();
    //cerr << sz(arriv) << ' ' << sz(g) << '\n';
    fill(all(occ), 0);
    if(s != -1) {
      fill(all(arriv), inf);
      heap[0].emplace_back(s);
      arriv[s] = 0;
    }
    else {
      for(int i = 0; i < sz(g); i++)
        arriv[i] = min(arriv[i], n + 2),
        heap[arriv[i]].emplace_back(i);
    }
    for(int i = 0; i < n + 2; i++) {
      int c = i;
      for(int j = 0; j < sz(heap[i]); j++) {
        auto node = heap[i][j];
        if(occ[node]) continue;
        occ[node] = 1;
        for(auto [x, e] : g[node]) {
          if(arriv[x] > c + e) {
            arriv[x] = c + e;
            heap[arriv[x]].emplace_back(x);
          }
        }
      }
    }
    return arriv;
  }
}

long long minimum_walk(std::vector<signed> p, signed s) {
  n = sz(p);
  vector<int> col(n, 0);
  vector<int> rs(n + 2, 0), ls(n + 2, 0);
  int flag = 0;
  
  multiset<int> lhf;
  
  ll sum = 0;
  
  G::init(n);
  G::AINT::init();
  
  for(int i = 0; i < n; i++) {
    if(col[i] == 0) {
      flag++;
      int x = i, cnt = 0;
      set<int> vals;
      while(col[x] != flag) {
        col[x] = flag;
        sum += abs(x - p[x]);
        x = p[x];
        cnt++;
        vals.emplace(x);
      }
      int L = *vals.begin();
      int R = *vals.rbegin();
      flag++;
      x = i;
      while(col[x] != flag) {
        col[x] = flag;
        G::AINT::pushto(x, L, R);
        x = p[x];
      }
    }
    if(i > 0)
      G::addedge(i - 1, i, 1),
      G::addedge(i, i - 1, 1);
  }
  
  int sl = 0, sr = n - 1;
  while(p[sl] == sl && sl < s) sl++;
  while(p[sr] == sr && sr > s) sr--;
  
  auto fl = G::dijkstra(sl), fr = G::dijkstra(sr);
  for(int i = 0; i < sz(G::g); i++) {
    G::arriv[i] = fl[i] + fr[i];
  }
  auto rez = G::dijkstra();
  
  return sum + rez[s] * 2;
}
#undef int

/**
      Anul asta se da centroid.
-- Surse oficiale
*/

Compilation message

books.cpp: In function 'void G::AINT::init(ll, ll, ll)':
books.cpp:33:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   33 |       int mid = cl + cr >> 1;
      |                 ~~~^~~~
books.cpp: In function 'void G::AINT::pushto(ll, ll, ll, ll, ll, ll)':
books.cpp:47:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |       int mid = cl + cr >> 1;
      |                 ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 16 ms 23764 KB Output is correct
3 Correct 15 ms 23764 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 13 ms 23752 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 14 ms 23788 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 13 ms 23772 KB Output is correct
12 Correct 13 ms 23772 KB Output is correct
13 Correct 12 ms 23740 KB Output is correct
14 Correct 13 ms 23724 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23748 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 13 ms 23788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 16 ms 23764 KB Output is correct
3 Correct 15 ms 23764 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 13 ms 23752 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 14 ms 23788 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 13 ms 23772 KB Output is correct
12 Correct 13 ms 23772 KB Output is correct
13 Correct 12 ms 23740 KB Output is correct
14 Correct 13 ms 23724 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23748 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 13 ms 23788 KB Output is correct
19 Correct 13 ms 24344 KB Output is correct
20 Correct 14 ms 24292 KB Output is correct
21 Correct 14 ms 24096 KB Output is correct
22 Correct 14 ms 24164 KB Output is correct
23 Correct 13 ms 24148 KB Output is correct
24 Correct 13 ms 24156 KB Output is correct
25 Correct 14 ms 24060 KB Output is correct
26 Correct 13 ms 24148 KB Output is correct
27 Correct 16 ms 24148 KB Output is correct
28 Correct 14 ms 24148 KB Output is correct
29 Correct 13 ms 24112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 16 ms 23764 KB Output is correct
3 Correct 15 ms 23764 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 13 ms 23752 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 14 ms 23788 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 13 ms 23772 KB Output is correct
12 Correct 13 ms 23772 KB Output is correct
13 Correct 12 ms 23740 KB Output is correct
14 Correct 13 ms 23724 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23748 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 13 ms 23788 KB Output is correct
19 Correct 13 ms 24344 KB Output is correct
20 Correct 14 ms 24292 KB Output is correct
21 Correct 14 ms 24096 KB Output is correct
22 Correct 14 ms 24164 KB Output is correct
23 Correct 13 ms 24148 KB Output is correct
24 Correct 13 ms 24156 KB Output is correct
25 Correct 14 ms 24060 KB Output is correct
26 Correct 13 ms 24148 KB Output is correct
27 Correct 16 ms 24148 KB Output is correct
28 Correct 14 ms 24148 KB Output is correct
29 Correct 13 ms 24112 KB Output is correct
30 Execution timed out 2122 ms 709312 KB Time limit exceeded
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 24020 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 16 ms 23764 KB Output is correct
3 Correct 15 ms 23764 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 13 ms 23752 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 14 ms 23788 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 13 ms 23772 KB Output is correct
12 Correct 13 ms 23772 KB Output is correct
13 Correct 12 ms 23740 KB Output is correct
14 Correct 13 ms 23724 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23748 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 13 ms 23788 KB Output is correct
19 Correct 13 ms 24344 KB Output is correct
20 Correct 14 ms 24292 KB Output is correct
21 Correct 14 ms 24096 KB Output is correct
22 Correct 14 ms 24164 KB Output is correct
23 Correct 13 ms 24148 KB Output is correct
24 Correct 13 ms 24156 KB Output is correct
25 Correct 14 ms 24060 KB Output is correct
26 Correct 13 ms 24148 KB Output is correct
27 Correct 16 ms 24148 KB Output is correct
28 Correct 14 ms 24148 KB Output is correct
29 Correct 13 ms 24112 KB Output is correct
30 Execution timed out 2122 ms 709312 KB Time limit exceeded
31 Halted 0 ms 0 KB -