답안 #933943

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
933943 2024-02-26T15:23:50 Z boris_mihov Tourism (JOI23_tourism) C++17
17 / 100
5000 ms 69548 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>
#include <cmath>

typedef long long llong;
const int MAXN = 200000 + 10;
const int MAXLOG = 20;
const int INF  = 1e9;

int n, m, q;
int in[MAXN];
int out[MAXN];
int globalCost;
bool isInC[MAXN];
std::vector <int> notInC;
std::vector <int> inTimes;
int cnt[MAXN];
int c[MAXN];

struct LinkedList
{
    struct Sparse
    {
        int sparse[MAXLOG][2 * MAXN];
        std::vector <int> tour;
        int getLog[2 * MAXN];
        int depth[MAXN];

        int cmp(int x, int y)
        {
            if (depth[x] < depth[y]) return x;
            else return y;
        }

        void build(std::vector <int> _tour, int dep[])
        {
            tour = _tour;
            for (int i = 1 ; i <= n ; ++i)
            {
                depth[i] = dep[i];
            }

            for (int i = 1 ; i < tour.size() ; ++i)
            {
                sparse[0][i] = tour[i];
            }

            for (int lg = 1 ; (1 << lg) <= tour.size() ; ++lg)
            {
                for (int i = 1 ; i + (1 << lg) - 1 < tour.size() ; ++i)
                {
                    sparse[lg][i] = cmp(sparse[lg - 1][i], sparse[lg - 1][i + (1 << lg - 1)]);
                }
            }

            for (int i = 1 ; i <= tour.size() ; ++i)
            {
                getLog[i] = getLog[i - 1];
                if ((1 << getLog[i] + 1) < i) getLog[i]++;
            }
        }

        int findLCA(int l, int r)
        {
            int lg = getLog[r - l + 1];
            return cmp(sparse[lg][l], sparse[lg][r - (1 << lg) + 1]);
        }

        int findDist(int u, int v)
        {
            if (in[u] > in[v])
            {
                std::swap(u, v);
            }

            return depth[u] + depth[v] - 2 * depth[findLCA(in[u], in[v])];
        }
    };

    struct RollElement
    {
        int *element;
        int value;
        int time;
    };

    int answer;
    int prev[2 * MAXN];
    int next[2 * MAXN];
    std::stack <RollElement> st;
    std::vector <int> tour;
    Sparse sparse;
    int timer;

    void build(std::vector <int> _tour, int depth[])
    {
        tour = _tour;
        sparse.build(tour, depth);
    }

    void rebuild()
    {
        while (st.size())
        {
            st.pop();
        }

        int ptr = 0;
        for (int i = 0 ; i <= tour.size() ; ++i)
        {
            while (inTimes[ptr] <= i)
            {
                ptr++;
            }

            next[i] = inTimes[ptr];
        }

        ptr = inTimes.size() - 1;
        for (int i = tour.size() ; i >= 0 ; --i)
        {
            while (inTimes[ptr] >= i)
            {
                ptr--;
            }

            prev[i] = inTimes[ptr];
        }

        std::fill(cnt, cnt + 1 + 2 * n, 0);
        for (int i = 1 ; i <= m ; ++i)
        {
            cnt[in[c[i]]]++;
        }

        answer = 2 * n - 2 - sparse.findDist(tour[next[0]], tour[prev[tour.size()]]);
        timer = 0;

        for (const int &i : notInC)
        {
            remove(in[i]);
        }

    }

    void roll(int to)
    {
        while (st.size() && st.top().time > to)
        {
            *st.top().element = st.top().value;
            st.pop();
        }
    }

    void remove(int idx)
    {
        assert(idx >= 1 && idx < tour.size());
        timer++;
        st.push({&cnt[idx], cnt[idx], timer});
        cnt[idx]--;

        if (cnt[idx] > 0)
        {
            return;
        }

        int l = prev[idx];
        int r = next[idx];
        st.push({&answer, answer, timer});
        st.push({&next[l], next[l], timer});
        st.push({&prev[r], prev[r], timer});

        if (l > 0) answer -= sparse.findDist(tour[l], tour[idx]);
        if (r < tour.size()) answer -= sparse.findDist(tour[idx], tour[r]);
        if (l > 0 && r < tour.size()) answer += sparse.findDist(tour[l], tour[r]);
        next[l] = r;
        prev[r] = l;
        getAnswer();
    }

    int getAnswer()
    {
        int res = answer;
        res += sparse.findDist(tour[next[0]], tour[prev[tour.size()]]);
        assert(res % 2 == 0);
        return res / 2 + 1;
    }
};

int bucketSize;
LinkedList list;
struct Query
{
    int l, r;
    int idx;

    friend bool operator < (const Query &a, const Query &b)
    {
        if (a.l / bucketSize != b.l / bucketSize) return a.l < b.l;
        return a.r > b.r;
    }
};

int depth[MAXN];
int answer[MAXN];
std::vector <int> tour;
std::vector <int> g[MAXN];
Query query[MAXN];

void buildDFS(int node, int par)
{
    depth[node] = depth[par] + 1;
    in[node] = tour.size();
    tour.push_back(node);

    for (const int &u : g[node])
    {
        if (u == par)
        {
            continue;
        }

        buildDFS(u, node);
        tour.push_back(node);
    }
    
    out[node] = tour.size();
}

void solve()
{
    bucketSize = sqrt(m);
    tour.push_back(0);
    buildDFS(1, 0);

    for (int i = 1 ; i <= n ; ++i)
    {
        inTimes.push_back(in[i]);
    }

    inTimes.push_back(-1);
    inTimes.push_back(0);
    inTimes.push_back(tour.size());
    inTimes.push_back(tour.size() + 1);
    std::sort(inTimes.begin(), inTimes.end());
    for (int i = 1 ; i <= m ; ++i)
    {
        isInC[c[i]] = true;
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        if (!isInC[i])
        {
            notInC.push_back(i);
        }
    }

    list.build(tour, depth);

    int lPtr = 1;
    int rPtr = m;
    int lastBucket = -1;
    std::sort(query + 1, query + 1 + q);
    for (int i = 1 ; i <= q ; ++i)
    {
        int qL = query[i].l;
        int qR = query[i].r;

        if (qL / bucketSize != lastBucket)
        {
            lPtr = 1;
            rPtr = m;
            list.rebuild();
            lastBucket = qL / bucketSize;
            while (lPtr / bucketSize != lastBucket)
            {
                list.remove(in[c[lPtr]]);
                lPtr++;
            }
        }

        while (rPtr > qR)
        {
            list.remove(in[c[rPtr]]);
            rPtr--;
        }

        int rollTo = list.timer;
        int beforeL = lPtr;

        while (lPtr < qL)
        {
            list.remove(in[c[lPtr]]);
            lPtr++;
        }

        answer[query[i].idx] = list.getAnswer();
        lPtr = beforeL;
        list.getAnswer();
        list.roll(rollTo);
        list.getAnswer();
    }
}

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

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

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

void print()
{
    for (int i = 1 ; i <= q ; ++i)
    {
        std::cout << answer[i] << '\n';
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();
    print();
    
    return 0;
}

Compilation message

tourism.cpp: In member function 'void LinkedList::Sparse::build(std::vector<int>, int*)':
tourism.cpp:47:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |             for (int i = 1 ; i < tour.size() ; ++i)
      |                              ~~^~~~~~~~~~~~~
tourism.cpp:52:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |             for (int lg = 1 ; (1 << lg) <= tour.size() ; ++lg)
      |                               ~~~~~~~~~~^~~~~~~~~~~~~~
tourism.cpp:54:52: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |                 for (int i = 1 ; i + (1 << lg) - 1 < tour.size() ; ++i)
      |                                  ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
tourism.cpp:56:88: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   56 |                     sparse[lg][i] = cmp(sparse[lg - 1][i], sparse[lg - 1][i + (1 << lg - 1)]);
      |                                                                                     ~~~^~~
tourism.cpp:60:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |             for (int i = 1 ; i <= tour.size() ; ++i)
      |                              ~~^~~~~~~~~~~~~~
tourism.cpp:63:37: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   63 |                 if ((1 << getLog[i] + 1) < i) getLog[i]++;
      |                           ~~~~~~~~~~^~~
tourism.cpp: In member function 'void LinkedList::rebuild()':
tourism.cpp:113:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |         for (int i = 0 ; i <= tour.size() ; ++i)
      |                          ~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from tourism.cpp:4:
tourism.cpp: In member function 'void LinkedList::remove(int)':
tourism.cpp:161:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |         assert(idx >= 1 && idx < tour.size());
      |                            ~~~~^~~~~~~~~~~~~
tourism.cpp:178:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |         if (r < tour.size()) answer -= sparse.findDist(tour[idx], tour[r]);
      |             ~~^~~~~~~~~~~~~
tourism.cpp:179:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |         if (l > 0 && r < tour.size()) answer += sparse.findDist(tour[l], tour[r]);
      |                      ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 20828 KB Output is correct
2 Correct 3 ms 20976 KB Output is correct
3 Correct 5 ms 22876 KB Output is correct
4 Correct 5 ms 29160 KB Output is correct
5 Correct 5 ms 29020 KB Output is correct
6 Correct 4 ms 29108 KB Output is correct
7 Correct 5 ms 29020 KB Output is correct
8 Correct 6 ms 31208 KB Output is correct
9 Correct 5 ms 31064 KB Output is correct
10 Correct 5 ms 31320 KB Output is correct
11 Correct 6 ms 31208 KB Output is correct
12 Correct 5 ms 31064 KB Output is correct
13 Correct 4 ms 31068 KB Output is correct
14 Correct 5 ms 31068 KB Output is correct
15 Correct 8 ms 31068 KB Output is correct
16 Correct 5 ms 31064 KB Output is correct
17 Correct 6 ms 31208 KB Output is correct
18 Correct 5 ms 31068 KB Output is correct
19 Correct 5 ms 31064 KB Output is correct
20 Correct 5 ms 31068 KB Output is correct
21 Correct 6 ms 31068 KB Output is correct
22 Correct 6 ms 31320 KB Output is correct
23 Correct 5 ms 31068 KB Output is correct
24 Correct 6 ms 31316 KB Output is correct
25 Correct 6 ms 31208 KB Output is correct
26 Correct 5 ms 31204 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 5 ms 31208 KB Output is correct
29 Correct 6 ms 31068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 20828 KB Output is correct
2 Correct 3 ms 20976 KB Output is correct
3 Correct 5 ms 22876 KB Output is correct
4 Correct 5 ms 29160 KB Output is correct
5 Correct 5 ms 29020 KB Output is correct
6 Correct 4 ms 29108 KB Output is correct
7 Correct 5 ms 29020 KB Output is correct
8 Correct 6 ms 31208 KB Output is correct
9 Correct 5 ms 31064 KB Output is correct
10 Correct 5 ms 31320 KB Output is correct
11 Correct 6 ms 31208 KB Output is correct
12 Correct 5 ms 31064 KB Output is correct
13 Correct 4 ms 31068 KB Output is correct
14 Correct 5 ms 31068 KB Output is correct
15 Correct 8 ms 31068 KB Output is correct
16 Correct 5 ms 31064 KB Output is correct
17 Correct 6 ms 31208 KB Output is correct
18 Correct 5 ms 31068 KB Output is correct
19 Correct 5 ms 31064 KB Output is correct
20 Correct 5 ms 31068 KB Output is correct
21 Correct 6 ms 31068 KB Output is correct
22 Correct 6 ms 31320 KB Output is correct
23 Correct 5 ms 31068 KB Output is correct
24 Correct 6 ms 31316 KB Output is correct
25 Correct 6 ms 31208 KB Output is correct
26 Correct 5 ms 31204 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 5 ms 31208 KB Output is correct
29 Correct 6 ms 31068 KB Output is correct
30 Correct 13 ms 35420 KB Output is correct
31 Correct 13 ms 35420 KB Output is correct
32 Correct 16 ms 35584 KB Output is correct
33 Correct 22 ms 35700 KB Output is correct
34 Correct 16 ms 35672 KB Output is correct
35 Correct 8 ms 35420 KB Output is correct
36 Correct 8 ms 35420 KB Output is correct
37 Correct 8 ms 35416 KB Output is correct
38 Correct 15 ms 35568 KB Output is correct
39 Correct 15 ms 35672 KB Output is correct
40 Correct 17 ms 35660 KB Output is correct
41 Correct 8 ms 35572 KB Output is correct
42 Correct 8 ms 35572 KB Output is correct
43 Correct 7 ms 35652 KB Output is correct
44 Correct 17 ms 35676 KB Output is correct
45 Correct 15 ms 35676 KB Output is correct
46 Correct 15 ms 35676 KB Output is correct
47 Correct 7 ms 35420 KB Output is correct
48 Correct 9 ms 35416 KB Output is correct
49 Correct 8 ms 35420 KB Output is correct
50 Correct 16 ms 35676 KB Output is correct
51 Correct 15 ms 35708 KB Output is correct
52 Correct 14 ms 35672 KB Output is correct
53 Correct 20 ms 35676 KB Output is correct
54 Correct 15 ms 35676 KB Output is correct
55 Correct 14 ms 35676 KB Output is correct
56 Correct 6 ms 16732 KB Output is correct
57 Correct 7 ms 35676 KB Output is correct
58 Correct 17 ms 35704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20828 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 5 ms 16884 KB Output is correct
4 Correct 2509 ms 57360 KB Output is correct
5 Correct 2481 ms 64072 KB Output is correct
6 Correct 3438 ms 66596 KB Output is correct
7 Correct 4161 ms 69308 KB Output is correct
8 Correct 4110 ms 69312 KB Output is correct
9 Correct 4121 ms 69308 KB Output is correct
10 Correct 4086 ms 69304 KB Output is correct
11 Correct 4149 ms 69548 KB Output is correct
12 Correct 370 ms 64316 KB Output is correct
13 Correct 367 ms 64376 KB Output is correct
14 Correct 361 ms 64252 KB Output is correct
15 Correct 69 ms 66984 KB Output is correct
16 Correct 4248 ms 68796 KB Output is correct
17 Correct 774 ms 22168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 20824 KB Output is correct
2 Correct 1699 ms 50036 KB Output is correct
3 Correct 2539 ms 51220 KB Output is correct
4 Correct 2543 ms 54296 KB Output is correct
5 Correct 60 ms 54368 KB Output is correct
6 Correct 92 ms 57252 KB Output is correct
7 Correct 238 ms 58684 KB Output is correct
8 Correct 482 ms 59080 KB Output is correct
9 Correct 1713 ms 59208 KB Output is correct
10 Correct 4600 ms 59276 KB Output is correct
11 Correct 4899 ms 59304 KB Output is correct
12 Correct 4961 ms 59352 KB Output is correct
13 Execution timed out 5058 ms 59304 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 20824 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 5 ms 16840 KB Output is correct
4 Correct 2784 ms 54480 KB Output is correct
5 Correct 2936 ms 54752 KB Output is correct
6 Correct 4275 ms 61052 KB Output is correct
7 Correct 4872 ms 63132 KB Output is correct
8 Execution timed out 5104 ms 62364 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 20828 KB Output is correct
2 Correct 3 ms 20976 KB Output is correct
3 Correct 5 ms 22876 KB Output is correct
4 Correct 5 ms 29160 KB Output is correct
5 Correct 5 ms 29020 KB Output is correct
6 Correct 4 ms 29108 KB Output is correct
7 Correct 5 ms 29020 KB Output is correct
8 Correct 6 ms 31208 KB Output is correct
9 Correct 5 ms 31064 KB Output is correct
10 Correct 5 ms 31320 KB Output is correct
11 Correct 6 ms 31208 KB Output is correct
12 Correct 5 ms 31064 KB Output is correct
13 Correct 4 ms 31068 KB Output is correct
14 Correct 5 ms 31068 KB Output is correct
15 Correct 8 ms 31068 KB Output is correct
16 Correct 5 ms 31064 KB Output is correct
17 Correct 6 ms 31208 KB Output is correct
18 Correct 5 ms 31068 KB Output is correct
19 Correct 5 ms 31064 KB Output is correct
20 Correct 5 ms 31068 KB Output is correct
21 Correct 6 ms 31068 KB Output is correct
22 Correct 6 ms 31320 KB Output is correct
23 Correct 5 ms 31068 KB Output is correct
24 Correct 6 ms 31316 KB Output is correct
25 Correct 6 ms 31208 KB Output is correct
26 Correct 5 ms 31204 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 5 ms 31208 KB Output is correct
29 Correct 6 ms 31068 KB Output is correct
30 Correct 13 ms 35420 KB Output is correct
31 Correct 13 ms 35420 KB Output is correct
32 Correct 16 ms 35584 KB Output is correct
33 Correct 22 ms 35700 KB Output is correct
34 Correct 16 ms 35672 KB Output is correct
35 Correct 8 ms 35420 KB Output is correct
36 Correct 8 ms 35420 KB Output is correct
37 Correct 8 ms 35416 KB Output is correct
38 Correct 15 ms 35568 KB Output is correct
39 Correct 15 ms 35672 KB Output is correct
40 Correct 17 ms 35660 KB Output is correct
41 Correct 8 ms 35572 KB Output is correct
42 Correct 8 ms 35572 KB Output is correct
43 Correct 7 ms 35652 KB Output is correct
44 Correct 17 ms 35676 KB Output is correct
45 Correct 15 ms 35676 KB Output is correct
46 Correct 15 ms 35676 KB Output is correct
47 Correct 7 ms 35420 KB Output is correct
48 Correct 9 ms 35416 KB Output is correct
49 Correct 8 ms 35420 KB Output is correct
50 Correct 16 ms 35676 KB Output is correct
51 Correct 15 ms 35708 KB Output is correct
52 Correct 14 ms 35672 KB Output is correct
53 Correct 20 ms 35676 KB Output is correct
54 Correct 15 ms 35676 KB Output is correct
55 Correct 14 ms 35676 KB Output is correct
56 Correct 6 ms 16732 KB Output is correct
57 Correct 7 ms 35676 KB Output is correct
58 Correct 17 ms 35704 KB Output is correct
59 Correct 4 ms 20828 KB Output is correct
60 Correct 3 ms 16732 KB Output is correct
61 Correct 5 ms 16884 KB Output is correct
62 Correct 2509 ms 57360 KB Output is correct
63 Correct 2481 ms 64072 KB Output is correct
64 Correct 3438 ms 66596 KB Output is correct
65 Correct 4161 ms 69308 KB Output is correct
66 Correct 4110 ms 69312 KB Output is correct
67 Correct 4121 ms 69308 KB Output is correct
68 Correct 4086 ms 69304 KB Output is correct
69 Correct 4149 ms 69548 KB Output is correct
70 Correct 370 ms 64316 KB Output is correct
71 Correct 367 ms 64376 KB Output is correct
72 Correct 361 ms 64252 KB Output is correct
73 Correct 69 ms 66984 KB Output is correct
74 Correct 4248 ms 68796 KB Output is correct
75 Correct 774 ms 22168 KB Output is correct
76 Correct 3 ms 20824 KB Output is correct
77 Correct 1699 ms 50036 KB Output is correct
78 Correct 2539 ms 51220 KB Output is correct
79 Correct 2543 ms 54296 KB Output is correct
80 Correct 60 ms 54368 KB Output is correct
81 Correct 92 ms 57252 KB Output is correct
82 Correct 238 ms 58684 KB Output is correct
83 Correct 482 ms 59080 KB Output is correct
84 Correct 1713 ms 59208 KB Output is correct
85 Correct 4600 ms 59276 KB Output is correct
86 Correct 4899 ms 59304 KB Output is correct
87 Correct 4961 ms 59352 KB Output is correct
88 Execution timed out 5058 ms 59304 KB Time limit exceeded
89 Halted 0 ms 0 KB -