# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
51726 |
2018-06-20T10:40:22 Z |
Costin Andrei Oncescu(#1294) |
Factories (JOI14_factories) |
C++11 |
|
3897 ms |
639768 KB |
#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
int nr, rmq[20][1000009], lg[1000009], lev[500009], firstPos[500009], lastPos[500009], color[500009], ap[500009];
long long depth[500009], ans;
const long long INF = 1LL << 59;
vector < pair < int, int > > v[500009];
void dfs (int nod, int tata)
{
rmq[0][++nr] = nod, firstPos[nod] = nr;
for (auto it : v[nod])
if (it.first != tata)
depth[it.first] = depth[nod] + it.second,
lev[it.first] = lev[nod] + 1, dfs (it.first, nod),
rmq[0][++nr] = nod;
lastPos[nod] = nr;
}
int getHigher (int x, int y)
{
if (lev[x] > lev[y]) return y;
return x;
}
int getLCA (int x, int y)
{
x = firstPos[x], y = firstPos[y];
if (x > y) swap (x, y);
int p = lg[y - x + 1];
return getHigher (rmq[p][x], rmq[p][y - (1 << p) + 1]);
}
bool cmp (int x, int y)
{
return (firstPos[x] < firstPos[y]);
}
void Init (int N, int A[], int B[], int D[])
{
for (int i=0; i<N - 1; i++)
{
A[i] ++, B[i] ++;
v[A[i]].push_back ({B[i], D[i]});
v[B[i]].push_back ({A[i], D[i]});
}
dfs (1, -1);
for (int i=2; i<=nr; i++)
{
lg[i] = lg[i - 1];
if ((2 << lg[i]) <= i)
lg[i] ++;
}
for (int i=1; i<=lg[nr]; i++)
for (int j=1; j + (1 << i) - 1<=nr; j++)
rmq[i][j] = getHigher (rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
}
bool isAncestor (int up, int down)
{
return (firstPos[up] <= firstPos[down] && lastPos[down] <= lastPos[up]);
}
vector < int > sons[500009];
pair < long long, long long > solve (int nod)
{
pair < long long, long long > curr = {INF, INF};
if (color[nod] == 1) curr.first = depth[nod];
else
if (color[nod] == 2) curr.second = depth[nod];
for (auto it : sons[nod])
{
auto currIt = solve (it);
if (currIt.first < curr.first) curr.first = currIt.first;
if (currIt.second < curr.second) curr.second = currIt.second;
}
if (curr.first + curr.second - 2LL * depth[nod] < ans)
ans = curr.first + curr.second - 2LL * depth[nod];
return curr;
}
int stk[500009], h[500009];
long long Query (int S, int X[], int T, int Y[])
{
nr = 0, ans = INF;
for (int i=0; i<S; i++)
X[i] ++, h[++nr] = X[i], color[X[i]] = 1, ap[X[i]] = 1;
for (int i=0; i<T; i++)
Y[i] ++, h[++nr] = Y[i], color[Y[i]] = 2, ap[Y[i]] = 1;
sort (h + 1, h + nr + 1, cmp);
for (int i=nr - 1; i>=1; i--)
{
int curr = getLCA (h[i], h[i + 1]);
if (ap[curr] == 0)
ap[curr] = 2, h[++nr] = curr;
}
sort (h + 1, h + nr + 1, cmp);
int l = 0, root = 0;
for (int i=1; i<=nr; i++)
{
while (l > 0 && isAncestor (stk[l], h[i]) == 0) l --;
if (l == 0) root = h[i];
else sons[stk[l]].push_back (h[i]);
stk[++l] = h[i];
}
solve (root);
for (int i=1; i<=nr; i++)
ap[h[i]] = 0, color[h[i]] = 0,
sons[h[i]].clear ();
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
24440 KB |
Output is correct |
2 |
Correct |
956 ms |
42808 KB |
Output is correct |
3 |
Correct |
873 ms |
52412 KB |
Output is correct |
4 |
Correct |
942 ms |
61952 KB |
Output is correct |
5 |
Correct |
844 ms |
71512 KB |
Output is correct |
6 |
Correct |
792 ms |
80868 KB |
Output is correct |
7 |
Correct |
939 ms |
90320 KB |
Output is correct |
8 |
Correct |
985 ms |
99780 KB |
Output is correct |
9 |
Correct |
824 ms |
109344 KB |
Output is correct |
10 |
Correct |
744 ms |
118468 KB |
Output is correct |
11 |
Correct |
886 ms |
128264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
128264 KB |
Output is correct |
2 |
Correct |
1749 ms |
261068 KB |
Output is correct |
3 |
Correct |
2163 ms |
280796 KB |
Output is correct |
4 |
Correct |
1347 ms |
297888 KB |
Output is correct |
5 |
Correct |
1951 ms |
345768 KB |
Output is correct |
6 |
Correct |
2190 ms |
345768 KB |
Output is correct |
7 |
Correct |
1500 ms |
345768 KB |
Output is correct |
8 |
Correct |
959 ms |
345768 KB |
Output is correct |
9 |
Correct |
1095 ms |
345768 KB |
Output is correct |
10 |
Correct |
1648 ms |
345768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
24440 KB |
Output is correct |
2 |
Correct |
956 ms |
42808 KB |
Output is correct |
3 |
Correct |
873 ms |
52412 KB |
Output is correct |
4 |
Correct |
942 ms |
61952 KB |
Output is correct |
5 |
Correct |
844 ms |
71512 KB |
Output is correct |
6 |
Correct |
792 ms |
80868 KB |
Output is correct |
7 |
Correct |
939 ms |
90320 KB |
Output is correct |
8 |
Correct |
985 ms |
99780 KB |
Output is correct |
9 |
Correct |
824 ms |
109344 KB |
Output is correct |
10 |
Correct |
744 ms |
118468 KB |
Output is correct |
11 |
Correct |
886 ms |
128264 KB |
Output is correct |
12 |
Correct |
24 ms |
128264 KB |
Output is correct |
13 |
Correct |
1749 ms |
261068 KB |
Output is correct |
14 |
Correct |
2163 ms |
280796 KB |
Output is correct |
15 |
Correct |
1347 ms |
297888 KB |
Output is correct |
16 |
Correct |
1951 ms |
345768 KB |
Output is correct |
17 |
Correct |
2190 ms |
345768 KB |
Output is correct |
18 |
Correct |
1500 ms |
345768 KB |
Output is correct |
19 |
Correct |
959 ms |
345768 KB |
Output is correct |
20 |
Correct |
1095 ms |
345768 KB |
Output is correct |
21 |
Correct |
1648 ms |
345768 KB |
Output is correct |
22 |
Correct |
3183 ms |
422184 KB |
Output is correct |
23 |
Correct |
3086 ms |
448188 KB |
Output is correct |
24 |
Correct |
3777 ms |
474800 KB |
Output is correct |
25 |
Correct |
3653 ms |
502412 KB |
Output is correct |
26 |
Correct |
3786 ms |
518528 KB |
Output is correct |
27 |
Correct |
3456 ms |
570056 KB |
Output is correct |
28 |
Correct |
2584 ms |
570056 KB |
Output is correct |
29 |
Correct |
3621 ms |
591260 KB |
Output is correct |
30 |
Correct |
3897 ms |
615072 KB |
Output is correct |
31 |
Correct |
3711 ms |
639768 KB |
Output is correct |
32 |
Correct |
1810 ms |
639768 KB |
Output is correct |
33 |
Correct |
1445 ms |
639768 KB |
Output is correct |
34 |
Correct |
1781 ms |
639768 KB |
Output is correct |
35 |
Correct |
1825 ms |
639768 KB |
Output is correct |
36 |
Correct |
1899 ms |
639768 KB |
Output is correct |
37 |
Correct |
1833 ms |
639768 KB |
Output is correct |