Submission #850649

# Submission time Handle Problem Language Result Execution time Memory
850649 2023-09-17T08:25:20 Z danikoynov Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 30048 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;
}*/

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()
{
    block_size = 100;
    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 1 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 3 ms 14892 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 2 ms 14680 KB Output is correct
7 Correct 2 ms 14680 KB Output is correct
8 Correct 2 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14680 KB Output is correct
11 Correct 3 ms 14680 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 14680 KB Output is correct
15 Correct 3 ms 14680 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14680 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 3 ms 14680 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 3 ms 14680 KB Output is correct
24 Correct 3 ms 14680 KB Output is correct
25 Correct 3 ms 14680 KB Output is correct
26 Correct 3 ms 14680 KB Output is correct
27 Correct 1 ms 8536 KB Output is correct
28 Correct 2 ms 14684 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 1 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 3 ms 14892 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 2 ms 14680 KB Output is correct
7 Correct 2 ms 14680 KB Output is correct
8 Correct 2 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14680 KB Output is correct
11 Correct 3 ms 14680 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 14680 KB Output is correct
15 Correct 3 ms 14680 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14680 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 3 ms 14680 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 3 ms 14680 KB Output is correct
24 Correct 3 ms 14680 KB Output is correct
25 Correct 3 ms 14680 KB Output is correct
26 Correct 3 ms 14680 KB Output is correct
27 Correct 1 ms 8536 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14680 KB Output is correct
30 Correct 9 ms 16984 KB Output is correct
31 Correct 11 ms 16984 KB Output is correct
32 Correct 13 ms 16984 KB Output is correct
33 Correct 14 ms 16984 KB Output is correct
34 Correct 13 ms 16984 KB Output is correct
35 Correct 5 ms 16984 KB Output is correct
36 Correct 5 ms 16984 KB Output is correct
37 Correct 5 ms 16988 KB Output is correct
38 Correct 13 ms 16984 KB Output is correct
39 Correct 14 ms 16984 KB Output is correct
40 Correct 15 ms 16984 KB Output is correct
41 Correct 5 ms 16984 KB Output is correct
42 Correct 6 ms 16988 KB Output is correct
43 Correct 6 ms 16984 KB Output is correct
44 Correct 14 ms 16988 KB Output is correct
45 Correct 14 ms 17056 KB Output is correct
46 Correct 14 ms 16984 KB Output is correct
47 Correct 5 ms 16984 KB Output is correct
48 Correct 5 ms 16984 KB Output is correct
49 Correct 5 ms 16984 KB Output is correct
50 Correct 13 ms 16988 KB Output is correct
51 Correct 14 ms 16984 KB Output is correct
52 Correct 13 ms 17044 KB Output is correct
53 Correct 13 ms 16984 KB Output is correct
54 Correct 13 ms 16984 KB Output is correct
55 Correct 14 ms 16984 KB Output is correct
56 Correct 3 ms 8540 KB Output is correct
57 Correct 3 ms 16988 KB Output is correct
58 Correct 4 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 3 ms 8536 KB Output is correct
4 Execution timed out 5037 ms 29264 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 50 ms 23896 KB Output is correct
3 Correct 68 ms 24412 KB Output is correct
4 Correct 63 ms 24656 KB Output is correct
5 Correct 62 ms 28244 KB Output is correct
6 Correct 82 ms 28756 KB Output is correct
7 Correct 88 ms 28420 KB Output is correct
8 Correct 92 ms 28288 KB Output is correct
9 Correct 105 ms 28496 KB Output is correct
10 Correct 100 ms 28252 KB Output is correct
11 Correct 105 ms 28408 KB Output is correct
12 Correct 104 ms 28708 KB Output is correct
13 Correct 106 ms 28496 KB Output is correct
14 Correct 105 ms 28764 KB Output is correct
15 Correct 127 ms 29932 KB Output is correct
16 Correct 93 ms 28496 KB Output is correct
17 Correct 106 ms 28804 KB Output is correct
18 Correct 99 ms 28536 KB Output is correct
19 Correct 55 ms 28472 KB Output is correct
20 Correct 66 ms 28512 KB Output is correct
21 Correct 72 ms 28252 KB Output is correct
22 Correct 79 ms 28240 KB Output is correct
23 Correct 85 ms 28248 KB Output is correct
24 Correct 90 ms 28240 KB Output is correct
25 Correct 91 ms 28428 KB Output is correct
26 Correct 96 ms 28480 KB Output is correct
27 Correct 96 ms 28472 KB Output is correct
28 Correct 93 ms 28424 KB Output is correct
29 Correct 94 ms 28244 KB Output is correct
30 Correct 92 ms 28496 KB Output is correct
31 Correct 99 ms 28576 KB Output is correct
32 Correct 99 ms 28708 KB Output is correct
33 Correct 106 ms 29144 KB Output is correct
34 Correct 119 ms 30048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Execution timed out 5047 ms 25436 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 1 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 3 ms 14892 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 2 ms 14680 KB Output is correct
7 Correct 2 ms 14680 KB Output is correct
8 Correct 2 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14680 KB Output is correct
11 Correct 3 ms 14680 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 14680 KB Output is correct
15 Correct 3 ms 14680 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14680 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 3 ms 14680 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 3 ms 14680 KB Output is correct
24 Correct 3 ms 14680 KB Output is correct
25 Correct 3 ms 14680 KB Output is correct
26 Correct 3 ms 14680 KB Output is correct
27 Correct 1 ms 8536 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14680 KB Output is correct
30 Correct 9 ms 16984 KB Output is correct
31 Correct 11 ms 16984 KB Output is correct
32 Correct 13 ms 16984 KB Output is correct
33 Correct 14 ms 16984 KB Output is correct
34 Correct 13 ms 16984 KB Output is correct
35 Correct 5 ms 16984 KB Output is correct
36 Correct 5 ms 16984 KB Output is correct
37 Correct 5 ms 16988 KB Output is correct
38 Correct 13 ms 16984 KB Output is correct
39 Correct 14 ms 16984 KB Output is correct
40 Correct 15 ms 16984 KB Output is correct
41 Correct 5 ms 16984 KB Output is correct
42 Correct 6 ms 16988 KB Output is correct
43 Correct 6 ms 16984 KB Output is correct
44 Correct 14 ms 16988 KB Output is correct
45 Correct 14 ms 17056 KB Output is correct
46 Correct 14 ms 16984 KB Output is correct
47 Correct 5 ms 16984 KB Output is correct
48 Correct 5 ms 16984 KB Output is correct
49 Correct 5 ms 16984 KB Output is correct
50 Correct 13 ms 16988 KB Output is correct
51 Correct 14 ms 16984 KB Output is correct
52 Correct 13 ms 17044 KB Output is correct
53 Correct 13 ms 16984 KB Output is correct
54 Correct 13 ms 16984 KB Output is correct
55 Correct 14 ms 16984 KB Output is correct
56 Correct 3 ms 8540 KB Output is correct
57 Correct 3 ms 16988 KB Output is correct
58 Correct 4 ms 16988 KB Output is correct
59 Correct 2 ms 10584 KB Output is correct
60 Correct 1 ms 8536 KB Output is correct
61 Correct 3 ms 8536 KB Output is correct
62 Execution timed out 5037 ms 29264 KB Time limit exceeded
63 Halted 0 ms 0 KB -