Submission #1001185

# Submission time Handle Problem Language Result Execution time Memory
1001185 2024-06-18T16:44:22 Z LonlyR Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 27220 KB
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()

using namespace std;
const int maxn = 1e5 + 5, bl = sqrt(maxn);
int n, m, q, cnt, cur;
int in[maxn], out[maxn], h[maxn], par[18][maxn], id[maxn], c[maxn], ans[maxn], in_set[maxn];
vector<int> adj[maxn];

struct cmp
{
    bool operator() (int x, int y) const
    {
        return in[x] < in[y];
    }
};

set<int, cmp> s;

struct Q{
    int l, r, id;
    bool operator < (Q b) const
    {
        if (l / bl != b.l / bl) return l < b.l;
        return (l / bl & 1) ? (r < b.r) : (r > b.r);
    }
} qr[maxn];

void dfs(int x = 1, int p = 1)
{
    par[0][x] = p;
    h[x] = h[p] + 1;
    for (int i = 1; i < 18; i++)
        par[i][x] = par[i - 1][par[i - 1][x]];
    in[x] = ++cnt;
    for (int i : adj[x]) if (i != p)
        dfs(i, x);
    out[x] = cnt;
}

inline bool sub(int u, int v) /// u in subtree of v
{
    return in[v] <= in[u] && out[u] <= out[v];
}

int lca(int u, int v)
{
    if (h[u] > h[v]) swap(u, v);
    if (sub(v, u)) return u;
    for (int i = 17; i >= 0; i--)
        if (sub(u, par[i][v]) == 0)
            v = par[i][v];
    return par[0][v];
}

int dist(int u, int v, int w = -1)
{
    if (w == -1) return h[u] + h[v] - h[lca(u, v)] * 2;
    return h[u] + h[v] - h[w] * 2;
}

void add(int x)
{
    x = c[x];
    in_set[x]++;
    if (in_set[x] == 1)
    {
        cur += h[x];
        if (s.size() == 0) return s.emplace(x), void();
        auto it = s.lower_bound(x);
        if (it == s.begin())
            cur -= h[lca(x, *it)];
        else if (it == s.end())
            cur -= h[lca(x, *prev(it))];
        else
        {
            auto u = *it, v = *prev(it);
            cur += h[lca(u, v)];
            cur -= h[lca(u, x)];
            cur -= h[lca(v, x)];
        }
    }
    s.emplace(x);
}

void del(int x)
{
    x = c[x];
    in_set[x]--;
    if (in_set[x] == 0)
    {
        cur -= h[x];
        if (s.size() == 1) return s.clear(), void();
        auto it = s.lower_bound(x);
        if (it == s.begin())
            cur += h[lca(x, *next(it))];
        else if (next(it) == s.end())
            cur += h[lca(x, *prev(it))];
        else
        {
            auto u = *prev(it), v = *next(it);
            cur -= h[lca(u, v)];
            cur += h[lca(u, x)];
            cur += h[lca(v, x)];
        }
        s.erase(it);
    }

}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
    cin >> n >> m >> q;
    for (int i = 1, u, v; i < n; i++)
        cin >> u >> v,
        adj[u].emplace_back(v),
        adj[v].emplace_back(u);
    for (int i = 1; i <= m; i++)
        cin >> c[i], id[i] = (i - 1) / bl + 1;
    dfs();
    for (int i = 1; i <= q; i++)
        cin >> qr[i].l >> qr[i].r, qr[i].id = i;
    sort(qr + 1, qr + q + 1);
    for (int i = 1, l = 1, r = 0; i <= q; i++)
    {
        auto [lx, rx, id] = qr[i];
        while (r < rx) add(++r);
        while (l > lx) add(--l);
        while (r > rx) del(r--);
        while (l < lx) del(l++);
        ans[id] = cur - h[lca(*s.begin(), *s.rbegin())] + 1;
    }
    for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 4 ms 10588 KB Output is correct
5 Correct 3 ms 10784 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 4 ms 10680 KB Output is correct
8 Correct 3 ms 10588 KB Output is correct
9 Correct 6 ms 10588 KB Output is correct
10 Correct 5 ms 10588 KB Output is correct
11 Correct 5 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 1 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 3 ms 10680 KB Output is correct
16 Correct 3 ms 10800 KB Output is correct
17 Correct 3 ms 10584 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 5 ms 10588 KB Output is correct
22 Correct 5 ms 10588 KB Output is correct
23 Correct 5 ms 10588 KB Output is correct
24 Correct 7 ms 10796 KB Output is correct
25 Correct 6 ms 10588 KB Output is correct
26 Correct 6 ms 10792 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 1 ms 10588 KB Output is correct
29 Correct 1 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 4 ms 10588 KB Output is correct
5 Correct 3 ms 10784 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 4 ms 10680 KB Output is correct
8 Correct 3 ms 10588 KB Output is correct
9 Correct 6 ms 10588 KB Output is correct
10 Correct 5 ms 10588 KB Output is correct
11 Correct 5 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 1 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 3 ms 10680 KB Output is correct
16 Correct 3 ms 10800 KB Output is correct
17 Correct 3 ms 10584 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 5 ms 10588 KB Output is correct
22 Correct 5 ms 10588 KB Output is correct
23 Correct 5 ms 10588 KB Output is correct
24 Correct 7 ms 10796 KB Output is correct
25 Correct 6 ms 10588 KB Output is correct
26 Correct 6 ms 10792 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 1 ms 10588 KB Output is correct
29 Correct 1 ms 10588 KB Output is correct
30 Correct 31 ms 10844 KB Output is correct
31 Correct 39 ms 10844 KB Output is correct
32 Correct 49 ms 10844 KB Output is correct
33 Correct 50 ms 10940 KB Output is correct
34 Correct 48 ms 10840 KB Output is correct
35 Correct 5 ms 10840 KB Output is correct
36 Correct 5 ms 10844 KB Output is correct
37 Correct 6 ms 10840 KB Output is correct
38 Correct 19 ms 11024 KB Output is correct
39 Correct 19 ms 10840 KB Output is correct
40 Correct 18 ms 10844 KB Output is correct
41 Correct 3 ms 10844 KB Output is correct
42 Correct 3 ms 11032 KB Output is correct
43 Correct 4 ms 10840 KB Output is correct
44 Correct 23 ms 10844 KB Output is correct
45 Correct 23 ms 10984 KB Output is correct
46 Correct 22 ms 10844 KB Output is correct
47 Correct 4 ms 10844 KB Output is correct
48 Correct 3 ms 10800 KB Output is correct
49 Correct 4 ms 10776 KB Output is correct
50 Correct 52 ms 10956 KB Output is correct
51 Correct 49 ms 10844 KB Output is correct
52 Correct 50 ms 10952 KB Output is correct
53 Correct 51 ms 10840 KB Output is correct
54 Correct 56 ms 10840 KB Output is correct
55 Correct 51 ms 10844 KB Output is correct
56 Correct 4 ms 10584 KB Output is correct
57 Correct 2 ms 10844 KB Output is correct
58 Correct 4 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 4 ms 10588 KB Output is correct
4 Correct 3933 ms 19028 KB Output is correct
5 Correct 2331 ms 23636 KB Output is correct
6 Correct 3698 ms 25680 KB Output is correct
7 Execution timed out 5058 ms 27220 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 115 ms 13820 KB Output is correct
3 Correct 171 ms 14168 KB Output is correct
4 Correct 134 ms 14680 KB Output is correct
5 Correct 96 ms 19548 KB Output is correct
6 Correct 123 ms 19024 KB Output is correct
7 Correct 154 ms 17536 KB Output is correct
8 Correct 163 ms 17132 KB Output is correct
9 Correct 158 ms 16732 KB Output is correct
10 Correct 145 ms 16724 KB Output is correct
11 Correct 195 ms 16852 KB Output is correct
12 Correct 205 ms 16720 KB Output is correct
13 Correct 201 ms 16976 KB Output is correct
14 Correct 184 ms 16996 KB Output is correct
15 Correct 162 ms 16988 KB Output is correct
16 Correct 180 ms 18512 KB Output is correct
17 Correct 172 ms 18516 KB Output is correct
18 Correct 169 ms 18516 KB Output is correct
19 Correct 73 ms 19536 KB Output is correct
20 Correct 85 ms 19756 KB Output is correct
21 Correct 92 ms 18264 KB Output is correct
22 Correct 106 ms 17564 KB Output is correct
23 Correct 103 ms 17236 KB Output is correct
24 Correct 119 ms 16724 KB Output is correct
25 Correct 130 ms 16724 KB Output is correct
26 Correct 145 ms 16732 KB Output is correct
27 Correct 212 ms 16732 KB Output is correct
28 Correct 256 ms 16980 KB Output is correct
29 Correct 269 ms 16720 KB Output is correct
30 Correct 260 ms 16892 KB Output is correct
31 Correct 252 ms 16976 KB Output is correct
32 Correct 242 ms 16984 KB Output is correct
33 Correct 235 ms 16980 KB Output is correct
34 Correct 211 ms 16984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 4 ms 10588 KB Output is correct
4 Execution timed out 5092 ms 16244 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 4 ms 10588 KB Output is correct
5 Correct 3 ms 10784 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 4 ms 10680 KB Output is correct
8 Correct 3 ms 10588 KB Output is correct
9 Correct 6 ms 10588 KB Output is correct
10 Correct 5 ms 10588 KB Output is correct
11 Correct 5 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 1 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 3 ms 10680 KB Output is correct
16 Correct 3 ms 10800 KB Output is correct
17 Correct 3 ms 10584 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 5 ms 10588 KB Output is correct
22 Correct 5 ms 10588 KB Output is correct
23 Correct 5 ms 10588 KB Output is correct
24 Correct 7 ms 10796 KB Output is correct
25 Correct 6 ms 10588 KB Output is correct
26 Correct 6 ms 10792 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 1 ms 10588 KB Output is correct
29 Correct 1 ms 10588 KB Output is correct
30 Correct 31 ms 10844 KB Output is correct
31 Correct 39 ms 10844 KB Output is correct
32 Correct 49 ms 10844 KB Output is correct
33 Correct 50 ms 10940 KB Output is correct
34 Correct 48 ms 10840 KB Output is correct
35 Correct 5 ms 10840 KB Output is correct
36 Correct 5 ms 10844 KB Output is correct
37 Correct 6 ms 10840 KB Output is correct
38 Correct 19 ms 11024 KB Output is correct
39 Correct 19 ms 10840 KB Output is correct
40 Correct 18 ms 10844 KB Output is correct
41 Correct 3 ms 10844 KB Output is correct
42 Correct 3 ms 11032 KB Output is correct
43 Correct 4 ms 10840 KB Output is correct
44 Correct 23 ms 10844 KB Output is correct
45 Correct 23 ms 10984 KB Output is correct
46 Correct 22 ms 10844 KB Output is correct
47 Correct 4 ms 10844 KB Output is correct
48 Correct 3 ms 10800 KB Output is correct
49 Correct 4 ms 10776 KB Output is correct
50 Correct 52 ms 10956 KB Output is correct
51 Correct 49 ms 10844 KB Output is correct
52 Correct 50 ms 10952 KB Output is correct
53 Correct 51 ms 10840 KB Output is correct
54 Correct 56 ms 10840 KB Output is correct
55 Correct 51 ms 10844 KB Output is correct
56 Correct 4 ms 10584 KB Output is correct
57 Correct 2 ms 10844 KB Output is correct
58 Correct 4 ms 10844 KB Output is correct
59 Correct 1 ms 10588 KB Output is correct
60 Correct 2 ms 10588 KB Output is correct
61 Correct 4 ms 10588 KB Output is correct
62 Correct 3933 ms 19028 KB Output is correct
63 Correct 2331 ms 23636 KB Output is correct
64 Correct 3698 ms 25680 KB Output is correct
65 Execution timed out 5058 ms 27220 KB Time limit exceeded
66 Halted 0 ms 0 KB -