QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#236666 | #7513. Palindromic Beads | alpha1022 | WA | 191ms | 513604kb | C++14 | 4.4kb | 2023-11-04 09:43:50 | 2023-11-04 09:43:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 2e5;
const int LG = 20;
int n;
int a[N + 5];
pair<int, int> pos[N + 5];
vector<int> e[N + 5];
int fa[N + 5], anc[N + 5][LG], dep[N + 5], siz[N + 5], id[N + 5];
int f[N + 5];
void dfs1(int u) {
static int tot = 0;
id[u] = ++tot, siz[u] = 1, anc[u][0] = fa[u];
for (int k = 1; k < LG; ++k) anc[u][k] = anc[anc[u][k - 1]][k - 1];
for (int v: e[u])
if (v != fa[u])
fa[v] = u, dep[v] = dep[u] + 1, dfs1(v), siz[u] += siz[v];
}
int query(int u, int v) {
for (int k = LG - 1; k >= 0; --k)
if (dep[anc[u][k]] > dep[v]) u = anc[u][k];
return u;
}
struct SegmentTree {
#define ls (u << 1)
#define rs (ls | 1)
int seg[N * 4 + 5];
void build(int u, int tl, int tr) {
seg[u] = -inf;
if (tl == tr) return ;
int mid = (tl + tr) >> 1;
build(ls, tl, mid), build(rs, mid + 1, tr);
}
void pushUp(int u) { seg[u] = max(seg[ls], seg[rs]); }
void insert(int x, int k, int u, int tl, int tr) {
if (tl == tr) { seg[u] = k; return ; }
int mid = (tl + tr) >> 1;
if (x <= mid) insert(x, k, ls, tl, mid);
else insert(x, k, rs, mid + 1, tr);
pushUp(u);
}
int query(int l, int u, int tl, int tr) {
if (l <= tl) return seg[u];
int mid = (tl + tr) >> 1;
int ret = query(l, rs, mid + 1, tr);
if (l <= mid) ret = max(ret, query(l, ls, tl, mid));
return ret;
}
#undef ls
#undef rs
} seg;
struct TreeOfTree {
struct segnode { int val = -inf, ls, rs; } seg[N * 200 + 5];
void update(int l, int r, int k, int &u, int tl, int tr) {
if (l > r) return ;
static int tot = 0;
if (!u) u = ++tot;
if (l <= tl && tr <= r) { seg[u].val = max(seg[u].val, k); return ; }
int mid = (tl + tr) >> 1;
if (l <= mid) update(l, r, k, seg[u].ls, tl, mid);
if (r > mid) update(l, r, k, seg[u].rs, mid + 1, tr);
}
int query(int x, int u, int tl, int tr) {
if (tl == tr) return seg[u].val;
int mid = (tl + tr) >> 1;
return max(seg[u].val, x <= mid ? query(x, seg[u].ls, tl, mid) : query(x, seg[u].rs, mid + 1, tr));
}
#define ls (u << 1)
#define rs (ls | 1)
int rt[N * 4 + 5];
void update(int l, int r, int x, int y, int k, int u, int tl, int tr) {
if (l > r || x > y) return ;
if (l <= tl && tr <= r) { update(x, y, k, rt[u], 1, n); return ; }
int mid = (tl + tr) >> 1;
if (l <= mid) update(l, r, x, y, k, ls, tl, mid);
if (r > mid) update(l, r, x, y, k, rs, mid + 1, tr);
}
int query(int x, int y, int u, int tl, int tr) {
if (tl == tr) return query(y, rt[u], 1, n);
int mid = (tl + tr) >> 1;
return x <= mid ? query(x, y, ls, tl, mid) : query(x, y, rs, mid + 1, tr);
}
#undef ls
#undef rs
} tree;
int up[N + 5];
void dfs2(int u) {
if (up[u]) {
f[a[u]] = 2 + (dep[up[u]] < dep[u] - 1);
seg.insert(dep[up[u]], f[a[u]] = max(f[a[u]], seg.query(dep[up[u]], 1, 0, n - 1) + 2), 1, 0, n - 1);
int v = query(u, up[u]);
tree.update(1, id[v] - 1, id[u], id[u] + siz[u] - 1, f[a[u]], 1, 1, n), tree.update(id[u], id[u] + siz[u] - 1, id[v] + siz[v], n, f[a[u]], 1, 1, n);
}
for (int v: e[u])
if (v != fa[u])
dfs2(v);
if (up[u]) seg.insert(dep[up[u]], -inf, 1, 0, n - 1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) pos[i] = {n + 1, n + 1};
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x), a[i] = x;
if (pos[x].first <= n) pos[x].second = i;
else pos[x].first = i;
}
for (int i = 2; i <= n; ++i) { int u, v; scanf("%d%d", &u, &v), e[u].push_back(v), e[v].push_back(u); }
dfs1(1), id[n + 1] = n + 1;
for (int i = 1; i <= n; ++i) {
auto &[u, v] = pos[i];
if (id[u] < id[v]) swap(u, v);
}
sort(pos + 1, pos + n + 1, [](pair<int, int> x, pair<int, int> y) { return id[x.first] < id[y.first]; });
for (int i = 1; i <= n; ++i) {
auto [u, v] = pos[i];
if (v > n) continue;
if (id[u] >= id[v] && id[u] < id[v] + siz[v]) a[u] = i, up[u] = v;
}
seg.build(1, 0, n - 1), dfs2(1);
for (int i = 1; i <= n; ++i) {
auto [u, v] = pos[i];
if (v > n) continue;
if (u > n) { f[i] = 1; continue; }
if (id[u] >= id[v] && id[u] < id[v] + siz[v]) continue;
tree.update(id[v], id[v] + siz[v] - 1, id[u], id[u] + siz[u] - 1, f[i] = max(3, tree.query(id[v], id[u], 1, 1, n) + 2), 1, 1, n);
}
printf("%d\n", *max_element(f + 1, f + n + 1));
}
詳細信息
Test #1:
score: 100
Accepted
time: 28ms
memory: 487556kb
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: 16ms
memory: 489032kb
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: 20ms
memory: 488112kb
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: 35ms
memory: 487104kb
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: 20ms
memory: 487112kb
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: 0
Accepted
time: 160ms
memory: 512312kb
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
result:
ok single line: '5'
Test #7:
score: 0
Accepted
time: 171ms
memory: 511812kb
input:
200000 13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...
output:
7
result:
ok single line: '7'
Test #8:
score: -100
Wrong Answer
time: 191ms
memory: 513604kb
input:
200000 38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...
output:
43
result:
wrong answer 1st lines differ - expected: '45', found: '43'