QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204183#7513. Palindromic Beadsucup-team228AC ✓966ms314752kbC++206.9kb2023-10-07 05:09:032024-03-27 16:27:36

Judging History

你现在查看的是测评时间为 2024-03-27 16:27:36 的历史记录

  • [2024-03-27 16:42:17]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:998ms
  • 内存:314692kb
  • [2024-03-27 16:34:54]
  • hack成功,自动添加数据
  • (/hack/584)
  • [2024-03-27 16:27:36]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:100
  • 用时:966ms
  • 内存:314752kb
  • [2024-03-27 16:18:45]
  • hack成功,自动添加数据
  • (/hack/583)
  • [2023-10-07 05:09:04]
  • 评测
  • 测评结果:100
  • 用时:866ms
  • 内存:316952kb
  • [2023-10-07 05:09:03]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

template <typename T>
struct small_tree {
    int n;
    vector<T> t;
    void init(int _n) {
        n = _n + 5;
        t.assign(2 * n + 10, 0);
    }
    void update(int pos, T val) {
        for (t[pos += n] = val; pos > 1; pos >>= 1) {
            t[pos >> 1] = max(t[pos], t[pos ^ 1]);
        }
    }
    T get(int l, int r) {
        T res = 0;
        for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) res = max(res, t[l++]);
            if (!(r & 1)) res = max(res, t[r--]);
        }
        return res;
    }
};

vector<pair<int, int>> merge(const vector<pair<int, int>>& a, const vector<pair<int, int>>& b) {
    int n = a.size(), m = b.size();
    int i = 0, j = 0;
    vector<pair<int, int>> res;
    while (i < n && j < m) {
        if (a[i].first < b[j].first) {
            res.push_back(a[i++]);
        } else {
            res.push_back(b[j++]);
        }
    }
    while (i < n) {
        res.push_back(a[i++]);
    }
    while (j < m) {
        res.push_back(b[j++]);
    }
    return res;
}

template <typename T>
struct Node {
    small_tree<T> tree;
    int tl, tr;
    vector<pair<int, int>> all;
    vector<int> pos;
    Node(int x = 0, int y = 0) {
        tl = x;
        tr = x;
        all = {{y, x}};
        pos = {0};
        tree.init(1);
    }
    friend Node operator+(const Node& a, const Node& b) {
        Node res;
        res.tl = a.tl;
        res.tr = b.tr;
        res.all = merge(a.all, b.all);
        res.pos.resize(res.all.size());
        for (int i = 0; i < res.all.size(); i++) {
            res.pos[res.all[i].second - res.tl] = i;
        }
        res.tree.init(res.tr - res.tl + 1);
        return res;
    }
    void update(int x, T val) {
        assert(tl <= x && x <= tr);
        tree.update(pos[x - tl], val);
    }
    T get(int l, int r) {
        int L = lower_bound(all.begin(), all.end(), make_pair(l, 0)) - all.begin();
        int R = lower_bound(all.begin(), all.end(), make_pair(r + 1, 0)) - all.begin() - 1;
        L = max(0, L);
        R = min(R, tr - tl);
        if (L <= R) {
            return tree.get(L, R);
        } else {
            return 0;
        }
    }
};

template <typename T>
struct big_tree {
    static const int N = 2e5 + 10;
    int y_init[N];
    T t[4 * N];
    void build(int v = 1, int tl = 1, int tr = N - 1) {
        if (tl == tr) {
            t[v] = T(tl, y_init[tl]);
            return;
        }
        int tm = (tl + tr) / 2;
        build(v + v, tl, tm);
        build(v + v + 1, tm + 1, tr);
        t[v] = t[v + v] + t[v + v + 1];
    }
    void update(int x, int val, int v = 1, int tl = 1, int tr = N - 1) {
        if (x < tl || tr < x) return;
        t[v].update(x, val);
        if (tl == tr) {
            return;
        }
        int tm = (tl + tr) / 2;
        update(x, val, v + v, tl, tm);
        update(x, val, v + v + 1, tm + 1, tr);
    }
    int get(int lx, int rx, int ly, int ry, int v = 1, int tl = 1, int tr = N - 1) {
        if (tr < lx || rx < tl) return 0;
        if (lx <= tl && tr <= rx) {
            return t[v].get(ly, ry);
        }
        int tm = (tl + tr) / 2;
        return max(get(lx, rx, ly, ry, v + v, tl, tm), get(lx, rx, ly, ry, v + v + 1, tm + 1, tr));
    }
};

big_tree<Node<int>> tree;

const int N = 2e5 + 10;
const int L = 20;
int c[N];
vector<int> g[N];
vector<int> pos[N];

vector<tuple<int, int, int>> vec;

int in[N], out[N];
int up[N][L];
int timer;
int depth[N];

void dfs_lca(int v = 1, int p = 1) {
    in[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i < L; i++) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for (int to : g[v]) {
        if (to != p) {
            depth[to] = depth[v] + 1;
            dfs_lca(to, v);
        }
    }
    out[v] = timer;
}

bool is_anc(int u, int v) {
    return in[u] <= in[v] && in[v] <= out[u];
}

int lca(int u, int v) {
    if (is_anc(u, v)) return u;
    else if (is_anc(v, u)) return v;
    for (int i = L - 1; i >= 0; i--) {
        if (!is_anc(up[u][i], v)) u = up[u][i];
    }
    return up[u][0];
}

int dist(int u, int v) {
    return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}

void stress_tree() {
    mt19937 rnd;
    const int n = 12322;
    vector<int> ys(n + 1, 0);
    vector<int> a(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        ys[i] = rnd() % n + 1;
    }
    for (int i = 1; i <= n; i++) {
        tree.y_init[i] = ys[i];
    }
    tree.build();
    while (true) {
        int lx = rnd() % n + 1;
        int rx = rnd() % n + 1;
        int ly = rnd() % n + 1;
        int ry = rnd() % n + 1;
        if (lx > rx) {
            swap(lx, rx);
        }
        if (ly > ry) {
            swap(ly, ry);
        }
        int ans = tree.get(lx, rx, ly, ry);
        int res = 0;
        for (int i = lx; i <= rx; i++) {
            if (ly <= ys[i] && ys[i] <= ry) {
                res = max(res, a[i]);
            }
        }
        if (ans != res) {
            cout << "WA\n";
            break;
        } else {
            cout << "OK: " << ans << endl;
            int i = rnd() % n + 1;
            tree.update(i, ans + 1);
            a[i] = ans + 1;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
    }
    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs_lca(1, 1);
    for (int i = 1; i <= n; i++) {
        pos[c[i]].push_back(i);
    }
    for (int i = 1; i <= n; i++) {
        if (pos[i].size() == 2) {
            int u = pos[i][0], v = pos[i][1];
            if (in[u] > in[v]) {
                swap(u, v);
            }
            vec.emplace_back(dist(u, v), u, v);
        }
    }
    sort(vec.rbegin(), vec.rend());

    for (auto [d, u, v] : vec) {
        tree.y_init[in[u]] = in[v];
        tree.y_init[in[v]] = in[u];
    }
    tree.build();

    int ans = 1;
    for (auto [d, u, v] : vec) {
        int cur = 0;
        if (is_anc(u, v)) {
            int w = 0;
            for (int to : g[u]) {
                if (is_anc(u, to) && is_anc(to, v)) {
                    w = to;
                }
            }
            cur = max(tree.get(1, in[w] - 1, in[v], out[v]), tree.get(out[w] + 1, n, in[v], out[v]));
        } else {
            cur = tree.get(in[u], out[u], in[v], out[v]);
        }
        cur += 2;
        tree.update(in[u], cur);
        tree.update(in[v], cur);
        ans = max(ans, d >= 2 ? cur + 1 : cur);
    }
    cout << ans << "\n";

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 220ms
memory: 269936kb

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: 198ms
memory: 269524kb

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: 201ms
memory: 269416kb

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: 190ms
memory: 268680kb

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: 228ms
memory: 269792kb

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: 701ms
memory: 305620kb

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: 694ms
memory: 305608kb

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: 750ms
memory: 306296kb

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: 816ms
memory: 307184kb

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: 865ms
memory: 310656kb

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: 966ms
memory: 314752kb

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: 656ms
memory: 306140kb

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: 0
Accepted
time: 650ms
memory: 306536kb

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:

8108

result:

ok single line: '8108'

Test #14:

score: 0
Accepted
time: 933ms
memory: 311024kb

input:

200000
103710 58811 65880 57203 97861 52397 63433 39586 97768 103209 103882 94183 50235 39832 92390 90699 48046 43740 86592 19659 27107 39892 7594 27400 95581 16516 29641 51389 17391 97193 93724 70446 91047 67946 5821 96978 101553 35096 54450 104968 93366 64974 46399 81084 97703 64161 38168 55002 10...

output:

16254

result:

ok single line: '16254'

Test #15:

score: 0
Accepted
time: 900ms
memory: 310412kb

input:

200000
88202 74369 54948 48433 90064 106858 57479 24810 31634 66587 14453 9546 77108 81830 36583 76158 5411 103453 9138 44425 55569 108655 29336 6156 33 78407 12653 61049 89911 48056 85840 41409 60932 69039 50823 604 57680 30956 7683 35427 99677 86508 44657 42731 105490 13120 101415 3024 32965 10326...

output:

4

result:

ok single line: '4'

Test #16:

score: 0
Accepted
time: 642ms
memory: 305904kb

input:

200000
10797 110783 14050 78735 2502 18514 116854 6066 7024 88298 49894 5068 113306 111164 293 38365 84020 117567 109538 103830 69630 28231 107911 18477 95413 78305 59492 112114 12973 20189 93556 49731 50223 69605 54609 74804 42990 28998 22878 45999 93363 7282 71880 49057 46461 94559 53582 109357 25...

output:

6

result:

ok single line: '6'

Test #17:

score: 0
Accepted
time: 726ms
memory: 305580kb

input:

200000
48737 28745 46719 51708 55344 100505 22387 88659 52750 92399 63371 63773 69558 28543 3923 23010 101235 32115 106541 2327 42175 67610 109244 77794 49476 70063 4296 22615 9227 107435 58380 27995 78950 8976 80501 71538 73668 82538 68732 86931 19515 54326 85071 28871 46614 56610 53551 67531 47246...

output:

5

result:

ok single line: '5'

Test #18:

score: 0
Accepted
time: 707ms
memory: 305640kb

input:

200000
26426 62873 30891 26591 23480 10950 50429 4430 25803 27494 14124 45946 2115 72645 23096 45892 34188 62963 63230 4026 19204 52592 84051 81275 101029 63022 83786 69487 46792 97893 25402 11283 50048 59942 53521 52133 12216 16801 24535 31962 102839 71080 53210 8687 6049 56323 74430 81175 55370 64...

output:

5

result:

ok single line: '5'

Test #19:

score: 0
Accepted
time: 729ms
memory: 305832kb

input:

200000
64192 51123 57775 25958 30138 51389 48055 24350 73959 42882 73159 20060 24764 56730 79344 32622 98700 1752 102538 20803 89788 11428 16935 19307 25767 38250 64733 76253 69835 40964 8146 21818 64392 104263 78420 57145 46240 44745 28359 28598 38527 60156 9818 30837 20404 99634 75031 71165 60654 ...

output:

4

result:

ok single line: '4'

Test #20:

score: 0
Accepted
time: 702ms
memory: 306084kb

input:

200000
30875 49873 48854 101512 66585 67679 13806 38786 40598 83034 6738 61701 26783 17734 29228 34850 55071 61098 88072 20496 60333 53325 32602 102521 99956 17288 105589 74068 66276 21670 78040 75943 2292 87380 76885 55439 897 82814 36764 80920 53230 16085 36883 61234 106576 28271 6915 11089 21463 ...

output:

3

result:

ok single line: '3'

Test #21:

score: 0
Accepted
time: 686ms
memory: 306196kb

input:

200000
75618 85661 92814 29954 24466 50323 95886 9384 11050 57981 86917 38495 12021 82933 13692 5285 32566 29306 59046 47309 90373 70494 81527 6001 62574 94496 70347 31615 21329 91026 46255 26717 108227 71208 49245 29004 99849 61951 46547 72596 51712 22511 4844 20109 60399 24412 104182 14880 40185 2...

output:

3

result:

ok single line: '3'

Extra Test:

score: 0
Extra Test Passed