Submission #898147

# Submission time Handle Problem Language Result Execution time Memory
898147 2024-01-04T10:56:42 Z vjudge1 Tourism (JOI23_tourism) C++17
0 / 100
5000 ms 16992 KB
#pragma GCC optimize("unroll-loops")
#pragma gcc optimize("Ofast")
#pragma GCC optimization("Ofast")
#pragma optimize(Ofast)
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define str string
#define fastio ios::sync_with_stdio(0), cin.tie(0);
#define fs first
#define ss second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define len(x) x.size()

#define print(a)          \
    for (auto &x : a)     \
        cout << x << " "; \
    cout << endl;

#define printmp(a)    \
    for (auto &x : a) \
        cout << x.fs << " " << x.ss << endl;

const int mod = 998244353;

struct Lowest_Common_Ancestor
{
    vector<vector<int>> LCA;
    vector<int> Degree;

    void dfs(int u, int p, vector<vector<int>> &tree)
    {
        LCA[u][0] = p;
        for (auto &x : tree[u])
        {
            if (x != p)
            {
                Degree[x] = Degree[u] + 1;
                dfs(x, u, tree);
            }
        }
    }

    Lowest_Common_Ancestor(vector<vector<int>> &tree)
    {
        LCA.resize(tree.size(), vector<int>((int)(log2(tree.size())) + 1));
        Degree.resize(tree.size(), 1);

        dfs(1, -1, tree);

        for (int i = 1; i <= tree.size() - 1; i++)
            for (int j = 1; j < LCA[i].size(); j++)
                if (LCA[i][j - 1] == -1)
                    LCA[i][j] = -1;
                else
                    LCA[i][j] = LCA[LCA[i][j - 1]][j - 1];
    }

    void binary_shifting(int &X, int k)
    {
        for (int i = LCA[X].size(); i >= 0; i--)
        {
            if (k >= (1 << i))
            {
                X = LCA[X][i];
                k -= (1 << i);
            }
        }
    }

    int lca(int X, int Y)
    {
        if (Degree[X] < Degree[Y])
            swap(X, Y);
        if (Degree[X] != Degree[Y])
            binary_shifting(X, Degree[X] - Degree[Y]);
        int k = LCA[0].size() - 1;
        while (X != Y)
        {
            int l = 0, r = k;
            while (l < r)
            {
                int m = (l + r + 1) >> 1;
                if (LCA[X][m] == LCA[Y][m])
                    r = m - 1;
                else
                    l = m;
            }
            X = LCA[X][l];
            Y = LCA[Y][l];
            k = l;
        }
        return X;
    }
};

void solve(){
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> tree(n + 1);
    for(int i = 0; i < n - 1; i ++){
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
    vector<int> c(m);
    for(int i = 0; i < m; i ++)
        cin >> c[i];
    Lowest_Common_Ancestor lca(tree);
    for(int i = 0; i < q; i ++){
        int l, r;
        cin >> l >> r;
        int p = c[l - 1];
        for(int j = l; j < r; j ++)
            p = lca.lca(p, c[j]);
        map<int, int> mp;
        for(int j = l - 1; j < r; j ++){
            if(c[j] != p){
                int x = c[j];
                lca.binary_shifting(x, lca.Degree[c[j]] - lca.Degree[p] - 1);
                mp[x] = max(mp[x], lca.Degree[c[j]] - lca.Degree[p]);
            }
        }
        int ans = 1;
        for(auto x : mp)
            ans += x.ss;
        cout << ans << endl;
    }
}

signed main()
{
    fastio
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
        cout << endl;
    }
}

Compilation message

tourism.cpp:2: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    2 | #pragma gcc optimize("Ofast")
      | 
tourism.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization("Ofast")
      | 
tourism.cpp:4: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
    4 | #pragma optimize(Ofast)
      | 
tourism.cpp: In constructor 'Lowest_Common_Ancestor::Lowest_Common_Ancestor(std::vector<std::vector<long long int> >&)':
tourism.cpp:53:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for (int i = 1; i <= tree.size() - 1; i++)
      |                         ~~^~~~~~~~~~~~~~~~~~
tourism.cpp:54:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |             for (int j = 1; j < LCA[i].size(); j++)
      |                             ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 3 ms 492 KB Output is correct
4 Execution timed out 5050 ms 16992 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -