Submission #39982

# Submission time Handle Problem Language Result Execution time Memory
39982 2018-01-25T05:35:23 Z funcsr Factories (JOI14_factories) C++14
33 / 100
6000 ms 143484 KB
#include "factories.h"
#include <iostream>
#include <queue>
#include <cassert>
#include <set>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
typedef pair<int, int> P;
typedef pair<long long, int> P2;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define index(x, y) (int)(lower_bound(all(x), y) - x.begin())
#define pb push_back
#define _1 first
#define _2 second
#define INF (1LL<<60)

int N;
vector<P> G[500000];
int U[19][500000];
int R[500000];
long long W[500000];
int IN[500000], OUT[500000];
vector<int> T;
void dfs(int x, int p, int r, long long w) {
  IN[x] = T.size();
  T.pb(x);
  R[x] = r, W[x] = w;
  U[0][x] = p;
  for (P pp : G[x]) if (pp._1 != p) dfs(pp._1, x, r+1, w+pp._2);
  OUT[x] = (int)T.size()-1;
}

int lca(int x, int y) {
  if (R[x] < R[y]) swap(x, y);
  for (int k=18; k>=0; k--) if ((R[x]-R[y])>>k) x = U[k][x];
  if (x == y) return x;
  for (int k=18; k>=0; k--) if (U[k][x] != U[k][y]) x = U[k][x], y = U[k][y];
  return U[0][x];
}
inline long long dist(int x, int y) {
  int g = lca(x, y);
  return W[x]+W[y]-2LL*W[g];
}

void Init(int NN, int A[], int B[], int D[]) {
  N = NN;
  rep(i, N-1) {
    G[A[i]].pb(P(B[i], D[i]));
    G[B[i]].pb(P(A[i], D[i]));
  }
  dfs(0, -1, 0, 0);
  rep(k, 18) rep(x, N) {
    if (U[k][x] == -1) U[k+1][x] = -1;
    U[k+1][x] = U[k][U[k][x]];
  }
}

long long D[500000];
bool isT[500000];
vector<int> G2[500000];

long long Query(int S, int X[], int T, int Y[]) {
  vector<P> ps;
  rep(i, S) ps.pb(P(IN[X[i]], X[i]));
  rep(i, T) ps.pb(P(IN[Y[i]], Y[i]));
  sort(all(ps)); uniq(ps);
  vector<P> new_ps(ps);
  rep(i, (int)ps.size()-1) {
    int v = lca(ps[i]._2, ps[i+1]._2);
    new_ps.pb(P(IN[v], v));
  }
  swap(new_ps, ps);
  sort(all(ps)); uniq(ps);
  stack<int> st;
  for (P p : ps) {
    int v = p._2;
    D[v] = INF;
    while (!st.empty() && !(IN[st.top()] <= IN[v] && IN[v] <= OUT[st.top()])) st.pop();
    if (!st.empty()) {
      int p = st.top();
      G2[p].pb(v);
      G2[v].pb(p);
    }
    st.push(v);
  }

  rep(i, T) isT[Y[i]] = true;
  priority_queue<P2, vector<P2>, greater<P2> > q;
  rep(i, S) q.push(P(0, X[i])), D[X[i]] = 0;
  long long ans = INF;
  while (!q.empty()) {
    long long r = q.top()._1;
    int x = q.top()._2;
    q.pop();
    if (D[x] < r) continue;
    if (isT[x]) {
      ans = r;
      break;
    }
    for (int t : G2[x]) {
      long long len = abs(W[t]-W[x]);
      if (D[t] > r+len) {
        D[t] = r+len;
        q.push(P2(D[t], t));
      }
    }
  }
  for (P p : ps) G2[p._2].clear(), G2[p._2].shrink_to_fit();
  rep(i, T) isT[Y[i]] = false;
  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 40 ms 99004 KB Output is correct
2 Correct 1587 ms 99492 KB Output is correct
3 Correct 1690 ms 99268 KB Output is correct
4 Correct 1664 ms 99828 KB Output is correct
5 Correct 1352 ms 99604 KB Output is correct
6 Correct 1100 ms 99516 KB Output is correct
7 Correct 1654 ms 99268 KB Output is correct
8 Correct 1702 ms 99604 KB Output is correct
9 Correct 1431 ms 99600 KB Output is correct
10 Correct 1105 ms 99516 KB Output is correct
11 Correct 1687 ms 99268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 99004 KB Output is correct
2 Correct 3099 ms 120840 KB Output is correct
3 Correct 3857 ms 123720 KB Output is correct
4 Correct 2118 ms 122900 KB Output is correct
5 Correct 3337 ms 143484 KB Output is correct
6 Correct 4417 ms 123448 KB Output is correct
7 Correct 4334 ms 104036 KB Output is correct
8 Correct 2337 ms 104256 KB Output is correct
9 Correct 3360 ms 105856 KB Output is correct
10 Correct 3991 ms 103948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5595 ms 127764 KB Output is correct
2 Correct 5440 ms 130260 KB Output is correct
3 Correct 5895 ms 129932 KB Output is correct
4 Correct 5590 ms 130632 KB Output is correct
5 Execution timed out 6000 ms 122616 KB Execution timed out
6 Halted 0 ms 0 KB -