답안 #892731

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
892731 2023-12-25T19:02:53 Z LucaIlie Tourism (JOI23_tourism) C++17
10 / 100
5000 ms 95044 KB
#include <bits/stdc++.h>

using namespace std;

struct Tree {
    struct edge {
        int u, v, c;

        int other( int w ) {
            return u ^ v ^ w;
        }
    };

    int n;
    vector<edge> edges;
    vector<vector<int>> adj;
    vector<int> depth, timeIn, timeOut;
    vector<vector<int>> parent;
    vector<int> maxLQ, minHQ;

    void resize( int _size ) {
        n = _size;

        edges.clear();
        adj.clear();
        adj.resize( n + 1 );

        depth.clear();
        depth.resize( n + 1 );
        timeIn.clear();
        timeIn.resize( n + 1 );
        timeOut.clear();
        timeOut.resize( n + 1 );

        maxLQ.resize( n + 1 );
        for ( int i = 0; i <= n; i++ )
            maxLQ[i] = 0;
        minHQ.resize( n + 1 );
        for ( int i = 0; i <= n; i++ )
            minHQ[i] = INT_MAX;

        parent.clear();
        parent.resize( (int)log2( n ) + 1 );
        for ( int i = 0; i <= log2( n ); i++ )
            parent[i].resize( n + 1 );
    }

    void add_edge( int u, int v, int c ) {
        adj[u].push_back( edges.size() );
        adj[v].push_back( edges.size() );
        edges.push_back( { u, v, c } );
    }
    void addMaxLQ( int v, int val ) {
        maxLQ[v] = max( maxLQ[v], val );
    }

    void addMinHQ( int v, int val ) {
        minHQ[v] = min( minHQ[v], val );
    }

    int timee;
    void dfs( int u, int p ) {
        parent[0][u] = p;
        timeIn[u] = ++timee;
        for ( int e: adj[u] ) {
            int v = edges[e].other( u ), c = edges[e].c;
            if ( v == p )
                continue;
            depth[v] = depth[u] + c;
            dfs( v, u );
            maxLQ[u] = max( maxLQ[u], maxLQ[v] );
            minHQ[u] = min( minHQ[u], minHQ[v] );
        }
        timeOut[u] = timee;
    }

    void init( int root ) {
        timee = 0;
        dfs( root, 0 );
        for ( int l = 1; l <= log2( n ); l++ ) {
            for ( int v = 1; v <= n; v++ )
                parent[l][v] = parent[l - 1][parent[l - 1][v]];
        }
    }

    int lca( int u, int v ) {
        if ( depth[u] > depth[v] )
            swap( u, v );

        for ( int l = log2( n ); l >= 0; l-- ) {
            if ( depth[parent[l][v]] >= depth[u] )
                v = parent[l][v];
        }

        if ( u == v )
            return v;

        for ( int l = log2( n ); l >= 0; l-- ) {
             if ( parent[l][u] != parent[l][v] ) {
                 u = parent[l][u];
                 v = parent[l][v];
             }
        }

        return parent[0][v];
    }

    bool inSubtree( int u, int v ) {
        return (timeIn[u] <= timeIn[v]) && (timeOut[v] <= timeOut[u]);
    }

};

const int MAX_M = 1e5;
const int MAX_Q = 1e5;
int answer[3000][3000];
Tree islandTree;
int spots[MAX_M], leftQ[MAX_Q], rightQ[MAX_Q];
unordered_map<int, int> valToPos;

void solve( int l, int r ) {
    if ( l > r )
        return;

    int mid = (l + r) / 2;
    solve( l, mid - 1 );
    solve( mid + 1, r );

    vector<int> rangeSpots, virtualSpots;
    virtualSpots.push_back( 0 );
    for ( int i = l; i <= r; i++ ) {
        rangeSpots.push_back( spots[i] );
        virtualSpots.push_back( spots[i] );
    }
    sort( rangeSpots.begin(), rangeSpots.end(), []( int u, int v ) { return islandTree.timeIn[u] < islandTree.timeIn[v]; } );
    for ( int i = 0; i < rangeSpots.size() - 1; i++ )
        virtualSpots.push_back( islandTree.lca( rangeSpots[i], rangeSpots[i + 1] ) );
    sort( virtualSpots.begin(), virtualSpots.end(), []( int u, int v ) { return islandTree.timeIn[u] < islandTree.timeIn[v]; } );
    virtualSpots.resize( unique( virtualSpots.begin(), virtualSpots.end() ) - virtualSpots.begin() );

    Tree virtualSpotsTree;
    virtualSpotsTree.resize( virtualSpots.size() - 1 );

    vector<int> stack;
    stack.push_back( 1 );
    for ( int i = 2; i < virtualSpots.size(); i++ ) {
        while ( !stack.empty() && !islandTree.inSubtree( virtualSpots[stack.back()], virtualSpots[i] ) ) {
            stack.pop_back();
        }
        virtualSpotsTree.add_edge( stack.back(), i, islandTree.depth[virtualSpots[i]] - islandTree.depth[virtualSpots[stack.back()]] );
        stack.push_back( i );
    }

    valToPos.clear();
    for ( int i = 1; i < virtualSpots.size(); i++ )
        valToPos[virtualSpots[i]] = i;
    for ( int i = l; i <= mid; i++ )
        virtualSpotsTree.addMaxLQ( valToPos[spots[i]], i );
    for ( int i = mid; i <= r; i++ )
        virtualSpotsTree.addMinHQ( valToPos[spots[i]], i );
    virtualSpotsTree.init( valToPos[spots[mid]] );

    for ( auto e: virtualSpotsTree.edges ) {
        if ( virtualSpotsTree.depth[e.u] > virtualSpotsTree.depth[e.v] )
            swap( e.u, e.v );

        /*for ( int i = virtualSpotsTree.maxLQ[e.v]; i >= l; i-- ) {
            for ( int j = mid; j <= r; j++ )
                answer[i][j] += e.c;
        }
        for ( int j = virtualSpotsTree.minHQ[e.v]; j <= r; j++ ) {
            for ( int i = mid; i > virtualSpotsTree.maxLQ[e.v]; i-- )
                answer[i][j] += e.c;
        }*/

        for ( int i = l; i <= mid; i++ ) {
            for ( int j = mid; j <= r; j++ ) {
                if ( i <= virtualSpotsTree.maxLQ[e.v] || j >= virtualSpotsTree.minHQ[e.v] )
                    answer[i][j] += e.c;
            }
        }
    }
}

int main() {
    int n, m, q;

    cin >> n >> m >> q;
    islandTree.resize( n );
    for ( int i = 0; i < n - 1; i++ ) {
        int u, v;
        cin >> u >> v;
        islandTree.add_edge( u, v, 1 );
    }
    for ( int i = 1; i <= m; i++ )
        cin >> spots[i];
    for ( int i = 1; i <= q; i++ )
        cin >> leftQ[i] >> rightQ[i];

    islandTree.init( 1 );
    solve( 1, m );

    for ( int i = 1; i <= q; i++ )
        cout << answer[leftQ[i]][rightQ[i]] + 1 << "\n";

    return 0;
}

Compilation message

tourism.cpp: In function 'void solve(int, int)':
tourism.cpp:136:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |     for ( int i = 0; i < rangeSpots.size() - 1; i++ )
      |                      ~~^~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:146:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |     for ( int i = 2; i < virtualSpots.size(); i++ ) {
      |                      ~~^~~~~~~~~~~~~~~~~~~~~
tourism.cpp:155:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |     for ( int i = 1; i < virtualSpots.size(); i++ )
      |                      ~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 4 ms 4456 KB Output is correct
5 Correct 3 ms 4540 KB Output is correct
6 Correct 4 ms 4440 KB Output is correct
7 Correct 3 ms 4444 KB Output is correct
8 Correct 3 ms 4444 KB Output is correct
9 Correct 8 ms 6488 KB Output is correct
10 Correct 8 ms 6744 KB Output is correct
11 Correct 11 ms 6700 KB Output is correct
12 Correct 12 ms 6716 KB Output is correct
13 Correct 7 ms 6488 KB Output is correct
14 Correct 13 ms 6852 KB Output is correct
15 Correct 6 ms 6488 KB Output is correct
16 Correct 6 ms 6488 KB Output is correct
17 Correct 6 ms 6712 KB Output is correct
18 Correct 7 ms 6492 KB Output is correct
19 Correct 7 ms 6492 KB Output is correct
20 Correct 6 ms 6600 KB Output is correct
21 Correct 7 ms 6744 KB Output is correct
22 Correct 7 ms 6492 KB Output is correct
23 Correct 7 ms 6492 KB Output is correct
24 Correct 7 ms 6600 KB Output is correct
25 Correct 7 ms 6492 KB Output is correct
26 Correct 7 ms 6596 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 8 ms 6664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 4 ms 4456 KB Output is correct
5 Correct 3 ms 4540 KB Output is correct
6 Correct 4 ms 4440 KB Output is correct
7 Correct 3 ms 4444 KB Output is correct
8 Correct 3 ms 4444 KB Output is correct
9 Correct 8 ms 6488 KB Output is correct
10 Correct 8 ms 6744 KB Output is correct
11 Correct 11 ms 6700 KB Output is correct
12 Correct 12 ms 6716 KB Output is correct
13 Correct 7 ms 6488 KB Output is correct
14 Correct 13 ms 6852 KB Output is correct
15 Correct 6 ms 6488 KB Output is correct
16 Correct 6 ms 6488 KB Output is correct
17 Correct 6 ms 6712 KB Output is correct
18 Correct 7 ms 6492 KB Output is correct
19 Correct 7 ms 6492 KB Output is correct
20 Correct 6 ms 6600 KB Output is correct
21 Correct 7 ms 6744 KB Output is correct
22 Correct 7 ms 6492 KB Output is correct
23 Correct 7 ms 6492 KB Output is correct
24 Correct 7 ms 6600 KB Output is correct
25 Correct 7 ms 6492 KB Output is correct
26 Correct 7 ms 6596 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 8 ms 6664 KB Output is correct
30 Correct 651 ms 19344 KB Output is correct
31 Correct 788 ms 21584 KB Output is correct
32 Correct 1629 ms 25712 KB Output is correct
33 Correct 1569 ms 25696 KB Output is correct
34 Correct 1661 ms 25684 KB Output is correct
35 Correct 1682 ms 25928 KB Output is correct
36 Correct 1575 ms 25744 KB Output is correct
37 Correct 1635 ms 25948 KB Output is correct
38 Correct 1205 ms 25776 KB Output is correct
39 Correct 1217 ms 25684 KB Output is correct
40 Correct 1186 ms 25724 KB Output is correct
41 Correct 1207 ms 25728 KB Output is correct
42 Correct 1238 ms 25772 KB Output is correct
43 Correct 1267 ms 25752 KB Output is correct
44 Correct 1425 ms 25700 KB Output is correct
45 Correct 1265 ms 25824 KB Output is correct
46 Correct 1238 ms 25696 KB Output is correct
47 Correct 1284 ms 25816 KB Output is correct
48 Correct 1260 ms 25680 KB Output is correct
49 Correct 1232 ms 25716 KB Output is correct
50 Correct 1419 ms 25684 KB Output is correct
51 Correct 1382 ms 25656 KB Output is correct
52 Correct 1346 ms 25680 KB Output is correct
53 Correct 1406 ms 25712 KB Output is correct
54 Correct 1369 ms 25672 KB Output is correct
55 Correct 1360 ms 25716 KB Output is correct
56 Correct 3 ms 2396 KB Output is correct
57 Correct 2 ms 2652 KB Output is correct
58 Correct 1617 ms 25684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 3 ms 2492 KB Output is correct
4 Runtime error 4684 ms 95044 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Runtime error 2334 ms 90200 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2408 KB Output is correct
3 Correct 3 ms 2508 KB Output is correct
4 Execution timed out 5084 ms 44628 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 4 ms 4456 KB Output is correct
5 Correct 3 ms 4540 KB Output is correct
6 Correct 4 ms 4440 KB Output is correct
7 Correct 3 ms 4444 KB Output is correct
8 Correct 3 ms 4444 KB Output is correct
9 Correct 8 ms 6488 KB Output is correct
10 Correct 8 ms 6744 KB Output is correct
11 Correct 11 ms 6700 KB Output is correct
12 Correct 12 ms 6716 KB Output is correct
13 Correct 7 ms 6488 KB Output is correct
14 Correct 13 ms 6852 KB Output is correct
15 Correct 6 ms 6488 KB Output is correct
16 Correct 6 ms 6488 KB Output is correct
17 Correct 6 ms 6712 KB Output is correct
18 Correct 7 ms 6492 KB Output is correct
19 Correct 7 ms 6492 KB Output is correct
20 Correct 6 ms 6600 KB Output is correct
21 Correct 7 ms 6744 KB Output is correct
22 Correct 7 ms 6492 KB Output is correct
23 Correct 7 ms 6492 KB Output is correct
24 Correct 7 ms 6600 KB Output is correct
25 Correct 7 ms 6492 KB Output is correct
26 Correct 7 ms 6596 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 8 ms 6664 KB Output is correct
30 Correct 651 ms 19344 KB Output is correct
31 Correct 788 ms 21584 KB Output is correct
32 Correct 1629 ms 25712 KB Output is correct
33 Correct 1569 ms 25696 KB Output is correct
34 Correct 1661 ms 25684 KB Output is correct
35 Correct 1682 ms 25928 KB Output is correct
36 Correct 1575 ms 25744 KB Output is correct
37 Correct 1635 ms 25948 KB Output is correct
38 Correct 1205 ms 25776 KB Output is correct
39 Correct 1217 ms 25684 KB Output is correct
40 Correct 1186 ms 25724 KB Output is correct
41 Correct 1207 ms 25728 KB Output is correct
42 Correct 1238 ms 25772 KB Output is correct
43 Correct 1267 ms 25752 KB Output is correct
44 Correct 1425 ms 25700 KB Output is correct
45 Correct 1265 ms 25824 KB Output is correct
46 Correct 1238 ms 25696 KB Output is correct
47 Correct 1284 ms 25816 KB Output is correct
48 Correct 1260 ms 25680 KB Output is correct
49 Correct 1232 ms 25716 KB Output is correct
50 Correct 1419 ms 25684 KB Output is correct
51 Correct 1382 ms 25656 KB Output is correct
52 Correct 1346 ms 25680 KB Output is correct
53 Correct 1406 ms 25712 KB Output is correct
54 Correct 1369 ms 25672 KB Output is correct
55 Correct 1360 ms 25716 KB Output is correct
56 Correct 3 ms 2396 KB Output is correct
57 Correct 2 ms 2652 KB Output is correct
58 Correct 1617 ms 25684 KB Output is correct
59 Correct 1 ms 2396 KB Output is correct
60 Correct 1 ms 2396 KB Output is correct
61 Correct 3 ms 2492 KB Output is correct
62 Runtime error 4684 ms 95044 KB Execution killed with signal 11
63 Halted 0 ms 0 KB -