QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#204181 | #7513. Palindromic Beads | ucup-team228 | WA | 239ms | 282300kb | C++20 | 7.0kb | 2023-10-07 05:04:31 | 2023-10-07 05:04:32 |
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)];
}
void stress_tree() {
mt19937 rnd;
const int n = 12322;
vector<int> ys(n + 1, 0);
vector<int> a(n + 1, 0);
for (int i = 1; i <= n; i++) {
ys[i] = rnd() % n + 1;
}
for (int i = 1; i <= n; i++) {
tree.y_init[i] = ys[i];
}
tree.build();
while (true) {
int lx = rnd() % n + 1;
int rx = rnd() % n + 1;
int ly = rnd() % n + 1;
int ry = rnd() % n + 1;
if (lx > rx) {
swap(lx, rx);
}
if (ly > ry) {
swap(ly, ry);
}
int ans = tree.get(lx, rx, ly, ry);
int res = 0;
for (int i = lx; i <= rx; i++) {
if (ly <= ys[i] && ys[i] <= ry) {
res = max(res, a[i]);
}
}
if (ans != res) {
cout << "WA\n";
break;
} else {
cout << "OK: " << ans << endl;
int i = rnd() % n + 1;
tree.update(i, ans + 1);
a[i] = ans + 1;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
// stress_tree();
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(tree.get(1, in[w] - 1, in[v], out[v]), tree.get(out[w] + 1, n, in[v], out[v]));
} else {
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: 217ms
memory: 280484kb
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: 203ms
memory: 281408kb
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: 239ms
memory: 282300kb
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: 230ms
memory: 281436kb
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'