QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#283454#7513. Palindromic Beadslight_ink_dots#TL 2017ms105360kbC++145.2kb2023-12-14 17:23:042023-12-14 17:23:04

Judging History

你现在查看的是最新测评结果

  • [2024-03-27 16:34:54]
  • hack成功,自动添加数据
  • (/hack/584)
  • [2024-03-27 16:18:45]
  • hack成功,自动添加数据
  • (/hack/583)
  • [2023-12-14 17:23:04]
  • 评测
  • 测评结果:TL
  • 用时:2017ms
  • 内存:105360kb
  • [2023-12-14 17:23:04]
  • 提交

answer

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

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;
    scanf("%d", &n);
    static vector<int> vec[maxn];
    for (int i = 1, c; i <= n; i++) scanf("%d", &c), vec[c].push_back(i);
    static vector<int> G[maxn];
    for (int i = 1, u, v; i < n; i++) {
        scanf("%d %d", &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;
        if (dfn[p] <= dfn[q] && dfn[q] <= ed[p]) {
            dp = max(query(1, dfn[p] - 1, 1, dfn[q], ed[q]), 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]);
        }
        dp += 2, ans = max(ans, dp);
        if (dfn[p] > dfn[q])
            swap(p, q);
        insert(dfn[p], 1, dfn[q], dp);
    }
    for (int p = 1; p <= n; p++)
        for (int i : G[p])
            if (dep[i] > dep[p]) {
                ans = max(ans, query(1, dfn[p] - 1, 1, dfn[i], ed[i]) + 1);
                ans = max(ans, query(dfn[p] + 1, dfn[i] - 1, dfn[i], ed[i], 1) + 1);
                ans = max(ans, query(dfn[i], ed[i], 1, ed[i] + 1, n) + 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));
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 17300kb

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: 17276kb

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: 19324kb

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: 3ms
memory: 15240kb

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: 3ms
memory: 23784kb

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: 1062ms
memory: 99388kb

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: 1025ms
memory: 98728kb

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: 1195ms
memory: 95552kb

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: 1369ms
memory: 98284kb

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: 2017ms
memory: 105360kb

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: -100
Time Limit Exceeded

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: