Submission #1094631

# Submission time Handle Problem Language Result Execution time Memory
1094631 2024-09-30T07:24:58 Z _8_8_ Tourism (JOI23_tourism) C++17
18 / 100
5000 ms 1048576 KB
#include <bits/stdc++.h>
    
using namespace std;
    
typedef long long ll;
const int  N = 1e5 + 12, MOD = (int)1e9 + 7;

const int L = 19;
vector<int> g[N];
int n, m, q, c[N], tin[N], tout[N], timer, s[N], head[N], ver[N];
int up[N][19], dep[N];
void bb(int v, int pr = -1) {
    s[v] = 1;
    for(int i = 0; i < (int)g[v].size(); i++) {
        int to = g[v][i];
        if(to == pr) continue;
        bb(to, v);
        s[v] += s[to];
        if(s[to] > s[g[v][0]] || g[v][0] == pr) {
            swap(g[v][i], g[v][0]);
        }
    }
}
void hld(int v, int pr = -1) {
    tin[v] = ++timer;
    ver[timer] = v;
    for(int to:g[v]) {
        if(to == pr) continue;
        if(to == g[v][0]) {
            head[to] = head[v];
        } else {
            head[to] = to;
        }
        hld(to, v);
    }
    tout[v] = timer;
}
vector<pair<int,int>> qr[N];

void dfs(int v, int pr = 1) {
    up[v][0] = pr;
    for(int i = 1; i < L; ++i) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for(int to:g[v]) {
        if(to != pr) {
            dep[to] = dep[v] + 1;
            dfs(to, v);
        }
    }
}
bool is(int v, int u) {
    return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v, int u) {
    if(is(v, u)) return v;
    if(is(u, v)) return u;
    for(int i = L - 1; i >= 0; i--) {
        if(!is(up[v][i], u)) {
            v = up[v][i];
        }
    }
    return up[v][0];
}
pair<int, int> t[N * 4];
void build(int v = 1, int tl = 1, int tr = m - 1) {
    if(tl == tr) {
        int x = lca(c[tl], c[tl + 1]);
        t[v] = {dep[x], x};
    } else {
        int tm = (tl + tr) >> 1;
        build(v + v, tl, tm);
        build(v + v + 1, tm + 1, tr);
        t[v] = min(t[v + v], t[v + v + 1]);
    }
}
const int inf = 1e9;
pair<int, int> get(int l, int r, int v = 1, int tl = 1, int tr = m - 1) {
    if(l > r || tl > r || l > tr) return {inf, inf};
    if(tl >= l && tr <= r) return t[v];
    int tm = (tl + tr) >> 1;
    return min(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
}

int mn[N * 4], mod[N * 4], mx[N * 4];

void make(int v, int val) {
    mod[v] = mx[v] = mn[v] = val;
}
void push(int v) {
    if(mod[v]) {
        make(v + v, mod[v]);
        make(v + v + 1, mod[v]);
        mod[v] = 0;
    }
}
void upd(int l, int r, int val, int v = 1, int tl = 1, int tr = n) {
    if(l > r|| tl > r || l > tr) return;
    if(tl >= l && tr <= r) {
        make(v, val);
    } else {
        push(v);
        int tm = (tl + tr) >> 1;
        upd(l, r, val, v + v, tl, tm);
        upd(l, r, val, v + v + 1, tm + 1, tr);
        mx[v] = max(mx[v + v], mx[v + v + 1]);
        mn[v] = min(mn[v + v], mn[v + v + 1]);
    }
}
int find(int val, int v = 1, int tl = 1, int tr = n) { 
    if(mn[v] >= val) {
        return (tr - tl + 1);
    } 
    if(mx[v] < val) {
        return 0;
    }
    push(v);
    int tm = (tl + tr) >> 1;
    return find(val,v + v, tl, tm) + find(val, v + v + 1, tm + 1, tr);
}
void nv(int v, int i) {
    do {
        upd(tin[head[v]], tin[v], i);
        v = up[head[v]][0];
    }while(v != 1);
}
int res[N];
void test() {
    cin >> n >> m >> q;
    for(int i = 1; i <= n - 1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i = 1; i <= m; i++) {
        cin >> c[i];
    }
    dep[1] = 1;
    bb(1);
    head[1] = 1;
    hld(1);
    dfs(1);
    build();
    for(int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;
        if(l == r) {
            res[i] = 1;
            continue;
        }
        qr[r].push_back({l, i});
    }
    for(int i = 1; i <= m; i++) {
        nv(c[i], i);
        for(auto [l, id]:qr[i]) {
            int x = get(l, i - 1).second;
            res[id] = find(l) - dep[x] + 1;
        }
    }
    for(int i = 1; i <= q; i++) {
        cout << res[i] << '\n';
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); 
    
    int t = 1; 
    // cin >> t;
    
    while(t--) 
        test();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 KB Output is correct
2 Correct 2 ms 19036 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 17176 KB Output is correct
5 Correct 2 ms 17244 KB Output is correct
6 Correct 2 ms 17244 KB Output is correct
7 Correct 2 ms 17244 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 2 ms 17260 KB Output is correct
10 Correct 2 ms 17244 KB Output is correct
11 Correct 3 ms 17244 KB Output is correct
12 Correct 3 ms 17244 KB Output is correct
13 Correct 2 ms 17232 KB Output is correct
14 Correct 3 ms 17244 KB Output is correct
15 Correct 2 ms 17244 KB Output is correct
16 Correct 2 ms 17244 KB Output is correct
17 Correct 2 ms 17212 KB Output is correct
18 Correct 2 ms 17244 KB Output is correct
19 Correct 2 ms 17244 KB Output is correct
20 Correct 2 ms 17244 KB Output is correct
21 Correct 3 ms 17244 KB Output is correct
22 Correct 3 ms 17244 KB Output is correct
23 Correct 3 ms 17244 KB Output is correct
24 Correct 3 ms 17244 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 3 ms 17244 KB Output is correct
27 Correct 3 ms 19036 KB Output is correct
28 Runtime error 517 ms 1048576 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 KB Output is correct
2 Correct 2 ms 19036 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 17176 KB Output is correct
5 Correct 2 ms 17244 KB Output is correct
6 Correct 2 ms 17244 KB Output is correct
7 Correct 2 ms 17244 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 2 ms 17260 KB Output is correct
10 Correct 2 ms 17244 KB Output is correct
11 Correct 3 ms 17244 KB Output is correct
12 Correct 3 ms 17244 KB Output is correct
13 Correct 2 ms 17232 KB Output is correct
14 Correct 3 ms 17244 KB Output is correct
15 Correct 2 ms 17244 KB Output is correct
16 Correct 2 ms 17244 KB Output is correct
17 Correct 2 ms 17212 KB Output is correct
18 Correct 2 ms 17244 KB Output is correct
19 Correct 2 ms 17244 KB Output is correct
20 Correct 2 ms 17244 KB Output is correct
21 Correct 3 ms 17244 KB Output is correct
22 Correct 3 ms 17244 KB Output is correct
23 Correct 3 ms 17244 KB Output is correct
24 Correct 3 ms 17244 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 3 ms 17244 KB Output is correct
27 Correct 3 ms 19036 KB Output is correct
28 Runtime error 517 ms 1048576 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 19032 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 3 ms 19292 KB Output is correct
4 Correct 78 ms 32336 KB Output is correct
5 Correct 88 ms 35328 KB Output is correct
6 Correct 79 ms 36432 KB Output is correct
7 Correct 110 ms 39760 KB Output is correct
8 Correct 110 ms 39964 KB Output is correct
9 Correct 110 ms 39332 KB Output is correct
10 Correct 107 ms 39232 KB Output is correct
11 Correct 128 ms 39248 KB Output is correct
12 Correct 93 ms 38344 KB Output is correct
13 Correct 92 ms 38392 KB Output is correct
14 Correct 94 ms 38440 KB Output is correct
15 Runtime error 528 ms 1048576 KB Execution killed with signal 9
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 KB Output is correct
2 Correct 96 ms 23892 KB Output is correct
3 Correct 140 ms 23892 KB Output is correct
4 Correct 127 ms 24660 KB Output is correct
5 Correct 176 ms 25168 KB Output is correct
6 Correct 158 ms 25828 KB Output is correct
7 Correct 162 ms 25280 KB Output is correct
8 Correct 163 ms 25684 KB Output is correct
9 Correct 180 ms 25168 KB Output is correct
10 Correct 182 ms 25680 KB Output is correct
11 Correct 184 ms 25300 KB Output is correct
12 Correct 194 ms 25320 KB Output is correct
13 Correct 210 ms 26196 KB Output is correct
14 Correct 215 ms 26848 KB Output is correct
15 Correct 176 ms 26844 KB Output is correct
16 Correct 194 ms 26196 KB Output is correct
17 Correct 186 ms 26196 KB Output is correct
18 Correct 181 ms 26192 KB Output is correct
19 Correct 153 ms 25376 KB Output is correct
20 Correct 146 ms 25356 KB Output is correct
21 Correct 143 ms 25680 KB Output is correct
22 Correct 144 ms 25172 KB Output is correct
23 Correct 150 ms 25164 KB Output is correct
24 Correct 151 ms 25680 KB Output is correct
25 Correct 147 ms 25708 KB Output is correct
26 Correct 164 ms 25936 KB Output is correct
27 Correct 155 ms 25936 KB Output is correct
28 Correct 153 ms 25684 KB Output is correct
29 Correct 161 ms 25424 KB Output is correct
30 Correct 186 ms 25424 KB Output is correct
31 Correct 170 ms 25712 KB Output is correct
32 Correct 183 ms 26108 KB Output is correct
33 Correct 183 ms 27916 KB Output is correct
34 Correct 149 ms 26448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 KB Output is correct
2 Correct 2 ms 19032 KB Output is correct
3 Correct 3 ms 19292 KB Output is correct
4 Execution timed out 5050 ms 29272 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 KB Output is correct
2 Correct 2 ms 19036 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 17176 KB Output is correct
5 Correct 2 ms 17244 KB Output is correct
6 Correct 2 ms 17244 KB Output is correct
7 Correct 2 ms 17244 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 2 ms 17260 KB Output is correct
10 Correct 2 ms 17244 KB Output is correct
11 Correct 3 ms 17244 KB Output is correct
12 Correct 3 ms 17244 KB Output is correct
13 Correct 2 ms 17232 KB Output is correct
14 Correct 3 ms 17244 KB Output is correct
15 Correct 2 ms 17244 KB Output is correct
16 Correct 2 ms 17244 KB Output is correct
17 Correct 2 ms 17212 KB Output is correct
18 Correct 2 ms 17244 KB Output is correct
19 Correct 2 ms 17244 KB Output is correct
20 Correct 2 ms 17244 KB Output is correct
21 Correct 3 ms 17244 KB Output is correct
22 Correct 3 ms 17244 KB Output is correct
23 Correct 3 ms 17244 KB Output is correct
24 Correct 3 ms 17244 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 3 ms 17244 KB Output is correct
27 Correct 3 ms 19036 KB Output is correct
28 Runtime error 517 ms 1048576 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -