QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#283464 | #7513. Palindromic Beads | light_ink_dots# | WA | 2474ms | 118520kb | C++14 | 6.0kb | 2023-12-14 17:37:20 | 2023-12-14 17:37:20 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct istream {
#define M (1 << 25)
char buf[M], *ch = buf - 1;
istream() { fread(buf, 1, M, stdin); }
inline istream& operator>>(int& x) {
while (!isdigit(*++ch)) continue;
for (x = *ch & 15; isdigit(*++ch);) x = x * 10 + (*ch & 15);
return *this;
}
inline istream& operator>>(long long& x) {
while (!isdigit(*++ch)) continue;
for (x = *ch & 15; isdigit(*++ch);) x = x * 10 + (*ch & 15);
return *this;
}
#undef M
} cinn;
const int maxn = 200010;
void build(int, int, int);
void insert(int, int, int, int);
int query(int, int, int, int, int);
int main() {
int n;
cinn >> n;
static vector<int> vec[maxn];
for (int i = 1, c; i <= n; i++) cinn >> c, vec[c].push_back(i);
static vector<int> G[maxn];
for (int i = 1, u, v; i < n; i++) {
cinn >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
static int fa[maxn], dep[maxn], dfn[maxn], ed[maxn];
function<void(int)> dfs = [&dfs](const int u) {
static int c;
dfn[u] = ++c;
for (int e : G[u])
if (e != fa[u]) {
fa[e] = u;
dep[e] = dep[u] + 1;
dfs(e);
}
ed[u] = c;
};
dfs(1);
static const int maxlg = 20;
static int lg[maxn], st[maxlg][maxn];
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= n; i++) st[0][dfn[i]] = fa[i];
static auto get = [](const int x, const int y) { return dep[x] < dep[y] ? x : y; };
for (int i = 1; i <= lg[n]; i++)
for (int j = 1; j + (1 << i) - 1 <= n; j++)
st[i][j] = get(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
static auto lca = [](int u, int v) {
if (u == v)
return u;
u = dfn[u], v = dfn[v];
if (u > v)
swap(u, v);
int Lg = lg[v - (++u) + 1];
return get(st[Lg][u], st[Lg][v - (1 << Lg) + 1]);
};
struct node {
int p, len;
};
vector<node> v;
for (int c = 1; c <= n; c++)
if (vec[c].size() == 2) {
int dis = dep[vec[c][0]] + dep[vec[c][1]] - (dep[lca(vec[c][0], vec[c][1])] << 1);
v.push_back({ c, dis });
}
sort(v.begin(), v.end(), [](const node x, const node y) { return x.len > y.len; });
int ans = 1;
build(1, n, 1);
for (auto u : v) {
int p = vec[u.p][0], q = vec[u.p][1];
if (dep[p] > dep[q])
swap(p, q);
int dp = 0;
if (dfn[p] <= dfn[q] && dfn[q] <= ed[p]) {
if (1 <= dfn[p] - 1 && dfn[q] <= ed[q])
dp = max(dp, query(1, dfn[p] - 1, 1, dfn[q], ed[q]));
if (dfn[q] <= ed[q] && ed[p] + 1 <= n)
dp = max(dp, query(dfn[q], ed[q], 1, ed[p] + 1, n));
} else {
if (dfn[p] > dfn[q])
swap(p, q);
dp = query(dfn[p], ed[p], 1, dfn[q], ed[q]);
}
if (dfn[p] > dfn[q])
swap(p, q);
dp += 2, ans = max(ans, dp);
insert(dfn[p], 1, dfn[q], dp);
}
for (int p = 1; p <= n; p++) {
if (G[p].size() == 1)
continue;
if (1 <= dfn[p] - 1 && dfn[p] + 1 <= ed[p])
ans = max(ans, query(1, dfn[p] - 1, 1, dfn[p] + 1, ed[p]) + 1);
if (dfn[p] + 1 <= ed[p] && ed[p] + 1 <= n)
ans = max(ans, query(dfn[p] + 1, ed[p], 1, ed[p] + 1, n) + 1);
for (int i : G[p])
if (i != fa[p] && dfn[p] + 1 <= dfn[i] - 1)
ans = max(ans, query(dfn[p] + 1, dfn[i] - 1, 1, dfn[i], ed[i]) + 1);
}
printf("%d\n", ans);
return 0;
}
struct bst_node {
int ls, rs;
int siz, r;
int p, val, mx;
};
bst_node bst[maxn << 6];
int cnt;
inline int newnode(int p, const int x) {
static random_device rd;
static mt19937 rng(rd());
bst[++cnt] = { 0, 0, 1, (int)rng(), p, x, x };
return cnt;
}
inline void pushup(const int u) {
bst[u].siz = bst[bst[u].ls].siz + bst[bst[u].rs].siz + 1;
bst[u].mx = max(max(bst[bst[u].ls].mx, bst[bst[u].rs].mx), bst[u].val);
return;
}
int merge(const int u, const int v) {
if (!u || !v)
return u | v;
if (bst[u].r < bst[v].r) {
bst[u].rs = merge(bst[u].rs, v);
pushup(u);
return u;
}
bst[v].ls = merge(u, bst[v].ls);
pushup(v);
return v;
}
void split(const int u, const int s, int& x, int& y) {
if (!u) {
x = y = 0;
return;
}
if (bst[u].p <= s) {
x = u, split(bst[x].rs, s, bst[x].rs, y);
pushup(x);
return;
}
y = u, split(bst[y].ls, s, x, bst[y].ls);
pushup(y);
return;
}
inline void insert(int& t, const int p, const int val) {
int x, y;
split(t, p, x, y);
t = merge(merge(x, newnode(p, val)), y);
return;
}
inline int query(int& u, int L, int R) {
int x, y, z;
split(u, L - 1, x, y);
split(y, R, y, z);
int ret = bst[y].mx;
u = merge(x, merge(y, z));
return ret;
}
struct node {
int l, r;
int rt;
};
node t[maxn << 2];
inline int ls(const int u) { return u << 1; }
inline int rs(const int u) { return u << 1 | 1; }
void build(const int l, const int r, const int u) {
t[u].l = l, t[u].r = r;
if (l == r)
return;
int mid = (l + r) >> 1;
build(l, mid, ls(u));
build(mid + 1, r, rs(u));
return;
}
void insert(const int p, const int u, const int q, const int v) {
if (p < t[u].l || t[u].r < p)
return;
insert(t[u].rt, q, v);
if (t[u].l == t[u].r)
return;
insert(p, ls(u), q, v);
insert(p, rs(u), q, v);
return;
}
int query(int l, int r, int u, int L, int R) {
if (r < t[u].l || t[u].r < l)
return 0;
if (l <= t[u].l && t[u].r <= r)
return query(t[u].rt, L, R);
return max(query(l, r, ls(u), L, R), query(l, r, rs(u), L, R));
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 19340kb
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: 17272kb
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: 4ms
memory: 19612kb
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: 4ms
memory: 19276kb
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: 4ms
memory: 27836kb
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: 903ms
memory: 105208kb
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: 880ms
memory: 101080kb
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: 0
Accepted
time: 1034ms
memory: 100464kb
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:
45
result:
ok single line: '45'
Test #9:
score: 0
Accepted
time: 1244ms
memory: 101784kb
input:
200000 36449 57574 3145 38591 832 17710 66613 78947 27635 83275 89878 48329 94614 584 96832 9321 72046 44873 5396 61452 63224 63740 26579 13706 108490 19092 89439 85884 12016 5105 48638 74004 41569 35006 22276 45609 25350 49906 35479 15875 68938 77699 48828 21628 11242 77040 70838 45771 27704 64865 ...
output:
135
result:
ok single line: '135'
Test #10:
score: 0
Accepted
time: 1886ms
memory: 106664kb
input:
200000 13528 65006 30352 8565 36687 6748 5507 44320 7189 17847 46996 82728 102722 4727 36914 74228 21460 87970 11733 47170 67282 104558 66436 64504 57055 88619 42995 25569 101298 90984 76491 51994 62257 103424 8221 69668 99170 6808 29043 73058 5277 26614 23654 25152 64939 38418 78518 5330 37531 4305...
output:
425
result:
ok single line: '425'
Test #11:
score: 0
Accepted
time: 2474ms
memory: 118520kb
input:
200000 92279 5566 62695 50240 45387 51097 57743 94873 53220 29260 72584 38043 86335 33441 12946 30267 12932 18258 4560 8896 64393 39608 53183 34285 36518 18501 51940 8658 101018 48522 21336 104735 25785 73132 33489 81905 94563 18128 87872 24765 54563 57218 61869 50458 75919 63764 48155 4489 35212 44...
output:
839
result:
ok single line: '839'
Test #12:
score: 0
Accepted
time: 811ms
memory: 99372kb
input:
200000 96438 84772 88103 81186 60908 116093 94738 28602 35022 91108 60687 75572 100094 50553 58445 117024 96154 53539 23185 112280 90369 32413 95244 11077 91008 109781 6404 2285 3544 111712 49414 10399 113626 81435 11321 52557 17023 113260 14225 66464 61352 98403 36521 110038 57172 42868 68512 69031...
output:
31
result:
ok single line: '31'
Test #13:
score: -100
Wrong Answer
time: 929ms
memory: 97216kb
input:
200000 50750 86282 92049 114579 8296 28675 45880 47381 71400 43379 111535 32316 37104 35968 100241 6914 81284 48969 62890 63486 107557 80178 76322 31515 24682 85646 12681 106054 5167 50339 39004 16152 112081 10605 66750 51623 96332 77287 75452 50609 1549 1652 45229 73171 10015 66323 90164 97491 1007...
output:
8106
result:
wrong answer 1st lines differ - expected: '8108', found: '8106'