Submission #1001188

# Submission time Handle Problem Language Result Execution time Memory
1001188 2024-06-18T16:48:54 Z LonlyR Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 42732 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];
}

map<pair<int,int>,int> mp;

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

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 10588 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 4 ms 10588 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 4 ms 10588 KB Output is correct
8 Correct 3 ms 10820 KB Output is correct
9 Correct 6 ms 10860 KB Output is correct
10 Correct 6 ms 10824 KB Output is correct
11 Correct 8 ms 10844 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 6 ms 10844 KB Output is correct
16 Correct 6 ms 10844 KB Output is correct
17 Correct 6 ms 10840 KB Output is correct
18 Correct 7 ms 10844 KB Output is correct
19 Correct 7 ms 10840 KB Output is correct
20 Correct 6 ms 10844 KB Output is correct
21 Correct 8 ms 10844 KB Output is correct
22 Correct 6 ms 10828 KB Output is correct
23 Correct 6 ms 10844 KB Output is correct
24 Correct 6 ms 11096 KB Output is correct
25 Correct 6 ms 10844 KB Output is correct
26 Correct 6 ms 10844 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 1 ms 10588 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 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 4 ms 10588 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 4 ms 10588 KB Output is correct
8 Correct 3 ms 10820 KB Output is correct
9 Correct 6 ms 10860 KB Output is correct
10 Correct 6 ms 10824 KB Output is correct
11 Correct 8 ms 10844 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 6 ms 10844 KB Output is correct
16 Correct 6 ms 10844 KB Output is correct
17 Correct 6 ms 10840 KB Output is correct
18 Correct 7 ms 10844 KB Output is correct
19 Correct 7 ms 10840 KB Output is correct
20 Correct 6 ms 10844 KB Output is correct
21 Correct 8 ms 10844 KB Output is correct
22 Correct 6 ms 10828 KB Output is correct
23 Correct 6 ms 10844 KB Output is correct
24 Correct 6 ms 11096 KB Output is correct
25 Correct 6 ms 10844 KB Output is correct
26 Correct 6 ms 10844 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 1 ms 10588 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 47 ms 11356 KB Output is correct
31 Correct 55 ms 11348 KB Output is correct
32 Correct 81 ms 11604 KB Output is correct
33 Correct 80 ms 11604 KB Output is correct
34 Correct 88 ms 11644 KB Output is correct
35 Correct 6 ms 11100 KB Output is correct
36 Correct 6 ms 11100 KB Output is correct
37 Correct 6 ms 11128 KB Output is correct
38 Correct 74 ms 11696 KB Output is correct
39 Correct 72 ms 11760 KB Output is correct
40 Correct 74 ms 11748 KB Output is correct
41 Correct 7 ms 11100 KB Output is correct
42 Correct 6 ms 11100 KB Output is correct
43 Correct 6 ms 11100 KB Output is correct
44 Correct 78 ms 11564 KB Output is correct
45 Correct 75 ms 11536 KB Output is correct
46 Correct 78 ms 11604 KB Output is correct
47 Correct 5 ms 11096 KB Output is correct
48 Correct 6 ms 11100 KB Output is correct
49 Correct 6 ms 11024 KB Output is correct
50 Correct 79 ms 11708 KB Output is correct
51 Correct 70 ms 11600 KB Output is correct
52 Correct 73 ms 11600 KB Output is correct
53 Correct 75 ms 11600 KB Output is correct
54 Correct 75 ms 11600 KB Output is correct
55 Correct 75 ms 11628 KB Output is correct
56 Correct 4 ms 10584 KB Output is correct
57 Correct 2 ms 10840 KB Output is correct
58 Correct 7 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 3 ms 10588 KB Output is correct
4 Execution timed out 5095 ms 33860 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 279 ms 27616 KB Output is correct
3 Correct 478 ms 35832 KB Output is correct
4 Correct 349 ms 31136 KB Output is correct
5 Correct 198 ms 27472 KB Output is correct
6 Correct 255 ms 29520 KB Output is correct
7 Correct 371 ms 32184 KB Output is correct
8 Correct 414 ms 34132 KB Output is correct
9 Correct 418 ms 35128 KB Output is correct
10 Correct 405 ms 35408 KB Output is correct
11 Correct 541 ms 38740 KB Output is correct
12 Correct 586 ms 41808 KB Output is correct
13 Correct 543 ms 42732 KB Output is correct
14 Correct 490 ms 41040 KB Output is correct
15 Correct 435 ms 36480 KB Output is correct
16 Correct 444 ms 38908 KB Output is correct
17 Correct 459 ms 38992 KB Output is correct
18 Correct 458 ms 38956 KB Output is correct
19 Correct 152 ms 27472 KB Output is correct
20 Correct 193 ms 28748 KB Output is correct
21 Correct 289 ms 30800 KB Output is correct
22 Correct 334 ms 32332 KB Output is correct
23 Correct 357 ms 33620 KB Output is correct
24 Correct 417 ms 34568 KB Output is correct
25 Correct 429 ms 34904 KB Output is correct
26 Correct 401 ms 35160 KB Output is correct
27 Correct 519 ms 36688 KB Output is correct
28 Correct 562 ms 38752 KB Output is correct
29 Correct 606 ms 40788 KB Output is correct
30 Correct 591 ms 42408 KB Output is correct
31 Correct 586 ms 42712 KB Output is correct
32 Correct 546 ms 42064 KB Output is correct
33 Correct 532 ms 38832 KB Output is correct
34 Correct 446 ms 36436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 3 ms 10588 KB Output is correct
3 Correct 4 ms 10588 KB Output is correct
4 Execution timed out 5063 ms 30052 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 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 4 ms 10588 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 4 ms 10588 KB Output is correct
8 Correct 3 ms 10820 KB Output is correct
9 Correct 6 ms 10860 KB Output is correct
10 Correct 6 ms 10824 KB Output is correct
11 Correct 8 ms 10844 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 6 ms 10844 KB Output is correct
16 Correct 6 ms 10844 KB Output is correct
17 Correct 6 ms 10840 KB Output is correct
18 Correct 7 ms 10844 KB Output is correct
19 Correct 7 ms 10840 KB Output is correct
20 Correct 6 ms 10844 KB Output is correct
21 Correct 8 ms 10844 KB Output is correct
22 Correct 6 ms 10828 KB Output is correct
23 Correct 6 ms 10844 KB Output is correct
24 Correct 6 ms 11096 KB Output is correct
25 Correct 6 ms 10844 KB Output is correct
26 Correct 6 ms 10844 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 1 ms 10588 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 47 ms 11356 KB Output is correct
31 Correct 55 ms 11348 KB Output is correct
32 Correct 81 ms 11604 KB Output is correct
33 Correct 80 ms 11604 KB Output is correct
34 Correct 88 ms 11644 KB Output is correct
35 Correct 6 ms 11100 KB Output is correct
36 Correct 6 ms 11100 KB Output is correct
37 Correct 6 ms 11128 KB Output is correct
38 Correct 74 ms 11696 KB Output is correct
39 Correct 72 ms 11760 KB Output is correct
40 Correct 74 ms 11748 KB Output is correct
41 Correct 7 ms 11100 KB Output is correct
42 Correct 6 ms 11100 KB Output is correct
43 Correct 6 ms 11100 KB Output is correct
44 Correct 78 ms 11564 KB Output is correct
45 Correct 75 ms 11536 KB Output is correct
46 Correct 78 ms 11604 KB Output is correct
47 Correct 5 ms 11096 KB Output is correct
48 Correct 6 ms 11100 KB Output is correct
49 Correct 6 ms 11024 KB Output is correct
50 Correct 79 ms 11708 KB Output is correct
51 Correct 70 ms 11600 KB Output is correct
52 Correct 73 ms 11600 KB Output is correct
53 Correct 75 ms 11600 KB Output is correct
54 Correct 75 ms 11600 KB Output is correct
55 Correct 75 ms 11628 KB Output is correct
56 Correct 4 ms 10584 KB Output is correct
57 Correct 2 ms 10840 KB Output is correct
58 Correct 7 ms 11100 KB Output is correct
59 Correct 1 ms 10584 KB Output is correct
60 Correct 2 ms 10584 KB Output is correct
61 Correct 3 ms 10588 KB Output is correct
62 Execution timed out 5095 ms 33860 KB Time limit exceeded
63 Halted 0 ms 0 KB -