QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#283459#7513. Palindromic Beadslight_ink_dots#WA 0ms21212kbC++145.8kb2023-12-14 17:29:512023-12-14 17:29:52

Judging History

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

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

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;
        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]);
        }
        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;
        ans = max(ans, query(1, dfn[p] - 1, 1, dfn[p] + 1, ed[p]) + 1);
        ans = max(ans, query(dfn[p] + 1, ed[p], 1, ed[p] + 1, n) + 1);
        for (int i : G[p])
            if (dep[i] > dep[p] && dfn[p] + 1 < dfn[i] - 1)
                ans = max(ans, query(dfn[p] + 1, dfn[i] - 1, dfn[i], ed[i], 1) + 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: 0
Wrong Answer
time: 0ms
memory: 21212kb

input:

4
1 1 2 2
1 2
2 3
2 4

output:

2

result:

wrong answer 1st lines differ - expected: '3', found: '2'