Submission #850641

# Submission time Handle Problem Language Result Execution time Memory
850641 2023-09-17T08:18:29 Z danikoynov Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 29928 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 < 2 * maxn; 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 bef = get_kth(query_sum(tin[ver]));
    int aft = get_kth(query_sum(tin[ver]) + 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 bef = get_kth(query_sum(tin[ver]));
    int aft = get_kth(query_sum(tin[ver]) + 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);
    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 4 ms 14684 KB Output is correct
5 Correct 4 ms 14680 KB Output is correct
6 Correct 4 ms 14680 KB Output is correct
7 Correct 4 ms 14680 KB Output is correct
8 Correct 4 ms 14680 KB Output is correct
9 Correct 6 ms 14680 KB Output is correct
10 Correct 6 ms 14684 KB Output is correct
11 Correct 6 ms 14680 KB Output is correct
12 Correct 3 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 6 ms 14684 KB Output is correct
16 Correct 6 ms 14680 KB Output is correct
17 Correct 6 ms 14680 KB Output is correct
18 Correct 6 ms 14680 KB Output is correct
19 Correct 6 ms 14680 KB Output is correct
20 Correct 6 ms 14680 KB Output is correct
21 Correct 6 ms 14680 KB Output is correct
22 Correct 8 ms 14936 KB Output is correct
23 Correct 6 ms 14684 KB Output is correct
24 Correct 6 ms 14684 KB Output is correct
25 Correct 6 ms 14680 KB Output is correct
26 Correct 6 ms 14680 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 14680 KB Output is correct
29 Correct 8 ms 14684 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 4 ms 14684 KB Output is correct
5 Correct 4 ms 14680 KB Output is correct
6 Correct 4 ms 14680 KB Output is correct
7 Correct 4 ms 14680 KB Output is correct
8 Correct 4 ms 14680 KB Output is correct
9 Correct 6 ms 14680 KB Output is correct
10 Correct 6 ms 14684 KB Output is correct
11 Correct 6 ms 14680 KB Output is correct
12 Correct 3 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 6 ms 14684 KB Output is correct
16 Correct 6 ms 14680 KB Output is correct
17 Correct 6 ms 14680 KB Output is correct
18 Correct 6 ms 14680 KB Output is correct
19 Correct 6 ms 14680 KB Output is correct
20 Correct 6 ms 14680 KB Output is correct
21 Correct 6 ms 14680 KB Output is correct
22 Correct 8 ms 14936 KB Output is correct
23 Correct 6 ms 14684 KB Output is correct
24 Correct 6 ms 14684 KB Output is correct
25 Correct 6 ms 14680 KB Output is correct
26 Correct 6 ms 14680 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 14680 KB Output is correct
29 Correct 8 ms 14684 KB Output is correct
30 Correct 108 ms 17232 KB Output is correct
31 Correct 140 ms 16988 KB Output is correct
32 Correct 225 ms 17008 KB Output is correct
33 Correct 218 ms 17184 KB Output is correct
34 Correct 205 ms 17012 KB Output is correct
35 Correct 7 ms 16988 KB Output is correct
36 Correct 7 ms 16984 KB Output is correct
37 Correct 7 ms 16984 KB Output is correct
38 Correct 207 ms 16988 KB Output is correct
39 Correct 209 ms 17112 KB Output is correct
40 Correct 201 ms 17132 KB Output is correct
41 Correct 7 ms 16988 KB Output is correct
42 Correct 8 ms 16984 KB Output is correct
43 Correct 8 ms 16988 KB Output is correct
44 Correct 225 ms 17076 KB Output is correct
45 Correct 209 ms 17036 KB Output is correct
46 Correct 204 ms 17064 KB Output is correct
47 Correct 7 ms 16984 KB Output is correct
48 Correct 7 ms 17184 KB Output is correct
49 Correct 7 ms 16984 KB Output is correct
50 Correct 207 ms 16988 KB Output is correct
51 Correct 197 ms 17020 KB Output is correct
52 Correct 197 ms 17016 KB Output is correct
53 Correct 209 ms 17008 KB Output is correct
54 Correct 208 ms 16988 KB Output is correct
55 Correct 208 ms 17232 KB Output is correct
56 Correct 7 ms 8536 KB Output is correct
57 Correct 4 ms 16996 KB Output is correct
58 Correct 301 ms 17264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 7 ms 8536 KB Output is correct
4 Execution timed out 5042 ms 29108 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 51 ms 24044 KB Output is correct
3 Correct 70 ms 24400 KB Output is correct
4 Correct 64 ms 24656 KB Output is correct
5 Correct 60 ms 28240 KB Output is correct
6 Correct 72 ms 28240 KB Output is correct
7 Correct 85 ms 28424 KB Output is correct
8 Correct 91 ms 28248 KB Output is correct
9 Correct 97 ms 28412 KB Output is correct
10 Correct 106 ms 28400 KB Output is correct
11 Correct 99 ms 28428 KB Output is correct
12 Correct 101 ms 28264 KB Output is correct
13 Correct 95 ms 28496 KB Output is correct
14 Correct 104 ms 28760 KB Output is correct
15 Correct 112 ms 29928 KB Output is correct
16 Correct 92 ms 28496 KB Output is correct
17 Correct 94 ms 28540 KB Output is correct
18 Correct 108 ms 28496 KB Output is correct
19 Correct 58 ms 28240 KB Output is correct
20 Correct 61 ms 28248 KB Output is correct
21 Correct 76 ms 28432 KB Output is correct
22 Correct 88 ms 28456 KB Output is correct
23 Correct 86 ms 28240 KB Output is correct
24 Correct 102 ms 28440 KB Output is correct
25 Correct 106 ms 28432 KB Output is correct
26 Correct 98 ms 28244 KB Output is correct
27 Correct 103 ms 28752 KB Output is correct
28 Correct 102 ms 28240 KB Output is correct
29 Correct 97 ms 28468 KB Output is correct
30 Correct 103 ms 28752 KB Output is correct
31 Correct 97 ms 28500 KB Output is correct
32 Correct 105 ms 28680 KB Output is correct
33 Correct 111 ms 29256 KB Output is correct
34 Correct 97 ms 29816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 7 ms 8792 KB Output is correct
4 Execution timed out 5067 ms 25268 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 2 ms 10584 KB Output is correct
3 Correct 2 ms 10584 KB Output is correct
4 Correct 4 ms 14684 KB Output is correct
5 Correct 4 ms 14680 KB Output is correct
6 Correct 4 ms 14680 KB Output is correct
7 Correct 4 ms 14680 KB Output is correct
8 Correct 4 ms 14680 KB Output is correct
9 Correct 6 ms 14680 KB Output is correct
10 Correct 6 ms 14684 KB Output is correct
11 Correct 6 ms 14680 KB Output is correct
12 Correct 3 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 6 ms 14684 KB Output is correct
16 Correct 6 ms 14680 KB Output is correct
17 Correct 6 ms 14680 KB Output is correct
18 Correct 6 ms 14680 KB Output is correct
19 Correct 6 ms 14680 KB Output is correct
20 Correct 6 ms 14680 KB Output is correct
21 Correct 6 ms 14680 KB Output is correct
22 Correct 8 ms 14936 KB Output is correct
23 Correct 6 ms 14684 KB Output is correct
24 Correct 6 ms 14684 KB Output is correct
25 Correct 6 ms 14680 KB Output is correct
26 Correct 6 ms 14680 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 14680 KB Output is correct
29 Correct 8 ms 14684 KB Output is correct
30 Correct 108 ms 17232 KB Output is correct
31 Correct 140 ms 16988 KB Output is correct
32 Correct 225 ms 17008 KB Output is correct
33 Correct 218 ms 17184 KB Output is correct
34 Correct 205 ms 17012 KB Output is correct
35 Correct 7 ms 16988 KB Output is correct
36 Correct 7 ms 16984 KB Output is correct
37 Correct 7 ms 16984 KB Output is correct
38 Correct 207 ms 16988 KB Output is correct
39 Correct 209 ms 17112 KB Output is correct
40 Correct 201 ms 17132 KB Output is correct
41 Correct 7 ms 16988 KB Output is correct
42 Correct 8 ms 16984 KB Output is correct
43 Correct 8 ms 16988 KB Output is correct
44 Correct 225 ms 17076 KB Output is correct
45 Correct 209 ms 17036 KB Output is correct
46 Correct 204 ms 17064 KB Output is correct
47 Correct 7 ms 16984 KB Output is correct
48 Correct 7 ms 17184 KB Output is correct
49 Correct 7 ms 16984 KB Output is correct
50 Correct 207 ms 16988 KB Output is correct
51 Correct 197 ms 17020 KB Output is correct
52 Correct 197 ms 17016 KB Output is correct
53 Correct 209 ms 17008 KB Output is correct
54 Correct 208 ms 16988 KB Output is correct
55 Correct 208 ms 17232 KB Output is correct
56 Correct 7 ms 8536 KB Output is correct
57 Correct 4 ms 16996 KB Output is correct
58 Correct 301 ms 17264 KB Output is correct
59 Correct 2 ms 10584 KB Output is correct
60 Correct 2 ms 8536 KB Output is correct
61 Correct 7 ms 8536 KB Output is correct
62 Execution timed out 5042 ms 29108 KB Time limit exceeded
63 Halted 0 ms 0 KB -