QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#534547 | #7513. Palindromic Beads | ly_xxys | WA | 574ms | 259356kb | C++17 | 5.1kb | 2024-08-27 13:46:05 | 2024-08-27 13:46:05 |
Judging History
answer
//#pragma GCC otimize(2)
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+11;
struct Node {
int Max = 0;
int l = 0, r = 0;
};
Node info[N*150];
struct Tree {
int tot = 0;
int open(){
return ++ tot;
}
void modify(int &u, int pl, int pr, int pos, int v){
if (!u) u = open();
info[u].Max = max(info[u].Max, v);
if (pl == pr)return;
int mid = (pl + pr) >> 1;
if (mid >= pos){
modify(info[u].l, pl, mid, pos, v);
} else {
modify(info[u].r, mid+1, pr, pos, v);
}
}
int query(int &u, int pl, int pr, int l, int r){
if (!u) return 0;
if (pl >= l && pr <= r){
return info[u].Max;
}
int mid = (pl + pr) >> 1;
int Max = 0;
if (mid >= l) Max = query(info[u].r, pl, mid, l, r);
if (mid < r) Max = max(Max, query(info[u].r, mid+1, pr, l, r));
return Max;
}
};
struct order {
int st, ed;
};
void solve(){
int n;
cin >> n;
vector <vector<int>> g(n), st(n, vector<int>(19)), pairs(n);
vector <int> dep(n), color(n);
vector <order> ords(n);
for (auto &x : color) cin >> x, x -= 1;
for (int i = 0, a, b; i < n-1; ++ i){
cin >> a >> b;
-- a, -- b;
g[a].push_back(b), g[b].push_back(a);
}
// 求lca部分
int tot = 0;
function <void(int,int)> dfs = [&](int x, int fa){
ords[x].st = tot ++;
st[x][0] = fa;
for (int i = 1; 1<<i <= dep[x]; ++ i)
st[x][i] = st[st[x][i-1]][i-1];
pairs[color[x]].push_back(x);
for (auto &y : g[x]){
if (y == fa) continue;
dep[y] = dep[x] + 1;
dfs(y, x);
}
ords[x].ed = tot-1;
};
auto lca = [&](int x, int y)->int{
if (dep[x] < dep[y]) swap(x, y);
for (int k=0, d=dep[x]-dep[y]; 1<<k <= d; ++ k)
if (d>>k & 1) x = st[x][k];
if (x == y) return x;
for (int k=log2(dep[x]); k >= 0; -- k)
if (st[x][k] != st[y][k]) x = st[x][k], y = st[y][k];
return st[x][0];
};
auto Dis = [&](int x, int y)->int{
int anc = lca(x, y);
return dep[x] + dep[y] - 2*dep[anc];
};
dfs(0, 0);
assert(tot == n);
vector <array<int,3>> fs;
for (int i = 0; i < n; ++ i){
if (pairs[i].size() == 2){
int u = pairs[i][0], v = pairs[i][1];
fs.push_back({u, v, Dis(pairs[i][0], pairs[i][1])});
}
}
sort(fs.begin(), fs.end(), [&](array <int,3> &a, array <int, 3> &b){
return a[2] > b[2];
});
// 外层线段树
vector <int> root(4*n);
Tree Y;
function<void(int,int,int,int,int,int)> modify = [&](int u, int pl, int pr, int x, int y, int v){
Y.modify(root[u], 0, n-1, y, v);
if (pl == pr) return;
int mid = (pl + pr) >> 1;
if (mid >= x) modify(2*u, pl, mid, x, y, v);
else modify(2*u+1, mid+1, pr, x, y, v);
};
function<int(int,int,int,int,int,int,int)> ask = [&](int u, int pl, int pr, int l1, int r1, int l2, int r2){
if (l1 > r1 || l2 > r2) return 0;
if (pl >= l1 && pr <= r1){
if (!root[u]) return 0;
else return Y.query(root[u], 0, n-1, l2, r2);
}
int mid = (pl + pr) >> 1, Max = 0;
if (root[2*u] && mid >= l1) Max = ask(2*u, pl, mid, l1, r1, l2, r2);
if (root[2*u+1] && mid < r1) Max = max(Max, ask(2*u+1, mid+1, pr, l1, r1, l2, r2));
return Max;
};
vector <int> f(fs.size());
// 查询 + dp 部分
for (int i = 0; i < fs.size(); ++ i){
auto &[u, v, d] = fs[i];
int ord1, ord2, ord3, ord4;
assert(ords[u].st < ords[v].st);
int anc = lca(u, v);
// 根据 u 是否是 v 的祖先, 讨论 子路径的 情况
if (anc == u){
int lead = 0;
for (auto &x : g[u]){
if (Dis(x, v) == d-1){
lead = x;
break;
}
}
// 小于 0 或者 大于 n-1 不影响结果
ord1 = ords[lead].st-1, ord2 = ords[v].st, ord3 = ords[v].ed;
f[i] = ask(1, 0, n-1, 0, ord1, ord2, ord3);
ord1 = ords[v].st, ord2 = ords[v].ed, ord3 = ords[lead].ed+1;
f[i] = max(f[i], ask(1, 0, n-1, ord1, ord2, ord3, n-1));
f[i] += 2;
} else {
ord1 = ords[u].st, ord2 = ords[u].ed, ord3 = ords[v].st, ord4 = ords[v].ed;
f[i] = ask(1, 0, n-1, ord1, ord2, ord3, ord4);
f[i] += 2;
}
modify(1, 0, n-1, ords[u].st, ords[v].st, f[i]);
}
int res = 1;
for (int i = 0; i < fs.size(); ++ i){
int d = fs[i][2];
if (d >= 2) ++ f[i];
res = max(res, f[i]);
}
cout << res << "\n";
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int _ = 1;
while (_--)
solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3540kb
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: 3608kb
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: 3616kb
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: 0ms
memory: 3544kb
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: 2ms
memory: 4860kb
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: 514ms
memory: 258416kb
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: 535ms
memory: 258352kb
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: 574ms
memory: 259356kb
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:
9
result:
wrong answer 1st lines differ - expected: '45', found: '9'