QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#407527#2580. 语言zzy09220 726ms111652kbC++144.2kb2024-05-08 21:49:332024-05-08 21:49:34

Judging History

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

  • [2024-05-08 21:49:34]
  • 评测
  • 测评结果:0
  • 用时:726ms
  • 内存:111652kb
  • [2024-05-08 21:49:33]
  • 提交

answer

#include <bits/stdc++.h>

constexpr int N = 100005;
int n, m;
std::vector<int> t[N];
int dep[N], dfn[N], rnk[N], fa[N], tot;

void dfs(int u) {
    dfn[u] = ++tot;
    rnk[tot] = u;
    for (const auto &v : t[u]) {
        if (v == fa[u])
            continue;
        fa[v] = u;
        dep[v] = dep[u] + 1;
        dfs(v);
    }
}

inline int maxv(int a, int b) {
    if (!a || !b) 
        return a | b;
    return dep[a] < dep[b] ? a : b;
}

int st[23][N];

inline void init() {
    for (int i = 1; i <= n; i++)
        st[0][i] = rnk[i];
    for (int k = 1; k <= 19; k++) 
        for (int j = 1; j + (1 << k) - 1 <= n; j++)
            st[k][j] = maxv(st[k - 1][j], st[k - 1][j + (1 << (k - 1))]);
}

inline int lca(int u, int v) {
    if (!u || !v)
        return u | v;
    if (u == v)
        return u;
    int l = std::min(dfn[u], dfn[v]) + 1, r = std::max(dfn[u], dfn[v]), k = std::__lg(r - l + 1);
    return fa[maxv(st[k][l], st[k][r - (1 << k) + 1])];
}

struct node {
    int ls, rs, lv, rv, lc, sz, cc;
}tree[N << 7];

#define ls(p)  tree[p].ls
#define rs(p)  tree[p].rs
#define lv(p)  tree[p].lv
#define rv(p)  tree[p].rv
#define lc(p)  tree[p].lc
#define sz(p)  tree[p].sz
#define cc(p)  tree[p].cc

int cnt;

inline void pushup(int p) {
    if (!ls(p) || !rs(p) || !lc(ls(p)) || !lc(rs(p))) {
        int son = (lc(ls(p)) ? ls(p) : rs(p));
        sz(p) = sz(son);
        lv(p) = lv(son);
        rv(p) = rv(son);
        lc(p) = lc(son);
        return;
    }
    sz(p) = sz(ls(p)) + sz(rs(p)) + dep[lc(ls(p))] + dep[lc(rs(p))] - dep[lca(rv(ls(p)), lv(rs(p)))] - dep[lca(lc(ls(p)), lc(rs(p)))] - 1;
    lv(p) = lv(ls(p)) ? lv(ls(p)) : lv(rs(p));
    rv(p) = rv(rs(p)) ? rv(rs(p)) : rv(ls(p));
    lc(p) = lca(lc(ls(p)), lc(rs(p)));
    // std::cout << "pushup:" << p << ' ' << sz(p) << ' ' << sz(ls(p)) << ' ' << sz(rs(p)) << ' ' << lc(ls(p)) << ' ' << lc(rs(p)) << '\n';
}

void mdf(int p, int l, int r, int x, int v) {
    if (l == r) {
        cc(p) += v;
        if (cc(p)) 
            lv(p) = rv(p) = lc(p) = rnk[x], sz(p) = 1;
        else 
            lv(p) = rv(p) = lc(p) = sz(p) = 0;
        return;
    }    
    int mid = (l + r) >> 1;
    if (x <= mid) {
        if (!ls(p))
            ls(p) = ++cnt;
        mdf(ls(p), l, mid, x, v);
    } else {
        if (!rs(p))
            rs(p) = ++cnt;
        mdf(rs(p), mid + 1, r, x, v);
    }
    pushup(p);
}

int merge(int r1, int r2, int l, int r) {
    if (!r1 || !r2) 
        return r1 | r2;
    if (l == r) {
        cc(r1) += cc(r2);
        if (cc(r1)) 
            lv(r1) = rv(r1) = lc(r1) = rnk[l], sz(r1) = 1;
        else 
            lv(r1) = rv(r1) = lc(r1) = sz(r1) = 0;
        return r1;
    }
    int mid = (l + r) >> 1;
    ls(r1) = merge(ls(r1), ls(r2), l, mid);
    rs(r1) = merge(rs(r1), rs(r2), mid + 1, r);
    pushup(r1);
    return r1;
}

std::vector<std::pair<int, int> > op[N];

int rt[N];
long long ans = 0;
void solve(int u) {
    rt[u] = ++cnt;
    for (const int &v : t[u]) {
        if (v == fa[u])
            continue;
        solve(v);
        rt[u] = merge(rt[u], rt[v], 1, n);
    }
    for (const auto &[x, v] : op[u]) 
        mdf(rt[u], 1, n, x, v);
    ans += sz(rt[u]);
    // std::cout << u << ' ' << sz(rt[u]) << '\n';
    // if (u == 1) {
    //     std::cout << lc(ls(rt[u])) << ' ' << lc(rs(rt[u])) << '\n';
    //     std::cout << sz(ls(rt[u])) << ' ' << sz(rs(rt[u])) << '\n';
    //     // exit(0);
    // }
}

int main() {
    std::cin >> n >> m;
    for (int i = 1, u, v; i <= n - 1; i++) {
        std::cin >> u >> v;
        t[u].push_back(v);
        t[v].push_back(u);
    }
    dfs(1);
    init();
    while (m--) {
        int x, y;
        std::cin >> x >> y;
        int l = lca(x, y);
        // std::cout << x << ' ' << y << ' ' << l << '\n';
        op[x].emplace_back(dfn[x], 1);
        op[y].emplace_back(dfn[x], 1);
        op[l].emplace_back(dfn[x], -1);
        op[fa[l]].emplace_back(dfn[x], -1);

        op[x].emplace_back(dfn[y], 1);
        op[y].emplace_back(dfn[y], 1);
        op[l].emplace_back(dfn[y], -1);
        op[fa[l]].emplace_back(dfn[y], -1);
    }
    solve(1);
    std::cout << (ans - n) / 2 << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 14716kb

input:

297 297
281 46
39 260
193 121
50 162
18 15
43 104
154 293
131 121
70 112
247 121
17 59
240 102
277 185
90 121
134 38
46 116
27 127
97 7
33 49
24 297
31 286
12 18
57 121
133 150
183 286
186 194
153 121
117 189
256 121
266 192
106 230
80 249
164 73
211 197
264 4
53 47
203 43
286 69
221 146
214 167
142...

output:

15898

result:

wrong answer 1st lines differ - expected: '15910', found: '15898'

Test #2:

score: 0
Wrong Answer
time: 4ms
memory: 16712kb

input:

293 296
286 83
144 202
108 42
70 112
85 29
291 112
14 112
97 292
123 168
247 112
143 112
238 102
174 262
252 185
63 112
137 103
64 112
15 11
282 273
248 1
100 112
159 181
226 20
47 129
258 34
72 160
189 282
50 252
145 175
13 112
250 152
106 267
30 196
22 112
43 104
17 112
105 80
95 177
34 251
259 29...

output:

15220

result:

wrong answer 1st lines differ - expected: '15234', found: '15220'

Test #3:

score: 0
Wrong Answer
time: 14ms
memory: 21804kb

input:

4858 4888
3140 3181
1664 1770
4200 493
3671 59
3209 1842
2502 2173
681 2155
2654 3278
4174 2554
3282 587
3337 4086
4287 2326
779 3708
2048 2448
3634 3148
4214 59
2083 59
325 1216
351 878
1497 3495
4642 4242
115 4419
1588 2172
1054 4610
3007 59
4821 2816
2078 1195
782 2610
2559 3187
4576 292
4000 59
...

output:

3627117

result:

wrong answer 1st lines differ - expected: '3627298', found: '3627117'

Test #4:

score: 0
Wrong Answer
time: 16ms
memory: 19344kb

input:

4983 4867
1535 1610
1899 1266
3238 2426
2405 1420
2578 4364
300 2296
4590 254
396 1242
3129 3631
324 3583
3748 1155
2771 1200
4246 4603
2331 1110
2824 1891
2544 1237
3487 3040
3687 2834
109 4636
4687 1200
1649 2420
2008 2926
2516 4852
3440 1551
765 3096
2754 1200
4879 4820
3073 4787
91 1200
2911 257...

output:

3873691

result:

wrong answer 1st lines differ - expected: '3873876', found: '3873691'

Test #5:

score: 0
Wrong Answer
time: 424ms
memory: 46732kb

input:

96808 99508
40695 40696
73595 73596
13615 13616
65545 65546
19391 19392
2353 2354
26730 26731
42541 42542
28008 28009
96282 96283
76530 76531
5872 5873
61286 61287
56072 56073
87216 87217
38177 38178
50372 50373
329 330
58557 58558
43629 43630
77425 77426
8017 8018
43507 43508
35913 35914
31123 3112...

output:

907477206

result:

wrong answer 1st lines differ - expected: '907477207', found: '907477206'

Test #6:

score: 0
Wrong Answer
time: 415ms
memory: 47676kb

input:

97092 99840
40695 40696
73595 73596
13615 13616
65545 65546
19391 19392
2353 2354
26730 26731
42541 42542
28008 28009
96282 96283
76530 76531
5872 5873
61286 61287
56072 56073
87216 87217
38177 38178
50372 50373
329 330
58557 58558
43629 43630
77425 77426
8017 8018
43507 43508
35913 35914
31123 3112...

output:

910321603

result:

wrong answer 1st lines differ - expected: '910321604', found: '910321603'

Test #7:

score: 0
Wrong Answer
time: 691ms
memory: 111480kb

input:

99924 97275
24723 56564
44193 73644
54591 22570
56965 55653
91120 48245
25702 41871
77524 67327
80722 7653
87907 54339
86418 76838
41215 78202
48534 59032
44710 32114
38879 34955
17434 99405
37456 39328
66649 35177
76911 54339
40612 7834
70065 54339
29689 38076
48485 60166
50641 57751
97629 94661
47...

output:

1654597175

result:

wrong answer 1st lines differ - expected: '1654600973', found: '1654597175'

Test #8:

score: 0
Wrong Answer
time: 688ms
memory: 110772kb

input:

99498 96777
19744 18289
4605 93580
43036 31212
42702 45122
16545 73127
70905 51611
27911 61773
48069 83562
95567 11283
66495 45122
65856 15502
26003 58273
46733 45122
7043 7247
80433 55125
67878 88881
20197 45122
39212 80482
25962 45122
62830 45122
40173 39170
14714 82850
24093 66693
50604 61001
866...

output:

1503893662

result:

wrong answer 1st lines differ - expected: '1503897488', found: '1503893662'

Test #9:

score: 0
Wrong Answer
time: 726ms
memory: 111652kb

input:

99214 98195
8234 36494
37451 75094
31731 33067
19984 36494
52041 11042
4970 37696
26871 43916
87010 25881
37092 8701
75030 89583
4706 75209
24536 4378
37412 69085
88883 17123
79895 36494
95090 79450
95892 73714
61952 77454
81044 8337
68757 11047
73613 36494
60137 16120
41322 47605
49142 24141
79221 ...

output:

1594998142

result:

wrong answer 1st lines differ - expected: '1595001829', found: '1594998142'

Test #10:

score: 0
Wrong Answer
time: 708ms
memory: 111256kb

input:

98930 97863
31939 63734
42943 69741
55957 86464
76683 63177
19844 89880
92738 35520
42184 7020
98581 89880
73665 19084
91904 89880
32101 31332
16923 89880
64777 72859
39842 59094
18714 87378
5513 21706
54019 89880
16688 80816
1260 35635
83182 52628
32044 61306
26440 39530
77024 69004
20199 64299
434...

output:

1481947940

result:

wrong answer 1st lines differ - expected: '1481951671', found: '1481947940'