Submission #1334015

#TimeUsernameProblemLanguageResultExecution timeMemory
1334015chikien2009Tourism (JOI23_tourism)C++20
28 / 100
5089 ms24392 KiB
#include <bits/stdc++.h>

using namespace std;

void setup()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int block = 1000;

int n, m, q, a, b, cur, highest;
int depth[100000], sp[20][200000], num[100000], c[100000], head[100000], tail[100000], res[100000], id[100000];
vector<int> g[100000];
array<int, 3> query[100000];
set<pair<int, int>> s;

inline void DFS(int node, int par)
{
    head[node] = a++;
    id[node] = b;
    sp[0][b++] = node;
    for (auto &i : g[node])
    {
        if (i != par)
        {
            depth[i] = depth[node] + 1;
            DFS(i, node);
            sp[0][b++] = node;                                             
        }
    }
    tail[node] = a - 1;
}

inline int LCA(int l, int r)
{
    l = id[l];
    r = id[r];
    if (l > r)
    {
        swap(l, r);
    }
    int k = __lg(r - l + 1);
    return (depth[sp[k][l]] < depth[sp[k][r - (1 << k) + 1]] ? sp[k][l] : sp[k][r - (1 << k) + 1]);
}

inline int Dist(int ina, int inb)
{
    return depth[inb] - depth[ina];
}

inline bool Comp(const array<int, 3> &ina, const array<int, 3> &inb)
{
    return (ina[0] / block == inb[0] / block ? ina[1] < inb[1] : ina[0] < inb[0]);
}

inline void Add(int node)
{
    set<pair<int, int>>::iterator it;
    s.insert({head[node], node});
    if (s.size() == 1)
    {
        cur++;
        highest = node;
        return;
    }
    if (head[highest] <= head[node] && tail[node] <= tail[highest])
    {
        a = 0;
        it = s.lower_bound({head[node], node});
        if (it != s.begin())
        {
            b = LCA((*prev(it)).second, node);
            a = (depth[b] > depth[a] ? b : a);
        }
        if (it != prev(s.end()))
        {
            b = LCA((*next(it)).second, node);
            a = (depth[b] > depth[a] ? b : a);
        }
        cur += Dist(a, node);
    }
    else
    {
        a = LCA(highest, node);
        cur += Dist(a, highest) + Dist(a, node);
        highest = a;
    }
}

inline void Remove(int node)
{
    set<pair<int, int>>::iterator it;
    a = 0;
    it = s.lower_bound({head[node], node});
    if (it != s.begin())
    {
        b = LCA((*prev(it)).second, node);
        a = (depth[b] > depth[a] ? b : a);
    }
    if (it != prev(s.end()))
    {
        b = LCA((*next(it)).second, node);
        a = (depth[b] > depth[a] ? b : a);
    }
    s.erase(it);
    highest = LCA((*s.begin()).second, (*s.rbegin()).second);
    if (head[highest] <= head[node] && head[node] <= tail[highest])
    {
        cur -= Dist(a, node);
    }
    else
    {
        a = LCA(highest, node);
        cur -= Dist(a, highest) + Dist(a, node);
    }
}

int main()
{
    setup();

    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; ++i)
    {
        cin >> a >> b;
        g[a - 1].push_back(b - 1);
        g[b - 1].push_back(a - 1);
    }
    for (int i = 0; i < m; ++i)
    {
        cin >> c[i];
        c[i]--;
    }
    for (int i = 0; i < q; ++i)
    {
        cin >> query[i][0] >> query[i][1];
        query[i][2] = i;
        query[i][0]--;
        query[i][1]--;
    }
    sort(query, query + q, Comp);
    a = b = 0;
    DFS(0, 0);
    for (int i = 1; i <= __lg(b); ++i)
    {
        for (int j = 0; j + (1 << i) <= b; ++j)
        {
            sp[i][j] = (depth[sp[i - 1][j]] < depth[sp[i - 1][j + (1 << (i - 1))]] ? sp[i - 1][j] : sp[i - 1][j + (1 << (i - 1))]);
        }
    }
    for (int i = 0, l = 0, r = -1; i < q; ++i)
    {
        while (r < query[i][1])
        {
            r++;
            num[c[r]]++;
            if (num[c[r]] == 1)
            {
                Add(c[r]);
            }
        }
        while (query[i][0] < l)
        {
            l--;
            num[c[l]]++;
            if (num[c[l]] == 1)
            {
                Add(c[l]);
            }
        }
        while (query[i][1] < r)
        {
            if (num[c[r]] == 1)
            {
                Remove(c[r]);
            }
            num[c[r]]--;
            r--;
        }
        while (l < query[i][0])
        {
            if (num[c[l]] == 1)
            {
                Remove(c[l]);
            }
            num[c[l]]--;
            l++;
        }
        res[query[i][2]] = cur;
    }
    for (int i = 0; i < q; ++i)
    {
        cout << res[i] << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...