Submission #898441

# Submission time Handle Problem Language Result Execution time Memory
898441 2024-01-04T16:31:19 Z vjudge1 Tourism (JOI23_tourism) C++17
17 / 100
2117 ms 24268 KB
#include <bits/stdc++.h>
using namespace std;



template<class T>
int len(T &a){
    return a.size();
}

using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";


mt19937 mt(time(nullptr));
const int mod = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;



struct Node{
    int mn;
};

struct Sparsemin{


    vector<vector<Node>> t;
    int n, logn;

    void init(int _n){
        n = _n;
        logn = 32 - __builtin_clz(n);
        t.assign(logn, vector<Node> (n));
    }

    Node merge(Node a, Node b){
        if(a.mn < b.mn) return a;
        return b;
    }

    void build(vector<int> &l){
        init(len(l));

        for(int i = 0; i < n; i ++){
            t[0][i] = {l[i]};
        }

        for(int j = 1; j < logn; j ++){
            for(int i = 0; i + (1 << j) - 1 < n; i ++){
                t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
            }
        }
    }

    int get(int l, int r){
        int k = 31 - __builtin_clz(r - l + 1);
        return merge(t[k][l], t[k][r - (1 << k) + 1]).mn;
    }
};



struct Sparsemax{
    vector<vector<Node>> t;
    int n, logn;

    void init(int _n){
        n = _n;
        logn = 32 - __builtin_clz(n);
        t.assign(logn, vector<Node> (n));
    }

    Node merge(Node a, Node b){
        if(a.mn > b.mn) return a;
        return b;
    }

    void build(vector<int> &l){
        init(len(l));

        for(int i = 0; i < n; i ++){
            t[0][i].mn = l[i];
        }

        for(int j = 1; j < logn; j ++){
            for(int i = 0; i + (1 << j) - 1 < n; i ++){
                t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
            }
        }
    }

    int get(int l, int r){
        int k = 31 - __builtin_clz(r - l + 1);
        //cout << l << ' ' << r << ' ' << k << endl;
        return merge(t[k][l], t[k][r - (1 << k) + 1]).mn;
    }
};



void solve(){
    int n, m, q; cin >> n >> m >> q;
    vector<int> sight(m);
    vector<vector<int>> g(n);

    for(int i = 1; i < n; i ++){
        int u, v; cin >> u >> v;
        u --; v --;
        g[u].push_back(v); g[v].push_back(u);
    }

    for(int i = 0; i < m; i ++){
        cin >> sight[i]; sight[i] --;
    }

    vector<pair<int,int>> qu(q);
    for(int i = 0; i < q; i ++){
        cin >> qu[i].ff >> qu[i].ss;
        qu[i].ff --; qu[i].ss --;
    }

    auto brute = [&]{vector<int> par(n);
        auto dfs = [&](auto &dfs, int i, int p = -1)->void{
            par[i] = p;
            for(auto u : g[i]){
                if(u == p) continue;
                dfs(dfs, u, i);
            }
        };
    
        for(auto [l, r] : qu){
            dfs(dfs, sight[l]);
            int cnt = 1;
            vector<int> vis(n);
            vis[sight[l]] = 1;
            for(int j = l + 1; j <= r; j ++){
                int cur = sight[j];
                while(vis[cur] == 0){
                    vis[cur] = 1;
                    //cout << "cur " << cur + 1 << endl;
                    cnt ++; cur = par[cur];
                }
            }
            cout << cnt << '\n';
        }
    };
    Sparsemax tmx;
    Sparsemin tmn;

    tmx.build(sight); tmn.build(sight);

    #ifdef LOCAL
    brute(); cout << "check: " << endl;
    #else
    if(n <= 2000) {brute(); return;}
    #endif

    for(auto [l, r] : qu){
        cout << tmx.get(l, r) - tmn.get(l, r) + 1 << '\n';
    }
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    auto start = chrono::high_resolution_clock::now();
    #ifndef LOCAL
    if(fileio.size()){
        freopen((fileio + ".in").c_str(), "r", stdin);
        freopen((fileio + ".out").c_str(), "w", stdout);
    }
    #endif
    int testcases = 1;
    #ifdef Tests
    cin >> testcases;
    #endif
    while(testcases --){
        solve();
        cout << '\n';
        #ifdef LOCAL
        cout << "_________________________" << endl;
        #endif
    }
    #ifdef LOCAL
    auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
    
    cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
    #endif
    return 0;
}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:172:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
  172 |     auto start = chrono::high_resolution_clock::now();
      |          ^~~~~
tourism.cpp:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  175 |         freopen((fileio + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:176:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |         freopen((fileio + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 380 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 380 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 26 ms 700 KB Output is correct
31 Correct 33 ms 604 KB Output is correct
32 Correct 55 ms 764 KB Output is correct
33 Correct 56 ms 600 KB Output is correct
34 Correct 57 ms 852 KB Output is correct
35 Correct 53 ms 604 KB Output is correct
36 Correct 54 ms 784 KB Output is correct
37 Correct 53 ms 600 KB Output is correct
38 Correct 44 ms 820 KB Output is correct
39 Correct 41 ms 808 KB Output is correct
40 Correct 43 ms 604 KB Output is correct
41 Correct 44 ms 604 KB Output is correct
42 Correct 45 ms 604 KB Output is correct
43 Correct 44 ms 604 KB Output is correct
44 Correct 42 ms 604 KB Output is correct
45 Correct 42 ms 604 KB Output is correct
46 Correct 47 ms 600 KB Output is correct
47 Correct 43 ms 604 KB Output is correct
48 Correct 44 ms 600 KB Output is correct
49 Correct 44 ms 856 KB Output is correct
50 Correct 19 ms 604 KB Output is correct
51 Correct 20 ms 604 KB Output is correct
52 Correct 20 ms 604 KB Output is correct
53 Correct 23 ms 792 KB Output is correct
54 Correct 20 ms 604 KB Output is correct
55 Correct 20 ms 604 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 25 ms 348 KB Output is correct
58 Correct 43 ms 776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 36 ms 16880 KB Output is correct
5 Correct 37 ms 15228 KB Output is correct
6 Correct 43 ms 19936 KB Output is correct
7 Correct 51 ms 24260 KB Output is correct
8 Correct 50 ms 24092 KB Output is correct
9 Correct 53 ms 24268 KB Output is correct
10 Correct 51 ms 24264 KB Output is correct
11 Correct 50 ms 24268 KB Output is correct
12 Correct 49 ms 23968 KB Output is correct
13 Correct 48 ms 24008 KB Output is correct
14 Correct 48 ms 24012 KB Output is correct
15 Correct 32 ms 8336 KB Output is correct
16 Correct 49 ms 23756 KB Output is correct
17 Correct 2117 ms 17056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 19 ms 10628 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Incorrect 41 ms 17320 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 380 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 26 ms 700 KB Output is correct
31 Correct 33 ms 604 KB Output is correct
32 Correct 55 ms 764 KB Output is correct
33 Correct 56 ms 600 KB Output is correct
34 Correct 57 ms 852 KB Output is correct
35 Correct 53 ms 604 KB Output is correct
36 Correct 54 ms 784 KB Output is correct
37 Correct 53 ms 600 KB Output is correct
38 Correct 44 ms 820 KB Output is correct
39 Correct 41 ms 808 KB Output is correct
40 Correct 43 ms 604 KB Output is correct
41 Correct 44 ms 604 KB Output is correct
42 Correct 45 ms 604 KB Output is correct
43 Correct 44 ms 604 KB Output is correct
44 Correct 42 ms 604 KB Output is correct
45 Correct 42 ms 604 KB Output is correct
46 Correct 47 ms 600 KB Output is correct
47 Correct 43 ms 604 KB Output is correct
48 Correct 44 ms 600 KB Output is correct
49 Correct 44 ms 856 KB Output is correct
50 Correct 19 ms 604 KB Output is correct
51 Correct 20 ms 604 KB Output is correct
52 Correct 20 ms 604 KB Output is correct
53 Correct 23 ms 792 KB Output is correct
54 Correct 20 ms 604 KB Output is correct
55 Correct 20 ms 604 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 25 ms 348 KB Output is correct
58 Correct 43 ms 776 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 2 ms 604 KB Output is correct
62 Correct 36 ms 16880 KB Output is correct
63 Correct 37 ms 15228 KB Output is correct
64 Correct 43 ms 19936 KB Output is correct
65 Correct 51 ms 24260 KB Output is correct
66 Correct 50 ms 24092 KB Output is correct
67 Correct 53 ms 24268 KB Output is correct
68 Correct 51 ms 24264 KB Output is correct
69 Correct 50 ms 24268 KB Output is correct
70 Correct 49 ms 23968 KB Output is correct
71 Correct 48 ms 24008 KB Output is correct
72 Correct 48 ms 24012 KB Output is correct
73 Correct 32 ms 8336 KB Output is correct
74 Correct 49 ms 23756 KB Output is correct
75 Correct 2117 ms 17056 KB Output is correct
76 Correct 1 ms 344 KB Output is correct
77 Incorrect 19 ms 10628 KB Output isn't correct
78 Halted 0 ms 0 KB -