Submission #524563

# Submission time Handle Problem Language Result Execution time Memory
524563 2022-02-09T13:50:24 Z mathking1021 JOI15_road_development (JOI15_road_development) C++17
100 / 100
685 ms 48352 KB
#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;
typedef long long ll;

ll n, Q;
ll a[300005], b[300005], c[300005], d[300005];
ll cp[100005];
ll ans[300005];
vector<ll> ve[100005];
ll e[25][100005];
ll h[100005];
ll cp2[100005];
ll cnt = 1;
ll f[100005], g[100005];
ll base = 131072;
ll seg[400005];

ll fnd(ll p)
{
    if(cp[p] == p) return p;
    return cp[p] = fnd(cp[p]);
}

ll fnd2(ll p)
{
    if(cp2[p] == p) return p;
    return cp2[p] = fnd2(cp2[p]);
}

void dfs(ll p, ll q)
{
    if(e[0][p]) return;
    e[0][p] = q;
    f[p] = cnt, cnt++;
    if(p != q) h[p] = h[q] + 1;
    for(ll i = 0; i < ve[p].size(); i++)
    {
        dfs(ve[p][i], p);
    }
    g[p] = cnt - 1;
}

ll lca(ll p, ll q)
{
    if(h[p] > h[q]) swap(p, q);
    ll t1 = h[q] - h[p];
    for(ll i = 20; i >= 0; i--)
    {
        if(t1 & (1 << i)) q = e[i][q];
    }
    for(ll i = 20; i >= 0; i--)
    {
        if(e[i][p] == e[i][q]) continue;
        p = e[i][p], q = e[i][q];
    }
    if(p != q) p = e[0][p], q = e[0][q];
    return p;
}

void upd2(ll p, ll q)
{
    p += base;
    while(p >= 1)
    {
        seg[p] += q;
        p /= 2;
    }
}

void upd(ll p)
{
    ll t1 = f[p];
    ll t2 = g[p];
    t2++;
    upd2(t1, -1);
    upd2(t2, 1);
}

ll que(ll p, ll q)
{
//    p = f[p];
    q = f[q];
    p += base;
    q += base;
    ll re = 0;
    while(p < q)
    {
        if(p % 2 == 1) re += seg[p], p++;
        if(q % 2 == 0) re += seg[q], q--;
        p /= 2, q /= 2;
    }
    if(p == q) re += seg[p];
    return re;
}

int main()
{
    scanf("%lld%lld", &n, &Q);
    for(ll i = 1; i <= n; i++) cp[i] = cp2[i] = i;
    for(ll i = 0; i < Q; i++)
    {
        scanf("%lld%lld%lld", &a[i], &b[i], &c[i]);
        if(a[i] == 1)
        {
            if(fnd(b[i]) == fnd(c[i]))
            {
                d[i] = 1;
            }
            else
            {
                cp[fnd(b[i])] = fnd(c[i]);
                ve[b[i]].push_back(c[i]);
                ve[c[i]].push_back(b[i]);
            }
        }
        else
        {
            if(fnd(b[i]) != fnd(c[i])) ans[i] = -1;
        }
    }
    for(ll i = 1; i <= n; i++)
    {
        if(e[0][i] > 0) continue;
        dfs(i, i);
    }
    for(ll i = 1; i <= n; i++)
    {
        seg[f[i] + base] += h[i];
        seg[f[i] + base + 1] -= h[i];
    }
    for(ll i = base - 1; i >= 1; i--)
    {
        seg[i] = seg[i * 2] + seg[i * 2 + 1];
    }
    for(ll i = 1; i <= 20; i++)
    {
        for(ll j = 1; j <= n; j++)
        {
            e[i][j] = e[i - 1][e[i - 1][j]];
        }
    }
    for(ll i = 0; i < Q; i++)
    {
//        ll tttttt1 = 1;
//        if(i == 1355)
//        {
//            tttttt1 = 2;
//        }
        if(a[i] == 1)
        {
            if(d[i] == 1)
            {
                ll t1 = b[i];
                ll t2 = c[i];
                ll t3 = lca(b[i], c[i]);
                t1 = fnd2(t1);
                t2 = fnd2(t2);
                while(h[t1] > h[t3])
                {
//                    if(t1 == 777) return -1;
                    upd(t1);
                    cp2[fnd2(t1)] = e[0][t1];
                    t1 = fnd2(t1);
                }
                while(h[t2] > h[t3])
                {
//                    if(t2 == 777) return -1;
                    upd(t2);
                    cp2[fnd2(t2)] = e[0][t2];
                    t2 = fnd2(t2);
                }
            }
        }
        else
        {
            if(ans[i] != -1)
            {
                ll t1 = b[i];
                ll t2 = c[i];
                ll t3 = lca(b[i], c[i]);
                ll t6 = que(0, t1);
                ll t4 = que(0, t1) - que(0, t3);
                ll t5 = que(0, t2) - que(0, t3);
//                if(i == 1408) printf("%lld %lld %lld %lld %lld %lld\n", t1, t2, t3, t4, t5, t6);
                ans[i] = t4 + t5;
            }
        }
    }
    for(ll i = 0; i < Q; i++)
    {
        if(a[i] == 2)
        {
//            printf("%lld %lld\n", ans[i], i);
            printf("%lld\n", ans[i]);
        }
    }
    return 0;
}

Compilation message

road_development.cpp: In function 'void dfs(ll, ll)':
road_development.cpp:39:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for(ll i = 0; i < ve[p].size(); i++)
      |                   ~~^~~~~~~~~~~~~~
road_development.cpp: In function 'int main()':
road_development.cpp:184:20: warning: unused variable 't6' [-Wunused-variable]
  184 |                 ll t6 = que(0, t1);
      |                    ^~
road_development.cpp:101:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |     scanf("%lld%lld", &n, &Q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~
road_development.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         scanf("%lld%lld%lld", &a[i], &b[i], &c[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4172 KB Output is correct
2 Correct 3 ms 4172 KB Output is correct
3 Correct 4 ms 4172 KB Output is correct
4 Correct 4 ms 4228 KB Output is correct
5 Correct 4 ms 4216 KB Output is correct
6 Correct 4 ms 4200 KB Output is correct
7 Correct 4 ms 4200 KB Output is correct
8 Correct 5 ms 4268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 627 ms 42400 KB Output is correct
2 Correct 565 ms 42084 KB Output is correct
3 Correct 685 ms 45160 KB Output is correct
4 Correct 596 ms 45068 KB Output is correct
5 Correct 599 ms 44204 KB Output is correct
6 Correct 574 ms 43660 KB Output is correct
7 Correct 593 ms 42212 KB Output is correct
8 Correct 621 ms 43648 KB Output is correct
9 Correct 528 ms 41320 KB Output is correct
10 Correct 540 ms 42436 KB Output is correct
11 Correct 594 ms 44264 KB Output is correct
12 Correct 517 ms 46020 KB Output is correct
13 Correct 582 ms 45612 KB Output is correct
14 Correct 594 ms 43864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 595 ms 44600 KB Output is correct
2 Correct 589 ms 44500 KB Output is correct
3 Correct 595 ms 46808 KB Output is correct
4 Correct 571 ms 47648 KB Output is correct
5 Correct 372 ms 44640 KB Output is correct
6 Correct 635 ms 46572 KB Output is correct
7 Correct 268 ms 44564 KB Output is correct
8 Correct 591 ms 45944 KB Output is correct
9 Correct 482 ms 45520 KB Output is correct
10 Correct 610 ms 47008 KB Output is correct
11 Correct 551 ms 43744 KB Output is correct
12 Correct 654 ms 47048 KB Output is correct
13 Correct 614 ms 46532 KB Output is correct
14 Correct 527 ms 44376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 315 ms 38124 KB Output is correct
2 Correct 315 ms 38140 KB Output is correct
3 Correct 322 ms 40008 KB Output is correct
4 Correct 323 ms 40748 KB Output is correct
5 Correct 494 ms 42180 KB Output is correct
6 Correct 576 ms 45220 KB Output is correct
7 Correct 80 ms 33508 KB Output is correct
8 Correct 86 ms 35680 KB Output is correct
9 Correct 111 ms 37004 KB Output is correct
10 Correct 121 ms 38260 KB Output is correct
11 Correct 408 ms 42244 KB Output is correct
12 Correct 540 ms 44188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4172 KB Output is correct
2 Correct 3 ms 4172 KB Output is correct
3 Correct 4 ms 4172 KB Output is correct
4 Correct 4 ms 4228 KB Output is correct
5 Correct 4 ms 4216 KB Output is correct
6 Correct 4 ms 4200 KB Output is correct
7 Correct 4 ms 4200 KB Output is correct
8 Correct 5 ms 4268 KB Output is correct
9 Correct 627 ms 42400 KB Output is correct
10 Correct 565 ms 42084 KB Output is correct
11 Correct 685 ms 45160 KB Output is correct
12 Correct 596 ms 45068 KB Output is correct
13 Correct 599 ms 44204 KB Output is correct
14 Correct 574 ms 43660 KB Output is correct
15 Correct 593 ms 42212 KB Output is correct
16 Correct 621 ms 43648 KB Output is correct
17 Correct 528 ms 41320 KB Output is correct
18 Correct 540 ms 42436 KB Output is correct
19 Correct 594 ms 44264 KB Output is correct
20 Correct 517 ms 46020 KB Output is correct
21 Correct 582 ms 45612 KB Output is correct
22 Correct 594 ms 43864 KB Output is correct
23 Correct 595 ms 44600 KB Output is correct
24 Correct 589 ms 44500 KB Output is correct
25 Correct 595 ms 46808 KB Output is correct
26 Correct 571 ms 47648 KB Output is correct
27 Correct 372 ms 44640 KB Output is correct
28 Correct 635 ms 46572 KB Output is correct
29 Correct 268 ms 44564 KB Output is correct
30 Correct 591 ms 45944 KB Output is correct
31 Correct 482 ms 45520 KB Output is correct
32 Correct 610 ms 47008 KB Output is correct
33 Correct 551 ms 43744 KB Output is correct
34 Correct 654 ms 47048 KB Output is correct
35 Correct 614 ms 46532 KB Output is correct
36 Correct 527 ms 44376 KB Output is correct
37 Correct 315 ms 38124 KB Output is correct
38 Correct 315 ms 38140 KB Output is correct
39 Correct 322 ms 40008 KB Output is correct
40 Correct 323 ms 40748 KB Output is correct
41 Correct 494 ms 42180 KB Output is correct
42 Correct 576 ms 45220 KB Output is correct
43 Correct 80 ms 33508 KB Output is correct
44 Correct 86 ms 35680 KB Output is correct
45 Correct 111 ms 37004 KB Output is correct
46 Correct 121 ms 38260 KB Output is correct
47 Correct 408 ms 42244 KB Output is correct
48 Correct 540 ms 44188 KB Output is correct
49 Correct 588 ms 44628 KB Output is correct
50 Correct 498 ms 44504 KB Output is correct
51 Correct 669 ms 47312 KB Output is correct
52 Correct 634 ms 48352 KB Output is correct
53 Correct 453 ms 42464 KB Output is correct
54 Correct 326 ms 42568 KB Output is correct
55 Correct 480 ms 44744 KB Output is correct
56 Correct 536 ms 45508 KB Output is correct
57 Correct 572 ms 45008 KB Output is correct
58 Correct 546 ms 44412 KB Output is correct
59 Correct 587 ms 44428 KB Output is correct
60 Correct 464 ms 44612 KB Output is correct
61 Correct 557 ms 45044 KB Output is correct