QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#204171 | #7513. Palindromic Beads | ucup-team228 | WA | 228ms | 282212kb | C++20 | 5.9kb | 2023-10-07 04:50:37 | 2023-10-07 04:50:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
struct small_tree {
int n;
vector<T> t;
void init(int _n) {
n = _n + 5;
t.assign(2 * n + 10, 0);
}
void update(int pos, T val) {
for (t[pos += n] = val; pos > 1; pos >>= 1) {
t[pos >> 1] = max(t[pos], t[pos ^ 1]);
}
}
T get(int l, int r) {
T res = 0;
for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, t[l++]);
if (!(r & 1)) res = max(res, t[r--]);
}
return res;
}
};
vector<pair<int, int>> merge(const vector<pair<int, int>>& a, const vector<pair<int, int>>& b) {
int n = a.size(), m = b.size();
int i = 0, j = 0;
vector<pair<int, int>> res;
while (i < n && j < m) {
if (a[i].first < b[j].first) {
res.push_back(a[i++]);
} else {
res.push_back(b[j++]);
}
}
while (i < n) {
res.push_back(a[i++]);
}
while (j < m) {
res.push_back(b[j++]);
}
return res;
}
template <typename T>
struct Node {
small_tree<T> tree;
int tl, tr;
vector<pair<int, int>> all;
vector<int> pos;
Node(int x = 0, int y = 0) {
tl = x;
tr = x;
all = {{y, x}};
pos = {0};
tree.init(1);
}
friend Node operator+(const Node& a, const Node& b) {
Node res;
res.tl = a.tl;
res.tr = b.tr;
res.all = merge(a.all, b.all);
res.pos.resize(res.all.size());
for (int i = 0; i < res.all.size(); i++) {
res.pos[res.all[i].second - res.tl] = i;
}
res.tree.init(res.tr - res.tl + 1);
return res;
}
void update(int x, T val) {
assert(tl <= x && x <= tr);
tree.update(pos[x - tl], val);
}
T get(int l, int r) {
int L = lower_bound(all.begin(), all.end(), make_pair(l, 0)) - all.begin();
int R = lower_bound(all.begin(), all.end(), make_pair(r + 1, 0)) - all.begin() - 1;
L = max(0, L);
R = min(R, tr - tl);
if (L <= R) {
return tree.get(L, R);
} else {
return 0;
}
}
};
template <typename T>
struct big_tree {
static const int N = 2e5 + 10;
int y_init[N];
T t[4 * N];
void build(int v = 1, int tl = 1, int tr = N - 1) {
if (tl == tr) {
t[v] = T(tl, y_init[tl]);
return;
}
int tm = (tl + tr) / 2;
build(v + v, tl, tm);
build(v + v + 1, tm + 1, tr);
t[v] = t[v + v] + t[v + v + 1];
}
void update(int x, int val, int v = 1, int tl = 1, int tr = N - 1) {
if (x < tl || tr < x) return;
t[v].update(x, val);
if (tl == tr) {
return;
}
int tm = (tl + tr) / 2;
update(x, val, v + v, tl, tm);
update(x, val, v + v + 1, tm + 1, tr);
}
int get(int lx, int rx, int ly, int ry, int v = 1, int tl = 1, int tr = N - 1) {
if (tr < lx || rx < tl) return 0;
if (lx <= tl && tr <= rx) {
return t[v].get(ly, ry);
}
int tm = (tl + tr) / 2;
return max(get(lx, rx, ly, ry, v + v, tl, tm), get(lx, rx, ly, ry, v + v + 1, tm + 1, tr));
}
};
big_tree<Node<int>> tree;
const int N = 2e5 + 10;
const int L = 20;
int c[N];
vector<int> g[N];
vector<int> pos[N];
int in[N], out[N];
int up[N][L];
int timer;
int depth[N];
void dfs_lca(int v = 1, int p = 1) {
in[v] = ++timer;
up[v][0] = p;
for (int i = 1; i < L; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for (int to : g[v]) {
if (to != p) {
depth[to] = depth[v] + 1;
dfs_lca(to, v);
}
}
out[v] = timer;
}
bool is_anc(int u, int v) {
return in[u] <= in[v] && in[v] <= out[u];
}
int lca(int u, int v) {
if (is_anc(u, v)) return u;
else if (is_anc(v, u)) return v;
for (int i = L - 1; i >= 0; i--) {
if (!is_anc(up[u][i], v)) u = up[u][i];
}
return up[u][0];
}
int dist(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs_lca(1, 1);
vector<tuple<int, int, int>> vec;
for (int i = 1; i <= n; i++) {
pos[c[i]].push_back(i);
}
for (int i = 1; i <= n; i++) {
if (pos[i].size() == 2) {
int u = pos[i][0], v = pos[i][1];
if (in[u] > in[v]) {
swap(u, v);
}
vec.emplace_back(dist(u, v), u, v);
}
}
sort(vec.rbegin(), vec.rend());
for (auto [d, u, v] : vec) {
tree.y_init[in[u]] = in[v];
tree.y_init[in[v]] = in[u];
}
tree.build();
int ans = 0;
for (auto [d, u, v] : vec) {
int cur = 0;
if (is_anc(u, v)) {
int w = 0;
for (int to : g[u]) {
if (is_anc(u, to) && is_anc(to, v)) {
w = to;
}
}
cur = max({cur, tree.get(1, in[w] - 1, in[v], out[v]), tree.get(out[w] + 1, n, in[v], out[v])});
} else {
cur = max(cur, tree.get(in[u], out[u], in[v], out[v]));
}
cur += 2;
tree.update(in[u], cur);
tree.update(in[v], cur);
ans = max(ans, d >= 2 ? cur + 1 : cur);
}
cout << ans << "\n";
#ifdef LOCAL
cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}
詳細信息
Test #1:
score: 100
Accepted
time: 218ms
memory: 281184kb
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: 198ms
memory: 282212kb
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: 228ms
memory: 280652kb
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: -100
Wrong Answer
time: 208ms
memory: 282004kb
input:
6 1 2 3 4 5 6 1 2 2 3 3 4 4 5 5 6
output:
0
result:
wrong answer 1st lines differ - expected: '1', found: '0'