QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#283249 | #7513. Palindromic Beads | dayux | ML | 3ms | 19312kb | C++14 | 4.1kb | 2023-12-14 10:39:53 | 2023-12-14 10:39:54 |
Judging History
answer
#include <bits/stdc++.h>
const int maxn = 2e5, maxlg = 17;
int n, dfc;
std::array<int, maxn + 1> p, fa, dep, dfn, siz;
std::array<std::vector<int>, maxn + 1> g;
std::array<std::array<int, maxn + 1>, maxlg + 1> min;
void dfs(int x) {
min[0][dfn[x] = ++dfc] = x;
siz[x] = 1;
for (int v : g[x])
if (v != fa[x]) {
dep[v] = dep[fa[v] = x] + 1;
dfs(v);
siz[x] += siz[v];
}
}
void rmq() {
for (int lg = 0; lg < maxlg; ++lg)
for (int i = 1; i + (2 << lg) - 1 <= n; ++i)
min[lg + 1][i] = dep[min[lg][i]] <= dep[min[lg][i + (1 << lg)]] ? min[lg][i] : min[lg][i + (1 << lg)];
}
int query(int l, int r) {
int k = 31 ^ __builtin_clz(r - l + 1);
return dep[min[k][l]] <= dep[min[k][r - (1 << k) + 1]] ? min[k][l] : min[k][r - (1 << k) + 1];
}
int getlca(int x, int y) {
if (x == y)
return x;
else {
if (dfn[x] > dfn[y])
std::swap(x, y);
return fa[query(dfn[x] + 1, dfn[y])];
}
}
int getdis(int x, int y) {
return dep[x] + dep[y] - dep[getlca(x, y)] * 2;
}
namespace segt {
int tot;
std::array<int, maxn * 4> rt;
struct node {
int max, ls, rs;
node() {
max = ls = rs = 0;
}
};
std::vector<node> t(1);
int ls(int x) {
return x * 2;
}
int rs(int x) {
return x * 2 + 1;
}
void ins(int &x, int p, int v, int l = 1, int r = n) {
if (x == 0) {
x = (int)t.size();
t.emplace_back();
}
t[x].max = std::max(t[x].max, v);
if (l < r) {
int mid = (l + r) / 2;
if (p <= mid) {
int ls = t[x].ls;
ins(ls, p, v, l, mid);
t[x].ls = ls;
}
else {
int rs = t[x].rs;
ins(rs, p, v, mid + 1, r);
t[x].rs = rs;
}
}
}
void mdf(int p, int y, int v, int x = 1, int l = 1, int r = n) {
ins(rt[x], y, v);
if (l < r) {
int mid = (l + r) / 2;
if (p <= mid)
mdf(p, y, v, ls(x), l, mid);
else
mdf(p, y, v, rs(x), mid + 1, r);
}
}
int get(int x, int ql, int qr, int l = 1, int r = n) {
if (ql > qr)
return 0;
if (x == 0)
return 0;
if (ql <= l && r <= qr)
return t[x].max;
int mid = (l + r) / 2;
return std::max(ql <= mid ? get(t[x].ls, ql, qr, l, mid) : 0, qr > mid ? get(t[x].rs, ql, qr, mid + 1, r) : 0);
}
int qry(int ql, int qr, int yl, int yr, int x = 1, int l = 1, int r = n) {
if (ql > qr)
return 0;
if (ql <= l && r <= qr)
return get(rt[x], yl, yr);
int mid = (l + r) / 2;
return std::max(ql <= mid ? qry(ql, qr, yl, yr, ls(x), l, mid) : 0, qr > mid ? qry(ql, qr, yl, yr, rs(x), mid + 1, r) : 0);
}
}
int main() {
#ifndef ONLINE_JUDGE
std::freopen("B.in", "r", stdin);
std::freopen("B.out", "w", stdout);
#endif
std::cin.tie(nullptr)->sync_with_stdio(false);
std::cin >> n;
std::vector<std::pair<int, int>> vpr;
for (int i = 1, c; i <= n; ++i) {
std::cin >> c;
if (p[c] == 0)
p[c] = i;
else
vpr.emplace_back(p[c], i);
}
for (int i = 1, u, v; i < n; ++i) {
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
rmq();
std::sort(vpr.begin(), vpr.end(), [&](const std::pair<int, int> &lhs, const std::pair<int, int> &rhs) { return getdis(lhs.first, lhs.second) > getdis(rhs.first, rhs.second); });
int max = 1, f;
for (auto &pr : vpr) {
if (dfn[pr.first] > dfn[pr.second])
std::swap(pr.first, pr.second);
if (dfn[pr.second] <= dfn[pr.first] + siz[pr.first] - 1)
f = std::max(segt::qry(1, dfn[pr.first] - 1, dfn[pr.second], dfn[pr.second] + siz[pr.second] - 1), segt::qry(dfn[pr.first] + siz[pr.first], n, dfn[pr.second], dfn[pr.second] + siz[pr.second] - 1)) + 2;
else
f = segt::qry(dfn[pr.first], dfn[pr.first] + siz[pr.first] - 1, dfn[pr.second], dfn[pr.second] + siz[pr.second] - 1) + 2;
segt::mdf(dfn[pr.first], dfn[pr.second], f);
segt::mdf(dfn[pr.second], dfn[pr.first], f);
max = std::max(max, f + (getdis(pr.first, pr.second) > 1));
}
std::cout << max << std::endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 16172kb
input:
4 1 1 2 2 1 2 2 3 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 16140kb
input:
5 1 3 2 2 1 1 2 2 3 3 4 4 5
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 15964kb
input:
6 1 1 2 2 3 3 1 2 2 3 3 4 4 5 5 6
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 3ms
memory: 16168kb
input:
6 1 2 3 4 5 6 1 2 2 3 3 4 4 5 5 6
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 0ms
memory: 19312kb
input:
2000 845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...
output:
5
result:
ok single line: '5'
Test #6:
score: -100
Memory Limit Exceeded
input:
200000 48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...
output:
5