답안 #823677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
823677 2023-08-13T00:20:04 Z thimote75 Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 62100 KB
#include <bits/stdc++.h>

using namespace std;

using idata = vector<int>;
using iset  = set<int>;
using igrid = vector<idata>;

using t_road  = pair<int, int>;
using t_roads = vector<t_road>;
using t_graph = vector<t_roads>;

using di = pair<int, int>;
using vd = vector<di>;

const int MAXK = 20;

struct Tree {
    igrid roads;

    idata preordre, postordre, parents, depth;
    igrid eulertour;
    igrid parents_2k;

    void dfs (int node, int parent, int _depth, int &iOrdre) {
        preordre[node] = iOrdre ++;
        parents [node] = parent;
        depth   [node] = _depth;

        for (int next : roads[node]) if (next != parent) {
            eulertour[node].push_back(iOrdre ++);
            dfs(next, node, _depth + 1, iOrdre);
        }
        
        postordre[node] = iOrdre ++;
    }

    Tree (int N, igrid &roads) : roads(roads) {
        eulertour.resize(N);
        postordre.resize(N);
        preordre .resize(N);
        parents  .resize(N);
        depth    .resize(N);

        int iOrdre = 0;
        dfs (0, -1, 0, iOrdre);

        parents_2k.resize(N, idata(MAXK, -1));

        for (int i = 0; i < N; i ++) parents_2k[i][0] = parents[i];

        for (int k = 0; k + 1 < MAXK; k ++) {
            for (int i = 0; i < N; i ++) {
                if (parents_2k[i][k] == -1) continue ;

                parents_2k[i][k + 1] = parents_2k[parents_2k[i][k]][k];
            }
        }
    }

    int jump (int node, int k) {
        for (int i = 0; i < MAXK; i ++)
            if ((1 << i) & k)
                node = parents_2k[node][i];
        
        return node;
    }
    int lca (int a, int b) {
        if (depth[a] > depth[b]) swap(a, b);

        b = jump(b, depth[b] - depth[a]);
        if (a == b) return a;

        for (int i = MAXK - 1; i >= 0; i --) {
            if (parents_2k[a][i] == parents_2k[b][i]) continue ;

            a = parents_2k[a][i];
            b = parents_2k[b][i];
        }

        if (a == b) return a;
        return parents[a];
    }
};

struct VirtualTree {
    int size = 1;
    t_graph roads;
    map<int, int> id_to_vid;

    int get (int node) {
        return (*id_to_vid.find(node)).second;
    }

    idata L;
    idata R;
    idata C;

    pair<int, int> dfs (int node, int parent, int coming) {
        C[node] = coming;
        for (const auto road : roads[node]) if (road.first != parent) {
            const auto data = dfs(road.first, node, road.second);

            L[node] = max(L[node], data.first);
            R[node] = min(R[node], data.second);
        }

        return { L[node], R[node] };
    }

    VirtualTree (Tree &tree, iset virtual_set) {
        vd order_positions;
        for (int u : virtual_set) order_positions.push_back({ tree.preordre[u], u });

        sort(order_positions.begin(), order_positions.end());

        for (int i = 0; i + 1 < order_positions.size(); i ++){
            int u = order_positions[i    ].second;
            int v = order_positions[i + 1].second;

            int l = tree.lca(u, v);
            virtual_set.insert(l);
        }

        vd dist_computations;
        for (int u : virtual_set) {
            dist_computations.push_back({ tree.preordre [u], u }); 
            for (int h : tree.eulertour[u])
                dist_computations.push_back({ h, u });
            dist_computations.push_back({ tree.postordre[u], u }); 
        }

        sort(dist_computations.begin(), dist_computations.end());

        set<pair<int, int>> pot_roads;

        for (int i = 0; i + 1 < dist_computations.size(); i ++) {
            int u = dist_computations[i]    .second;
            int v = dist_computations[i + 1].second;

            if (u == v) continue ;
            pot_roads.insert({ min(u, v), max(u, v) });
        }

        int iNode = 0;
        for (int u : virtual_set)
            id_to_vid.insert({ u, iNode ++ });
        
        roads.resize(virtual_set.size());
        for (const auto &road : pot_roads) {
            int u = road.first; int v = road.second;
            int dist = abs(tree.depth[u] - tree.depth[v]);

            size += dist;

            u = get(u); v = get(v);
            roads[u].push_back({ v, dist });
            roads[v].push_back({ u, dist });
        }

        L.resize(virtual_set.size(), - 1e9);
        R.resize(virtual_set.size(),   1e9);
        C.resize(virtual_set.size());
    }
};

struct Query {
    int l, r;
    int answer;

    int order (int mid) {
        if (r < mid) return -1;
        if (l > mid) return 1;
        return 0;
    }
};

VirtualTree create (Tree &tree, idata &R, int left, int right, int mid) {
    iset values;
    for (int u = left; u <= right; u ++)
        values.insert(R[u]);
    
    VirtualTree vt (tree, values);

    for (int u = left; u <= mid; u ++)
        vt.L[vt.get(R[u])] = u;
    for (int u = right; u >= mid; u --)
        vt.R[vt.get(R[u])] = u;
    
    vt.dfs(vt.get(R[mid]), -1, 1);

    return vt;
}

void solve (Tree &tree, idata &R, int l, int r, vector<Query*> queries) { // [l; r]
    if (queries.size() == 0) return ;

    int mid = (l + r) >> 1;
    VirtualTree vt = create(tree, R, l, r, mid);

    vector<Query*> left_q, right_q;
    for (Query* q : queries) {
        int o = q->order(mid);
        if (o == -1)  left_q.push_back(q);
        if (o ==  1) right_q.push_back(q);

        if (o == 0) {
            int res = 0;
            for (int i = 0; i < vt.L.size(); i ++)
                if (q->l <= vt.L[i] || q->r >= vt.R[i])
                    res += vt.C[i];
            
            q->answer = res;
        }
    }

    solve(tree, R, l, mid - 1, left_q);
    solve(tree, R, mid + 1, r, right_q);
}

int main () {
    int N, M, Q;
    cin >> N >> M >> Q;

    igrid roads(N);
    for (int i = 1; i < N; i ++) {
        int a, b;
        cin >> a >> b;
        a --; b --;

        roads[a].push_back(b);
        roads[b].push_back(a);
    }

    Tree tree(N, roads);

    idata R(M);
    for (int i = 0; i < M; i ++) {
        cin >> R[i];
        R[i] --;
    }

    vector<Query*> queries;
    
    for (int q = 0; q < Q; q ++) {
        int l, r;
        cin >> l >> r;
        l --; r --;

        Query* qu = new Query();
        qu->l = l; qu->r = r;
        queries.push_back(qu);
    }

    solve(tree, R, 0, M - 1, queries);

    for (Query* q : queries) {
        cout << q->answer << "\n";
        delete q;
    }
}

Compilation message

tourism.cpp: In constructor 'VirtualTree::VirtualTree(Tree&, iset)':
tourism.cpp:118:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |         for (int i = 0; i + 1 < order_positions.size(); i ++){
      |                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:138:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |         for (int i = 0; i + 1 < dist_computations.size(); i ++) {
      |                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp: In function 'void solve(Tree&, idata&, int, int, std::vector<Query*>)':
tourism.cpp:210:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  210 |             for (int i = 0; i < vt.L.size(); i ++)
      |                             ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 17 ms 1092 KB Output is correct
31 Correct 20 ms 1108 KB Output is correct
32 Correct 27 ms 1484 KB Output is correct
33 Correct 28 ms 1416 KB Output is correct
34 Correct 28 ms 1384 KB Output is correct
35 Correct 11 ms 1364 KB Output is correct
36 Correct 10 ms 1376 KB Output is correct
37 Correct 11 ms 1364 KB Output is correct
38 Correct 15 ms 1452 KB Output is correct
39 Correct 14 ms 1460 KB Output is correct
40 Correct 15 ms 1492 KB Output is correct
41 Correct 6 ms 1436 KB Output is correct
42 Correct 6 ms 1364 KB Output is correct
43 Correct 6 ms 1364 KB Output is correct
44 Correct 17 ms 1388 KB Output is correct
45 Correct 18 ms 1416 KB Output is correct
46 Correct 16 ms 1364 KB Output is correct
47 Correct 6 ms 1388 KB Output is correct
48 Correct 6 ms 1364 KB Output is correct
49 Correct 6 ms 1276 KB Output is correct
50 Correct 30 ms 1424 KB Output is correct
51 Correct 26 ms 1424 KB Output is correct
52 Correct 28 ms 1364 KB Output is correct
53 Correct 29 ms 1420 KB Output is correct
54 Correct 31 ms 1620 KB Output is correct
55 Correct 29 ms 1364 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 2 ms 852 KB Output is correct
58 Correct 26 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2376 ms 38588 KB Output is correct
5 Correct 1795 ms 45920 KB Output is correct
6 Correct 2167 ms 53296 KB Output is correct
7 Correct 4064 ms 62072 KB Output is correct
8 Correct 4033 ms 62080 KB Output is correct
9 Correct 4133 ms 61964 KB Output is correct
10 Correct 3979 ms 62084 KB Output is correct
11 Correct 3912 ms 62100 KB Output is correct
12 Execution timed out 5058 ms 60328 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1087 ms 28868 KB Output is correct
3 Correct 1825 ms 35148 KB Output is correct
4 Correct 1366 ms 35516 KB Output is correct
5 Correct 292 ms 49216 KB Output is correct
6 Correct 485 ms 52036 KB Output is correct
7 Correct 801 ms 52616 KB Output is correct
8 Correct 1131 ms 52708 KB Output is correct
9 Correct 1406 ms 52732 KB Output is correct
10 Correct 1794 ms 52796 KB Output is correct
11 Correct 2028 ms 52772 KB Output is correct
12 Correct 2283 ms 52360 KB Output is correct
13 Correct 2558 ms 53396 KB Output is correct
14 Correct 2753 ms 54528 KB Output is correct
15 Correct 2998 ms 59308 KB Output is correct
16 Correct 1794 ms 53440 KB Output is correct
17 Correct 1781 ms 53560 KB Output is correct
18 Correct 1809 ms 53300 KB Output is correct
19 Correct 275 ms 48512 KB Output is correct
20 Correct 346 ms 49340 KB Output is correct
21 Correct 507 ms 49840 KB Output is correct
22 Correct 648 ms 49652 KB Output is correct
23 Correct 786 ms 49648 KB Output is correct
24 Correct 1053 ms 50064 KB Output is correct
25 Correct 1236 ms 49656 KB Output is correct
26 Correct 1339 ms 49440 KB Output is correct
27 Correct 1641 ms 49424 KB Output is correct
28 Correct 1896 ms 49392 KB Output is correct
29 Correct 2061 ms 49436 KB Output is correct
30 Correct 2260 ms 49724 KB Output is correct
31 Correct 2455 ms 50116 KB Output is correct
32 Correct 2663 ms 50840 KB Output is correct
33 Correct 2786 ms 52768 KB Output is correct
34 Correct 2779 ms 56280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Execution timed out 5060 ms 36000 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 17 ms 1092 KB Output is correct
31 Correct 20 ms 1108 KB Output is correct
32 Correct 27 ms 1484 KB Output is correct
33 Correct 28 ms 1416 KB Output is correct
34 Correct 28 ms 1384 KB Output is correct
35 Correct 11 ms 1364 KB Output is correct
36 Correct 10 ms 1376 KB Output is correct
37 Correct 11 ms 1364 KB Output is correct
38 Correct 15 ms 1452 KB Output is correct
39 Correct 14 ms 1460 KB Output is correct
40 Correct 15 ms 1492 KB Output is correct
41 Correct 6 ms 1436 KB Output is correct
42 Correct 6 ms 1364 KB Output is correct
43 Correct 6 ms 1364 KB Output is correct
44 Correct 17 ms 1388 KB Output is correct
45 Correct 18 ms 1416 KB Output is correct
46 Correct 16 ms 1364 KB Output is correct
47 Correct 6 ms 1388 KB Output is correct
48 Correct 6 ms 1364 KB Output is correct
49 Correct 6 ms 1276 KB Output is correct
50 Correct 30 ms 1424 KB Output is correct
51 Correct 26 ms 1424 KB Output is correct
52 Correct 28 ms 1364 KB Output is correct
53 Correct 29 ms 1420 KB Output is correct
54 Correct 31 ms 1620 KB Output is correct
55 Correct 29 ms 1364 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 2 ms 852 KB Output is correct
58 Correct 26 ms 1492 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 2 ms 340 KB Output is correct
62 Correct 2376 ms 38588 KB Output is correct
63 Correct 1795 ms 45920 KB Output is correct
64 Correct 2167 ms 53296 KB Output is correct
65 Correct 4064 ms 62072 KB Output is correct
66 Correct 4033 ms 62080 KB Output is correct
67 Correct 4133 ms 61964 KB Output is correct
68 Correct 3979 ms 62084 KB Output is correct
69 Correct 3912 ms 62100 KB Output is correct
70 Execution timed out 5058 ms 60328 KB Time limit exceeded
71 Halted 0 ms 0 KB -