Submission #861929

# Submission time Handle Problem Language Result Execution time Memory
861929 2023-10-17T07:46:52 Z mgl_diamond Factories (JOI14_factories) C++17
100 / 100
1385 ms 203716 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
 
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define file "input"
 
void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
 
  if (fopen(file".inp", "r")) {
    freopen(file".inp", "r", stdin);
    freopen(file".out", "w", stdout);
  }
}
 
const ll BIG = 1e18;
const int N = 5e5+5, LOG = 20;
int n, q;
vector<ii> graph[N];
 
int cnt, anc[N*2][LOG], st[N], en[N], high[N], lg[N*2];
ll distb[N], distr[N], curpar[N], wei[N];
vector<int> node;
int red[N], blue[N];
 
void dfs_prep(int u, int p) {
  anc[++cnt][0] = u;
  st[u] = cnt;
  fore(x, graph[u]) if (x.first != p) {
    wei[x.first] = wei[u] + x.second;
    high[x.first] = high[u] + 1;
    dfs_prep(x.first, u);
    anc[++cnt][0] = u;
  }
  en[u] = cnt;
}
 
int comb(int i, int j) {
  return high[i] < high[j] ? i : j;
}

int lca(int u, int v) {
  u = st[u]; v = st[v];
  if (u > v) swap(u, v);
  int k = lg[v-u+1];
  return comb(anc[u][k], anc[v-(1<<k)+1][k]);
}
 
void Init(int N, int A[], int B[], int D[]) {
  n = N;
  foru(i, 0, n-2) {
    graph[A[i]+1].emplace_back(B[i]+1, D[i]);
    graph[B[i]+1].emplace_back(A[i]+1, D[i]);
  }

  dfs_prep(1, 0);
  foru(i, 2, cnt) lg[i] = lg[i >> 1] + 1;
  foru(i, 1, LOG-1) foru(j, 1, cnt-(1<<i)+1) anc[j][i] = comb(anc[j][i-1], anc[j+(1<<(i-1))][i-1]);
}
 
ll Query(int S, int X[], int T, int Y[]) {
  ++q;

  foru(i, 0, S-1) {
    int v = X[i]+1;
    node.push_back(v);
    red[v] = q;
  }
 
  foru(i, 0, T-1) {
    int v = Y[i]+1;
    node.push_back(v);
    blue[v] = q;
  }
 
  sort(all(node), [&](int x, int y){ return st[x] < st[y]; });
  int m = sz(node)-2;
  foru(i, 0, m) node.push_back(lca(node[i], node[i+1]));
  sort(all(node), [&](int x, int y){ return st[x] < st[y]; });
  node.resize(unique(all(node)) - node.begin());
 
  stack<int> tmp;
  fore(u, node) {
    distb[u] = distr[u] = BIG;
    while (!tmp.empty()) {
      if (en[tmp.top()] < st[u]) tmp.pop();
      else {
        curpar[u] = tmp.top();
        break;
      }
    }
    tmp.push(u);
  }
 
  ll ans = BIG;
  ford(i, sz(node)-1, 0) {
    int u = node[i];
    if (!(blue[u] ^ q)) { distb[u] = 0; }
    if (!(red[u] ^ q)) { distr[u] = 0; }
    if (ans > distb[u] + distr[u]) { ans = distb[u] + distr[u]; }
 
    if (curpar[u] > 0) {
      distb[curpar[u]] = min(distb[curpar[u]], distb[u] + wei[u] - wei[curpar[u]]);
      distr[curpar[u]] = min(distr[curpar[u]], distr[u] + wei[u] - wei[curpar[u]]);
    }
  }
  node.clear();
 
  return ans;
}

Compilation message

factories.cpp: In function 'void setIO()':
factories.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     freopen(file".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
factories.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen(file".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 45660 KB Output is correct
2 Correct 481 ms 59468 KB Output is correct
3 Correct 496 ms 59500 KB Output is correct
4 Correct 498 ms 59532 KB Output is correct
5 Correct 453 ms 59732 KB Output is correct
6 Correct 323 ms 59496 KB Output is correct
7 Correct 444 ms 59460 KB Output is correct
8 Correct 479 ms 59728 KB Output is correct
9 Correct 443 ms 59612 KB Output is correct
10 Correct 321 ms 59476 KB Output is correct
11 Correct 444 ms 59476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 45656 KB Output is correct
2 Correct 751 ms 178172 KB Output is correct
3 Correct 776 ms 180092 KB Output is correct
4 Correct 595 ms 178360 KB Output is correct
5 Correct 825 ms 202324 KB Output is correct
6 Correct 853 ms 181332 KB Output is correct
7 Correct 509 ms 90720 KB Output is correct
8 Correct 384 ms 90504 KB Output is correct
9 Correct 425 ms 93608 KB Output is correct
10 Correct 502 ms 91216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 45660 KB Output is correct
2 Correct 481 ms 59468 KB Output is correct
3 Correct 496 ms 59500 KB Output is correct
4 Correct 498 ms 59532 KB Output is correct
5 Correct 453 ms 59732 KB Output is correct
6 Correct 323 ms 59496 KB Output is correct
7 Correct 444 ms 59460 KB Output is correct
8 Correct 479 ms 59728 KB Output is correct
9 Correct 443 ms 59612 KB Output is correct
10 Correct 321 ms 59476 KB Output is correct
11 Correct 444 ms 59476 KB Output is correct
12 Correct 7 ms 45656 KB Output is correct
13 Correct 751 ms 178172 KB Output is correct
14 Correct 776 ms 180092 KB Output is correct
15 Correct 595 ms 178360 KB Output is correct
16 Correct 825 ms 202324 KB Output is correct
17 Correct 853 ms 181332 KB Output is correct
18 Correct 509 ms 90720 KB Output is correct
19 Correct 384 ms 90504 KB Output is correct
20 Correct 425 ms 93608 KB Output is correct
21 Correct 502 ms 91216 KB Output is correct
22 Correct 1385 ms 184016 KB Output is correct
23 Correct 1216 ms 185704 KB Output is correct
24 Correct 1324 ms 185708 KB Output is correct
25 Correct 1359 ms 188408 KB Output is correct
26 Correct 1203 ms 185680 KB Output is correct
27 Correct 1377 ms 203716 KB Output is correct
28 Correct 904 ms 186488 KB Output is correct
29 Correct 1192 ms 184976 KB Output is correct
30 Correct 1140 ms 184588 KB Output is correct
31 Correct 1168 ms 186936 KB Output is correct
32 Correct 694 ms 94924 KB Output is correct
33 Correct 485 ms 91336 KB Output is correct
34 Correct 673 ms 89236 KB Output is correct
35 Correct 621 ms 89240 KB Output is correct
36 Correct 598 ms 89684 KB Output is correct
37 Correct 586 ms 89712 KB Output is correct