Submission #950837

# Submission time Handle Problem Language Result Execution time Memory
950837 2024-03-20T19:11:18 Z vjudge1 Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 34472 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ld = long double;  // or double, if TL is tight
using str = string; 
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;

struct AINT {
    int n;
    vector<ii> V; /// (minim, cnt)
    vi Lz;

    ii join(ii a, ii b) {
        if(a.first == b.first)
            return {a.first, a.second + b.second};
        return min(a, b);
    }

    AINT(int N) : n(N), V(4 * N), Lz(4 * N) {}

    void init(int u, int s, int d) {
        if(s == d) {
            V[u] = {0, 1};
            return;
        }
        init(u << 1, s, (s + d) >> 1);
        init(u << 1 | 1, ((s + d) >> 1) + 1, d);
        V[u] = join(V[u << 1], V[u << 1 | 1]);
    }

    void init() { init(1, 0, n - 1); }

    void propag(int u, int s, int d) {
        if(!Lz[u]) return;
        V[u].first += Lz[u];
        if(s != d) {
            Lz[u << 1] += Lz[u];
            Lz[u << 1 | 1] += Lz[u];
        }
        Lz[u] = 0;
    }

    void update(int l, int r, int v, int u, int s, int d) {
        propag(u, s, d);
        if(r < s || d < l) return;
        if(l <= s && d <= r) {
            Lz[u] += v;
            propag(u, s, d);
            return;
        }
        update(l, r, v, u << 1, s, (s + d) >> 1);
        update(l, r, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
        V[u] = join(V[u << 1], V[u << 1 | 1]);
    }

    void update(int l, int r, int v) { update(l, r, v, 1, 0, n - 1); }

    int query() { /// nr de 0 uri
        if(V[1].first == 0) return V[1].second;
        return 0;
    }
};

struct tree {
    int n, tmr = 0;
    vi par, niv, sz, nxt, in;
    vector<vi> L, G, Anc;

    AINT A;

    tree(int N) : n(N), par(N), niv(N), sz(N, 0), 
    nxt(N, 0), in(N, 0), L(N), G(N), A(N) {} /// nxt[u] e capul la lant

    void add_edge(int u, int v) {
        L[u].push_back(v);
        L[v].push_back(u);
    }

    void init() {
        function<void(int, int, int)> dfs0 = [&](int u, int p, int li) {
            par[u] = p;
            niv[u] = li;
            sz[u] = 1;
            for (auto it : L[u])
                if (it != p) {
                    dfs0(it, u, li + 1);
                    sz[u] += it;
                    G[u].push_back(it);
                }
        };
        dfs0(0, -1, 0);

        A.init();

        function<void(int, int)> dfs1 = [&](int u, int p) {
            sort(all(G[u]), [&](auto a, auto b) { return sz[a] > sz[b]; });
            in[u] = tmr++;
            if (G[u].empty()) return;
            nxt[G[u][0]] = nxt[u];
            dfs1(G[u][0], u);
            for (int i = 1; i < G[u].size(); ++i) {
                nxt[G[u][i]] = G[u][i];
                dfs1(G[u][i], u);
            }
        };
        dfs1(0, -1);
        
        Anc.push_back(par);
        for (int k = 1; (1 << k) <= n; ++k) {
            Anc.push_back(vi(n, -1));
            for (int i = 0; i < n; ++i) {
                Anc[k][i] = Anc[k - 1][i] == -1 ? -1 : Anc[k - 1][Anc[k - 1][i]];
            }
        }
    }

    int lift(int u, int nr) {
        for (int i = 0; i < (int)Anc.size(); ++i)
            if (nr & (1 << i))
                u = Anc[i][u];
        return u;
    }

    int lca(int u, int v) {
        if(niv[u] > niv[v]) swap(u, v);
        v = lift(v, niv[v] - niv[u]);
        if(u == v) return u;
        for(int i = (int)Anc.size() - 1; i >= 0; --i)
            if(Anc[i][u] != Anc[i][v]) {
                u = Anc[i][u];
                v = Anc[i][v];
            }
        return par[u];
    }

    void update(int u, int v) {
        while(u != -1) {
            A.update(min(in[u], in[nxt[u]]), max(in[u], in[nxt[u]]), v);
            u = par[nxt[u]];
        }
    }

    int query() { /// nr pornite
        return n - A.query();
    }
};

struct RMQ_LCA {
    int m;
    tree &T;
    vi C;
    vector<vi> Re;

    RMQ_LCA(int N, vi C0, tree &T0) : m(C0.size()), C(C0), T(T0) {
        Re.push_back(C);
        for(int k = 1; (1 << k) <= m; ++k) {
            Re.push_back(vi(m, 0));
            for(int i = 0; i < m; ++i) {
                if(i + (1 << (k - 1)) < m)
                    Re[k][i] = T.lca(Re[k - 1][i], Re[k - 1][i + (1 << (k - 1))]);
                else Re[k][i] = Re[k - 1][i];
            }
        }
    }

    int query(int st, int dr) {
        int l = 31 - __builtin_clz(dr - st + 1);
        return T.lca(Re[l][st], Re[l][dr - (1 << l) + 1]);
    }
};

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int n, m, q;
    cin >> n >> m >> q;
    tree T(n);
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        T.add_edge(u, v);
    }
    T.init();
    vi C(m);
    for(int i = 0; i < m; ++i) {
        cin >> C[i];
        --C[i];
    }

    RMQ_LCA Stramos(n, C, T);

    int rad = (int)round(sqrt(double(n)));
    vector<pair<ii, int> > Q;
    vi Re(q, 0);
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l; --r;
        Q.push_back({ii{l, r}, i});
    }
    sort(all(Q), [&](auto a, auto b) {
        auto [l1, r1] = a.first;
        auto [l2, r2] = b.first;
        if(l1 / rad != l2 / rad) return l1 < l2;
        return r1 < r2;
    });
    int st = 1, dr = 0;
    for(auto [seg, id] : Q) {
        auto [l, r] = seg;
        auto add = [&](int p) {
            T.update(C[p], 1);
        };
        auto remove = [&](int p) {
            T.update(C[p], -1);
        };
        while(st > l) add(--st);
        while(st < l) remove(st++);
        while(dr < r) add(++dr);
        while(dr > r) remove(dr--);
        Re[id] = T.query() - T.niv[Stramos.query(l, r)];
    }

    for(auto it : Re)
        cout << it << "\n";
    return 0;
}

Compilation message

tourism.cpp: In lambda function:
tourism.cpp:110:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |             for (int i = 1; i < G[u].size(); ++i) {
      |                             ~~^~~~~~~~~~~~~
tourism.cpp: In constructor 'RMQ_LCA::RMQ_LCA(int, vi, tree&)':
tourism.cpp:160:8: warning: 'RMQ_LCA::C' will be initialized after [-Wreorder]
  160 |     vi C;
      |        ^
tourism.cpp:159:11: warning:   'tree& RMQ_LCA::T' [-Wreorder]
  159 |     tree &T;
      |           ^
tourism.cpp:163:5: warning:   when initialized here [-Wreorder]
  163 |     RMQ_LCA(int N, vi C0, tree &T0) : m(C0.size()), C(C0), T(T0) {
      |     ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 3 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 564 KB Output is correct
16 Correct 2 ms 1012 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 3 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 3 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 564 KB Output is correct
16 Correct 2 ms 1012 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 3 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 35 ms 884 KB Output is correct
31 Correct 49 ms 864 KB Output is correct
32 Correct 61 ms 860 KB Output is correct
33 Correct 105 ms 1000 KB Output is correct
34 Correct 76 ms 860 KB Output is correct
35 Correct 13 ms 856 KB Output is correct
36 Correct 20 ms 860 KB Output is correct
37 Correct 17 ms 860 KB Output is correct
38 Correct 33 ms 1168 KB Output is correct
39 Correct 28 ms 1112 KB Output is correct
40 Correct 39 ms 1112 KB Output is correct
41 Correct 6 ms 1112 KB Output is correct
42 Correct 5 ms 1112 KB Output is correct
43 Correct 8 ms 1116 KB Output is correct
44 Correct 65 ms 1116 KB Output is correct
45 Correct 63 ms 860 KB Output is correct
46 Correct 76 ms 1116 KB Output is correct
47 Correct 13 ms 1116 KB Output is correct
48 Correct 12 ms 1112 KB Output is correct
49 Correct 22 ms 1116 KB Output is correct
50 Correct 34 ms 1112 KB Output is correct
51 Correct 28 ms 860 KB Output is correct
52 Correct 28 ms 860 KB Output is correct
53 Correct 29 ms 856 KB Output is correct
54 Correct 31 ms 860 KB Output is correct
55 Correct 28 ms 1016 KB Output is correct
56 Correct 4 ms 600 KB Output is correct
57 Correct 2 ms 860 KB Output is correct
58 Correct 5 ms 1008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 4 ms 600 KB Output is correct
4 Execution timed out 5026 ms 29380 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 205 ms 16744 KB Output is correct
3 Correct 372 ms 20820 KB Output is correct
4 Correct 345 ms 21284 KB Output is correct
5 Correct 258 ms 31144 KB Output is correct
6 Correct 384 ms 31164 KB Output is correct
7 Correct 424 ms 31056 KB Output is correct
8 Correct 405 ms 31200 KB Output is correct
9 Correct 407 ms 31120 KB Output is correct
10 Correct 441 ms 31160 KB Output is correct
11 Correct 403 ms 31056 KB Output is correct
12 Correct 622 ms 31056 KB Output is correct
13 Correct 422 ms 31180 KB Output is correct
14 Correct 438 ms 31688 KB Output is correct
15 Correct 552 ms 32716 KB Output is correct
16 Correct 550 ms 31160 KB Output is correct
17 Correct 410 ms 31204 KB Output is correct
18 Correct 418 ms 31244 KB Output is correct
19 Correct 491 ms 32588 KB Output is correct
20 Correct 664 ms 32780 KB Output is correct
21 Correct 660 ms 32604 KB Output is correct
22 Correct 859 ms 32596 KB Output is correct
23 Correct 847 ms 32596 KB Output is correct
24 Correct 1006 ms 32592 KB Output is correct
25 Correct 841 ms 32596 KB Output is correct
26 Correct 957 ms 32732 KB Output is correct
27 Correct 942 ms 32608 KB Output is correct
28 Correct 906 ms 32596 KB Output is correct
29 Correct 912 ms 33028 KB Output is correct
30 Correct 991 ms 32608 KB Output is correct
31 Correct 887 ms 32716 KB Output is correct
32 Correct 688 ms 33080 KB Output is correct
33 Correct 675 ms 33476 KB Output is correct
34 Correct 1192 ms 34472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 5 ms 600 KB Output is correct
4 Execution timed out 5029 ms 22632 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 3 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 564 KB Output is correct
16 Correct 2 ms 1012 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 3 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 35 ms 884 KB Output is correct
31 Correct 49 ms 864 KB Output is correct
32 Correct 61 ms 860 KB Output is correct
33 Correct 105 ms 1000 KB Output is correct
34 Correct 76 ms 860 KB Output is correct
35 Correct 13 ms 856 KB Output is correct
36 Correct 20 ms 860 KB Output is correct
37 Correct 17 ms 860 KB Output is correct
38 Correct 33 ms 1168 KB Output is correct
39 Correct 28 ms 1112 KB Output is correct
40 Correct 39 ms 1112 KB Output is correct
41 Correct 6 ms 1112 KB Output is correct
42 Correct 5 ms 1112 KB Output is correct
43 Correct 8 ms 1116 KB Output is correct
44 Correct 65 ms 1116 KB Output is correct
45 Correct 63 ms 860 KB Output is correct
46 Correct 76 ms 1116 KB Output is correct
47 Correct 13 ms 1116 KB Output is correct
48 Correct 12 ms 1112 KB Output is correct
49 Correct 22 ms 1116 KB Output is correct
50 Correct 34 ms 1112 KB Output is correct
51 Correct 28 ms 860 KB Output is correct
52 Correct 28 ms 860 KB Output is correct
53 Correct 29 ms 856 KB Output is correct
54 Correct 31 ms 860 KB Output is correct
55 Correct 28 ms 1016 KB Output is correct
56 Correct 4 ms 600 KB Output is correct
57 Correct 2 ms 860 KB Output is correct
58 Correct 5 ms 1008 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 4 ms 600 KB Output is correct
62 Execution timed out 5026 ms 29380 KB Time limit exceeded
63 Halted 0 ms 0 KB -