답안 #823682

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
823682 2023-08-13T00:48:55 Z thimote75 Tourism (JOI23_tourism) C++14
10 / 100
5000 ms 46388 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;
    }
};

struct SegTree {
    vector<int> tree;

    SegTree () = default;
    SegTree (int size) {
        tree.resize(size);
    }
    int query (int node) { return tree[node]; }
    void add (int l, int r, int x) {
        for (int i = l; i <= r; i ++)
            tree[i] += x;
    }
};

const int MAXS = 2e5;

SegTree stree(MAXS);

using Point = pair<int, pair<int, int>>;

struct QueryManager {
    static void solve (vector<Query*> queries, vector<Point> points) {
        sort(points.begin(), points.end());

        vd queries_time;
        for (int i = 0; i < queries.size(); i ++)
            queries_time.push_back({ queries[i]->r, i });
        sort(queries_time.begin(), queries_time.end());

        for (int iOP = 0; iOP < points.size(); iOP ++)
            stree.add(0, max(-1, points[iOP].second.first), points[iOP].second.second);
        
        int bonus = 0;
        int iOP = 0;
        for (int iQ = 0; iQ < queries.size(); iQ ++) {
            int time = queries_time[iQ].first;
            int quid = queries_time[iQ].second;

            while (iOP != points.size() && time >= points[iOP].first) {
                stree.add(0, max(-1, points[iOP].second.first), - points[iOP].second.second);
                bonus += points[iOP].second.second;
                iOP ++;
            }

            queries[quid]->answer = bonus + stree.query(queries[quid]->l);
        }

        for (; iOP < points.size(); iOP ++)
            stree.add(0, max(-1, points[iOP].second.first), - points[iOP].second.second);
    }
};

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;
    vector<Query*> mid_q;
    vector<Point> points;
    for (int i = 0; i < vt.L.size(); i ++)
        points.push_back({ vt.R[i], { vt.L[i], vt.C[i] } });
    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) {
            mid_q.push_back(q);
        }
    }

    QueryManager::solve(mid_q, points);

    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 static member function 'static void QueryManager::solve(std::vector<Query*>, std::vector<std::pair<int, std::pair<int, int> > >)':
tourism.cpp:204:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Query*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  204 |         for (int i = 0; i < queries.size(); i ++)
      |                         ~~^~~~~~~~~~~~~~~~
tourism.cpp:208:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  208 |         for (int iOP = 0; iOP < points.size(); iOP ++)
      |                           ~~~~^~~~~~~~~~~~~~~
tourism.cpp:213:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Query*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  213 |         for (int iQ = 0; iQ < queries.size(); iQ ++) {
      |                          ~~~^~~~~~~~~~~~~~~~
tourism.cpp:217:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  217 |             while (iOP != points.size() && time >= points[iOP].first) {
      |                    ~~~~^~~~~~~~~~~~~~~~
tourism.cpp:226:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  226 |         for (; iOP < points.size(); iOP ++)
      |                ~~~~^~~~~~~~~~~~~~~
tourism.cpp: In function 'void solve(Tree&, idata&, int, int, std::vector<Query*>)':
tourism.cpp:257:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  257 |     for (int i = 0; i < vt.L.size(); i ++)
      |                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1096 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 2 ms 1236 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 2 ms 1220 KB Output is correct
8 Correct 2 ms 1236 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 3 ms 1236 KB Output is correct
11 Correct 3 ms 1236 KB Output is correct
12 Correct 2 ms 1236 KB Output is correct
13 Correct 1 ms 1236 KB Output is correct
14 Correct 1 ms 1236 KB Output is correct
15 Correct 2 ms 1232 KB Output is correct
16 Correct 2 ms 1236 KB Output is correct
17 Correct 3 ms 1236 KB Output is correct
18 Correct 3 ms 1236 KB Output is correct
19 Correct 2 ms 1236 KB Output is correct
20 Correct 3 ms 1236 KB Output is correct
21 Correct 3 ms 1236 KB Output is correct
22 Correct 3 ms 1236 KB Output is correct
23 Correct 3 ms 1236 KB Output is correct
24 Correct 3 ms 1236 KB Output is correct
25 Correct 3 ms 1228 KB Output is correct
26 Correct 3 ms 1236 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 4 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1096 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 2 ms 1236 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 2 ms 1220 KB Output is correct
8 Correct 2 ms 1236 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 3 ms 1236 KB Output is correct
11 Correct 3 ms 1236 KB Output is correct
12 Correct 2 ms 1236 KB Output is correct
13 Correct 1 ms 1236 KB Output is correct
14 Correct 1 ms 1236 KB Output is correct
15 Correct 2 ms 1232 KB Output is correct
16 Correct 2 ms 1236 KB Output is correct
17 Correct 3 ms 1236 KB Output is correct
18 Correct 3 ms 1236 KB Output is correct
19 Correct 2 ms 1236 KB Output is correct
20 Correct 3 ms 1236 KB Output is correct
21 Correct 3 ms 1236 KB Output is correct
22 Correct 3 ms 1236 KB Output is correct
23 Correct 3 ms 1236 KB Output is correct
24 Correct 3 ms 1236 KB Output is correct
25 Correct 3 ms 1228 KB Output is correct
26 Correct 3 ms 1236 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 4 ms 1236 KB Output is correct
30 Correct 18 ms 2004 KB Output is correct
31 Correct 20 ms 2096 KB Output is correct
32 Correct 25 ms 2388 KB Output is correct
33 Correct 27 ms 2388 KB Output is correct
34 Correct 26 ms 2372 KB Output is correct
35 Correct 6 ms 2132 KB Output is correct
36 Correct 6 ms 2172 KB Output is correct
37 Correct 6 ms 2132 KB Output is correct
38 Correct 23 ms 2340 KB Output is correct
39 Correct 24 ms 2484 KB Output is correct
40 Correct 22 ms 2260 KB Output is correct
41 Correct 6 ms 2260 KB Output is correct
42 Correct 7 ms 2280 KB Output is correct
43 Correct 6 ms 2260 KB Output is correct
44 Correct 24 ms 2260 KB Output is correct
45 Correct 25 ms 2328 KB Output is correct
46 Correct 23 ms 2260 KB Output is correct
47 Correct 6 ms 2232 KB Output is correct
48 Correct 6 ms 2132 KB Output is correct
49 Correct 6 ms 2228 KB Output is correct
50 Correct 27 ms 2260 KB Output is correct
51 Correct 32 ms 2264 KB Output is correct
52 Correct 25 ms 2260 KB Output is correct
53 Correct 26 ms 2260 KB Output is correct
54 Correct 25 ms 2260 KB Output is correct
55 Correct 26 ms 2332 KB Output is correct
56 Correct 2 ms 1236 KB Output is correct
57 Correct 3 ms 1748 KB Output is correct
58 Correct 41 ms 2360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 2 ms 1236 KB Output is correct
4 Execution timed out 5059 ms 42596 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Execution timed out 5082 ms 32016 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1104 KB Output is correct
2 Correct 1 ms 1104 KB Output is correct
3 Correct 2 ms 1236 KB Output is correct
4 Execution timed out 5054 ms 46388 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1096 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 2 ms 1236 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 2 ms 1220 KB Output is correct
8 Correct 2 ms 1236 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 3 ms 1236 KB Output is correct
11 Correct 3 ms 1236 KB Output is correct
12 Correct 2 ms 1236 KB Output is correct
13 Correct 1 ms 1236 KB Output is correct
14 Correct 1 ms 1236 KB Output is correct
15 Correct 2 ms 1232 KB Output is correct
16 Correct 2 ms 1236 KB Output is correct
17 Correct 3 ms 1236 KB Output is correct
18 Correct 3 ms 1236 KB Output is correct
19 Correct 2 ms 1236 KB Output is correct
20 Correct 3 ms 1236 KB Output is correct
21 Correct 3 ms 1236 KB Output is correct
22 Correct 3 ms 1236 KB Output is correct
23 Correct 3 ms 1236 KB Output is correct
24 Correct 3 ms 1236 KB Output is correct
25 Correct 3 ms 1228 KB Output is correct
26 Correct 3 ms 1236 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 4 ms 1236 KB Output is correct
30 Correct 18 ms 2004 KB Output is correct
31 Correct 20 ms 2096 KB Output is correct
32 Correct 25 ms 2388 KB Output is correct
33 Correct 27 ms 2388 KB Output is correct
34 Correct 26 ms 2372 KB Output is correct
35 Correct 6 ms 2132 KB Output is correct
36 Correct 6 ms 2172 KB Output is correct
37 Correct 6 ms 2132 KB Output is correct
38 Correct 23 ms 2340 KB Output is correct
39 Correct 24 ms 2484 KB Output is correct
40 Correct 22 ms 2260 KB Output is correct
41 Correct 6 ms 2260 KB Output is correct
42 Correct 7 ms 2280 KB Output is correct
43 Correct 6 ms 2260 KB Output is correct
44 Correct 24 ms 2260 KB Output is correct
45 Correct 25 ms 2328 KB Output is correct
46 Correct 23 ms 2260 KB Output is correct
47 Correct 6 ms 2232 KB Output is correct
48 Correct 6 ms 2132 KB Output is correct
49 Correct 6 ms 2228 KB Output is correct
50 Correct 27 ms 2260 KB Output is correct
51 Correct 32 ms 2264 KB Output is correct
52 Correct 25 ms 2260 KB Output is correct
53 Correct 26 ms 2260 KB Output is correct
54 Correct 25 ms 2260 KB Output is correct
55 Correct 26 ms 2332 KB Output is correct
56 Correct 2 ms 1236 KB Output is correct
57 Correct 3 ms 1748 KB Output is correct
58 Correct 41 ms 2360 KB Output is correct
59 Correct 1 ms 1108 KB Output is correct
60 Correct 1 ms 1108 KB Output is correct
61 Correct 2 ms 1236 KB Output is correct
62 Execution timed out 5059 ms 42596 KB Time limit exceeded
63 Halted 0 ms 0 KB -