Submission #850643

# Submission time Handle Problem Language Result Execution time Memory
850643 2023-09-17T08:20:27 Z danikoynov Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 39788 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 1e5 + 10;

struct query
{
    int l, r, idx;
} task[maxn];

int n, m, c[maxn], q;
vector < int > adj[maxn];

void input()
{
    cin >> n >> m >> q;
    for (int i = 1; i < n; i ++)
    {
        int v, u;
        cin >> v >> u;
        adj[v].push_back(u);
        adj[u].push_back(v);
    }

    for (int i = 1; i <= m; i ++)
    {
        cin >> c[i];
    }

    for (int i = 1; i <= q; i ++)
    {
        cin >> task[i].l >> task[i].r;
        task[i].idx = i;
    }
}


int depth[maxn], tin[maxn], tout[maxn];
int occ[2 * maxn], rev[2 * maxn], timer;

void euler(int v = 1, int p = - 1)
{
    occ[++ timer] = v;
    tin[v] = timer;
    rev[timer] = v;
    for (int u : adj[v])
    {
        if (u == p)
            continue;
        depth[u] = depth[v] + 1;
        euler(u, v);
        occ[++ timer] = v;
    }
    tout[v] = timer;
}

const int maxlog = 20;

int lg[2 * maxn], dp[maxlog][2 * maxn];

void sparse_table()
{
    for (int i = 1; i <= timer; i ++)
    {
        lg[i] = lg[i / 2] + 1;
        dp[0][i] = occ[i];
    }

    for (int j = 1; j < lg[timer]; j ++)
        for (int i = 1; i <= timer - (1 << j); i ++)
        {
            dp[j][i] = dp[j - 1][i + (1 << (j - 1))];
            if (depth[dp[j - 1][i]] < depth[dp[j][i]])
                dp[j][i] = dp[j - 1][i];
        }
}

int get_lca(int v, int u)
{
    int l = tin[v], r = tin[u];
    if (l > r)
        swap(l, r);
    int len = lg[r - l + 1] - 1;
    int lca = dp[len][r - (1 << len) + 1];
    if (depth[dp[len][l]] < depth[lca])
        lca = dp[len][l];
    return lca;
}

int get_distance(int v, int u)
{
    return depth[v] + depth[u] - 2 * depth[get_lca(v, u)];
}

/**bool cmp_tin(int v, int u)
{
    return tin[v] < tin[u];
}
void solve_query(int lf, int rf)
{
    vector < int > ord;
    int global_lca = c[lf];
    for (int i = lf; i <= rf; i ++)
    {
        ord.push_back(c[i]);
        global_lca = get_lca(global_lca, c[i]);
    }

    sort(ord.begin(), ord.end(), cmp_tin);

    int ans = 0;
    for (int i = 0; i < ord.size(); i ++)
    {
        ans = ans + depth[ord[i]] - depth[global_lca];
        if (i > 0)
        {
            ans = ans - (depth[get_lca(ord[i - 1], ord[i])] - depth[global_lca]);
        }
    }
    cout << ans  + 1 << endl;
}*/

const int block_size = sqrt(maxn);
bool cmp_mo(query a, query b)
{
    if (a.l / block_size == b.l / block_size)
        return a.r < b.r;

    return a.l / block_size < b.l / block_size;
}


int lf = 1, rf, cost;
int fen[2 * maxn], act_cnt[maxn];

void update(int v, int val)
{
    //cout << "update " << val << endl;
    for (int i = v; i <= timer; i += (i & -i))
        fen[i] += val;
}

int query_sum(int v)
{
    int s = 0;
    for (int i = v; i > 0; i -= (i & -i))
        s += fen[i];
    return s;
}

int get_kth(int k)
{
    if (k == 0) /// corner case?
        return 0;
    ///cout << "get_kth " << k << endl;
    int sum = 0, pos = 0;
    for (int bit = maxlog - 1; bit >= 0; bit --)
    {
        if ((pos | (1 << bit)) > timer)
            continue;

        int new_pos = (pos | (1 << bit));
        ///cout << "transition " << pos << " " << new_pos << " " << sum + fen[new_pos] << " " << fen[new_pos] << endl;
        if (sum + fen[new_pos] < k)
        {
            sum = sum + fen[new_pos];
            pos = new_pos;
        }
    }

    return pos + 1;
}


void add_vertex(int ver)
{
    act_cnt[ver] ++;
    if (act_cnt[ver] > 1)
        return;

    int sm = query_sum(tin[ver]);
    int bef = get_kth(sm);
    int aft = get_kth(sm + 1);
    ///cout << "add " << rev[bef] << " " << ver << " " << rev[aft] << endl;
    if (bef != 0 && aft != timer + 1)
        cost = cost + depth[get_lca(rev[bef], rev[aft])];
    cost = cost + depth[ver];
    if (bef != 0)
        cost = cost - depth[get_lca(rev[bef], ver)];
    if (aft != timer + 1)
        cost = cost - depth[get_lca(ver, rev[aft])];

    update(tin[ver], 1);
}

void remove_vertex(int ver)
{
    act_cnt[ver] --;
    if (act_cnt[ver] > 0)
        return;
    update(tin[ver], -1);
    int sm = query_sum(tin[ver]);
    int bef = get_kth(sm);
    int aft = get_kth(sm + 1);

    ///cout << "rem " << rev[bef] << " " << ver << " " << rev[aft] << endl;
    if (bef != 0 && aft != timer + 1)
        cost = cost - depth[get_lca(rev[bef], rev[aft])];

    cost = cost - depth[ver];
    if (bef != 0)
        cost = cost + depth[get_lca(rev[bef], ver)];
    if (aft != timer + 1)
        cost = cost + depth[get_lca(ver, rev[aft])];
}

void solve_query(int l, int r)
{
    while(rf < r)
    {
        rf ++;
        add_vertex(c[rf]);
    }


    while(lf > l)
    {
        lf --;
        add_vertex(c[lf]);
    }

    while(rf > r)
    {
        remove_vertex(c[rf]);
        rf --;
    }

    while(lf < l)
    {
        remove_vertex(c[lf]);
        lf ++;
    }
}

int tree[4 * maxn];

void build_tree(int root, int left, int right)
{
    if (left == right)
    {
        tree[root] = c[left];
        return;
    }

    int mid = (left + right) / 2;
    build_tree(root * 2, left, mid);
    build_tree(root * 2 + 1, mid + 1, right);

    tree[root] = get_lca(tree[root * 2], tree[root * 2 + 1]);
}

int query_lca(int root, int left, int right, int qleft, int qright)
{
    if (left == qleft && right == qright)
        return tree[root];

    int mid = (left + right) / 2;
    if (qright <= mid)
        return query_lca(root * 2, left, mid, qleft, qright);
    if (qleft > mid)
        return query_lca(root * 2 + 1, mid + 1, right, qleft, qright);

    return get_lca(query_lca(root * 2, left, mid, qleft, mid),
                   query_lca(root * 2 + 1, mid + 1, right, mid + 1, qright));

}
int res[maxn];
void queries()
{
    build_tree(1, 1, m);
    sort(task + 1, task + q + 1, cmp_mo);
    for (int i = 1; i <= q; i ++)
    {
        solve_query(task[i].l, task[i].r);
        ///cout << "cost " << cost << endl;
        ///if (i == 2)exit(0);
        int global_lca = query_lca(1, 1, m, task[i].l, task[i].r);
        //for (int j = task[i].l; j <= task[i].r; j ++)
        //  global_lca = get_lca(global_lca, c[j]);

        res[task[i].idx] = cost - depth[global_lca] + 1;
    }
    for (int i = 1; i <= q; i ++)
        cout << res[i] << endl;
}
void solve()
{
    input();
    euler();
    sparse_table();
    queries();
}

int main()
{
    speed();
    solve();
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10584 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14680 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 4 ms 14684 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 2 ms 14680 KB Output is correct
13 Correct 2 ms 14680 KB Output is correct
14 Correct 2 ms 14684 KB Output is correct
15 Correct 4 ms 14680 KB Output is correct
16 Correct 4 ms 14680 KB Output is correct
17 Correct 4 ms 14680 KB Output is correct
18 Correct 4 ms 14684 KB Output is correct
19 Correct 4 ms 14680 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
21 Correct 4 ms 14680 KB Output is correct
22 Correct 4 ms 14684 KB Output is correct
23 Correct 3 ms 14684 KB Output is correct
24 Correct 4 ms 14684 KB Output is correct
25 Correct 4 ms 14680 KB Output is correct
26 Correct 4 ms 14680 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 3 ms 14680 KB Output is correct
29 Correct 2 ms 14680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10584 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14680 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 4 ms 14684 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 2 ms 14680 KB Output is correct
13 Correct 2 ms 14680 KB Output is correct
14 Correct 2 ms 14684 KB Output is correct
15 Correct 4 ms 14680 KB Output is correct
16 Correct 4 ms 14680 KB Output is correct
17 Correct 4 ms 14680 KB Output is correct
18 Correct 4 ms 14684 KB Output is correct
19 Correct 4 ms 14680 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
21 Correct 4 ms 14680 KB Output is correct
22 Correct 4 ms 14684 KB Output is correct
23 Correct 3 ms 14684 KB Output is correct
24 Correct 4 ms 14684 KB Output is correct
25 Correct 4 ms 14680 KB Output is correct
26 Correct 4 ms 14680 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 3 ms 14680 KB Output is correct
29 Correct 2 ms 14680 KB Output is correct
30 Correct 15 ms 16984 KB Output is correct
31 Correct 18 ms 16984 KB Output is correct
32 Correct 23 ms 16988 KB Output is correct
33 Correct 24 ms 16988 KB Output is correct
34 Correct 22 ms 16984 KB Output is correct
35 Correct 5 ms 17240 KB Output is correct
36 Correct 5 ms 16984 KB Output is correct
37 Correct 5 ms 16988 KB Output is correct
38 Correct 23 ms 16984 KB Output is correct
39 Correct 23 ms 16984 KB Output is correct
40 Correct 22 ms 16984 KB Output is correct
41 Correct 5 ms 16984 KB Output is correct
42 Correct 5 ms 16984 KB Output is correct
43 Correct 5 ms 16988 KB Output is correct
44 Correct 24 ms 16984 KB Output is correct
45 Correct 23 ms 16984 KB Output is correct
46 Correct 23 ms 16984 KB Output is correct
47 Correct 5 ms 16984 KB Output is correct
48 Correct 5 ms 16988 KB Output is correct
49 Correct 5 ms 16988 KB Output is correct
50 Correct 23 ms 16984 KB Output is correct
51 Correct 24 ms 16984 KB Output is correct
52 Correct 22 ms 16988 KB Output is correct
53 Correct 22 ms 16984 KB Output is correct
54 Correct 25 ms 16984 KB Output is correct
55 Correct 23 ms 16988 KB Output is correct
56 Correct 3 ms 8536 KB Output is correct
57 Correct 3 ms 16984 KB Output is correct
58 Correct 4 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 8536 KB Output is correct
3 Correct 3 ms 8736 KB Output is correct
4 Correct 4057 ms 29536 KB Output is correct
5 Correct 2886 ms 34388 KB Output is correct
6 Correct 4446 ms 37940 KB Output is correct
7 Execution timed out 5089 ms 39788 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 49 ms 24048 KB Output is correct
3 Correct 70 ms 24400 KB Output is correct
4 Correct 61 ms 24664 KB Output is correct
5 Correct 60 ms 28420 KB Output is correct
6 Correct 73 ms 28404 KB Output is correct
7 Correct 99 ms 28496 KB Output is correct
8 Correct 92 ms 28240 KB Output is correct
9 Correct 102 ms 28392 KB Output is correct
10 Correct 110 ms 28416 KB Output is correct
11 Correct 98 ms 28420 KB Output is correct
12 Correct 95 ms 28240 KB Output is correct
13 Correct 98 ms 28496 KB Output is correct
14 Correct 94 ms 28760 KB Output is correct
15 Correct 100 ms 29776 KB Output is correct
16 Correct 94 ms 28496 KB Output is correct
17 Correct 107 ms 28496 KB Output is correct
18 Correct 103 ms 28532 KB Output is correct
19 Correct 75 ms 28472 KB Output is correct
20 Correct 63 ms 28236 KB Output is correct
21 Correct 77 ms 28496 KB Output is correct
22 Correct 84 ms 28336 KB Output is correct
23 Correct 85 ms 28240 KB Output is correct
24 Correct 92 ms 28240 KB Output is correct
25 Correct 91 ms 28428 KB Output is correct
26 Correct 104 ms 28252 KB Output is correct
27 Correct 103 ms 28500 KB Output is correct
28 Correct 91 ms 28240 KB Output is correct
29 Correct 95 ms 28496 KB Output is correct
30 Correct 100 ms 28760 KB Output is correct
31 Correct 103 ms 28496 KB Output is correct
32 Correct 107 ms 28760 KB Output is correct
33 Correct 96 ms 29268 KB Output is correct
34 Correct 124 ms 29860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 4735 ms 25960 KB Output is correct
5 Correct 4922 ms 28200 KB Output is correct
6 Execution timed out 5090 ms 31312 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10584 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14680 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 4 ms 14684 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 2 ms 14680 KB Output is correct
13 Correct 2 ms 14680 KB Output is correct
14 Correct 2 ms 14684 KB Output is correct
15 Correct 4 ms 14680 KB Output is correct
16 Correct 4 ms 14680 KB Output is correct
17 Correct 4 ms 14680 KB Output is correct
18 Correct 4 ms 14684 KB Output is correct
19 Correct 4 ms 14680 KB Output is correct
20 Correct 4 ms 14684 KB Output is correct
21 Correct 4 ms 14680 KB Output is correct
22 Correct 4 ms 14684 KB Output is correct
23 Correct 3 ms 14684 KB Output is correct
24 Correct 4 ms 14684 KB Output is correct
25 Correct 4 ms 14680 KB Output is correct
26 Correct 4 ms 14680 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 3 ms 14680 KB Output is correct
29 Correct 2 ms 14680 KB Output is correct
30 Correct 15 ms 16984 KB Output is correct
31 Correct 18 ms 16984 KB Output is correct
32 Correct 23 ms 16988 KB Output is correct
33 Correct 24 ms 16988 KB Output is correct
34 Correct 22 ms 16984 KB Output is correct
35 Correct 5 ms 17240 KB Output is correct
36 Correct 5 ms 16984 KB Output is correct
37 Correct 5 ms 16988 KB Output is correct
38 Correct 23 ms 16984 KB Output is correct
39 Correct 23 ms 16984 KB Output is correct
40 Correct 22 ms 16984 KB Output is correct
41 Correct 5 ms 16984 KB Output is correct
42 Correct 5 ms 16984 KB Output is correct
43 Correct 5 ms 16988 KB Output is correct
44 Correct 24 ms 16984 KB Output is correct
45 Correct 23 ms 16984 KB Output is correct
46 Correct 23 ms 16984 KB Output is correct
47 Correct 5 ms 16984 KB Output is correct
48 Correct 5 ms 16988 KB Output is correct
49 Correct 5 ms 16988 KB Output is correct
50 Correct 23 ms 16984 KB Output is correct
51 Correct 24 ms 16984 KB Output is correct
52 Correct 22 ms 16988 KB Output is correct
53 Correct 22 ms 16984 KB Output is correct
54 Correct 25 ms 16984 KB Output is correct
55 Correct 23 ms 16988 KB Output is correct
56 Correct 3 ms 8536 KB Output is correct
57 Correct 3 ms 16984 KB Output is correct
58 Correct 4 ms 16984 KB Output is correct
59 Correct 1 ms 10588 KB Output is correct
60 Correct 2 ms 8536 KB Output is correct
61 Correct 3 ms 8736 KB Output is correct
62 Correct 4057 ms 29536 KB Output is correct
63 Correct 2886 ms 34388 KB Output is correct
64 Correct 4446 ms 37940 KB Output is correct
65 Execution timed out 5089 ms 39788 KB Time limit exceeded
66 Halted 0 ms 0 KB -