# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1030505 |
2024-07-22T06:09:34 Z |
NeroZein |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
217784 KB |
#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int B = 320;
const int LOG = 18;
const int N = 1e5 + 5;
int n, m, q;
int c[N];
int dep[N];
int idd, id[N];
vector<int> g[N];
int spa[LOG][N * 2];
void dfs(int v, int p) {
id[v] = idd;
spa[0][idd++] = v;
for (int u : g[v]) {
if (u == p) {
continue;
}
dep[u] = dep[v] + 1;
dfs(u, v);
spa[0][idd++] = v;
}
}
int get(int l, int r) {
int lg = 31 - __builtin_clz(r - l + 1);
int u = spa[lg][l], v = spa[lg][r - (1 << lg) + 1];
return dep[u] < dep[v] ? u : v;
}
int distance(int u, int v) {
int l = id[u], r = id[v];
if (l > r) {
swap(l, r);
}
int lca = get(l, r);
int ret = dep[u] + dep[v] - 2 * dep[lca];
return ret;
}
int in_id[N];
int cnt[B][N];
long long ans[N];
long long res[B];
int pre[B][N], nxt[B][N];
vector<pair<int, int>> qs[N];
void preprocess() {
for (int ver = 0; ver * B < m; ++ver) {
for (int r = ver * B; r < m; ++r) {
int v = c[r];
cnt[ver][v]++;
in_id[id[v]] = v;
}
vector<int> ord;
for (int i = 0; i < idd; ++i) {
if (in_id[i] != 0) {
ord.push_back(in_id[i]);
in_id[i] = 0;
}
}
//debug(ord);
//cerr << '\n';
for (int i = 1; i < (int) ord.size(); ++i) {
int u = ord[i - 1], v = ord[i];
res[ver] += distance(u, v);
pre[ver][v] = u;
nxt[ver][u] = v;
}
nxt[ver][ord.back()] = ord[0];
pre[ver][ord[0]] = ord.back();
res[ver] += distance(ord[0], ord.back());
}
}
void back(int r) {
for (int ver = 0; ver * B <= r; ++ver) {
int v = c[r];
if (--cnt[ver][v] > 0) {
return;
}
//int old = res[ver];
int cpre = pre[ver][v];
int cnxt = nxt[ver][v];
if (cpre != 0) {
res[ver] -= distance(v, cpre);
}
if (cnxt != 0) {
res[ver] -= distance(v, cnxt);
}
if (cpre != 0 && cnxt != 0) {
nxt[ver][cpre] = cnxt;
pre[ver][cnxt] = cpre;
res[ver] += distance(cpre, cnxt);
}
//debug(v, cpre, cnxt, old, res[ver]);
}
}
int cnt2[N];
vector<int> clean;
int curpre[N], curnxt[N];
void check(int v) {
if (curpre[v] == 0 && curnxt[v] == 0) {
clean.push_back(v);
}
}
void rem(int ver, int cl, long long& ret) {
int v = c[cl];
check(v);
if (cnt2[v] == 0) {
cnt2[v] = cnt[ver][v];
}
if (curpre[v] == 0) {
curpre[v] = pre[ver][v];
}
if (curnxt[v] == 0) {
curnxt[v] = nxt[ver][v];
}
if (--cnt2[v] != 0) {
return;
}
int cpre = curpre[v];
int cnxt = curnxt[v];
if (cpre != 0) {
ret -= distance(v, cpre);
}
if (cnxt != 0) {
ret -= distance(v, cnxt);
}
if (cpre != 0 && cnxt != 0) {
check(cpre);
check(cnxt);
curpre[cnxt] = cpre;
curnxt[cpre] = cnxt;
ret += distance(cpre, cnxt);
}
}
long long qry(int l) {
int ver = l / B;
long long ret = res[ver];
int cl = ver * B;
while (cl < l) {
rem(ver, cl, ret);
cl++;
}
return ret;
}
void cleanup() {
for (int v : clean) {
curpre[v] = curnxt[v] = 0;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> q;
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1);
for (int j = 1; j < LOG; ++j) {
for (int i = 0; i + (1 << j) <= idd; ++i) {
int u = spa[j - 1][i], v = spa[j - 1][i + (1 << (j - 1))];
spa[j][i] = (dep[u] < dep[v] ? u : v);
}
}
for (int i = 0; i < m; ++i) {
cin >> c[i];
}
preprocess();
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l, --r;
qs[r].push_back({l, i});
}
for (int r = m - 1; r >= 0; --r) {
for (auto [ql, qi] : qs[r]) {
ans[qi] = (qry(ql) + 2) / 2;
cleanup();
}
back(r);
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Incorrect |
4 ms |
5724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Incorrect |
4 ms |
5724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
5 ms |
6748 KB |
Output is correct |
4 |
Execution timed out |
5024 ms |
216300 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5464 KB |
Output is correct |
2 |
Incorrect |
1188 ms |
133716 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
8 ms |
5772 KB |
Output is correct |
4 |
Execution timed out |
5092 ms |
217784 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Incorrect |
4 ms |
5724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |