Submission #1094633

# Submission time Handle Problem Language Result Execution time Memory
1094633 2024-09-30T07:26:18 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 3 ms 16988 KB Output is correct
2 Correct 2 ms 19036 KB Output is correct
3 Correct 2 ms 16984 KB Output is correct
4 Correct 3 ms 17244 KB Output is correct
5 Correct 3 ms 17244 KB Output is correct
6 Correct 2 ms 17244 KB Output is correct
7 Correct 3 ms 17244 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 4 ms 17244 KB Output is correct
10 Correct 3 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 3 ms 17264 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 17244 KB Output is correct
18 Correct 3 ms 17240 KB Output is correct
19 Correct 2 ms 17244 KB Output is correct
20 Correct 3 ms 17240 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 17240 KB Output is correct
24 Correct 4 ms 17256 KB Output is correct
25 Correct 3 ms 17256 KB Output is correct
26 Correct 3 ms 17256 KB Output is correct
27 Correct 2 ms 19048 KB Output is correct
28 Runtime error 529 ms 1048576 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 2 ms 19036 KB Output is correct
3 Correct 2 ms 16984 KB Output is correct
4 Correct 3 ms 17244 KB Output is correct
5 Correct 3 ms 17244 KB Output is correct
6 Correct 2 ms 17244 KB Output is correct
7 Correct 3 ms 17244 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 4 ms 17244 KB Output is correct
10 Correct 3 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 3 ms 17264 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 17244 KB Output is correct
18 Correct 3 ms 17240 KB Output is correct
19 Correct 2 ms 17244 KB Output is correct
20 Correct 3 ms 17240 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 17240 KB Output is correct
24 Correct 4 ms 17256 KB Output is correct
25 Correct 3 ms 17256 KB Output is correct
26 Correct 3 ms 17256 KB Output is correct
27 Correct 2 ms 19048 KB Output is correct
28 Runtime error 529 ms 1048576 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19048 KB Output is correct
2 Correct 2 ms 19048 KB Output is correct
3 Correct 3 ms 19304 KB Output is correct
4 Correct 80 ms 30452 KB Output is correct
5 Correct 77 ms 33120 KB Output is correct
6 Correct 119 ms 34400 KB Output is correct
7 Correct 118 ms 36916 KB Output is correct
8 Correct 136 ms 36976 KB Output is correct
9 Correct 113 ms 36448 KB Output is correct
10 Correct 117 ms 36448 KB Output is correct
11 Correct 114 ms 36440 KB Output is correct
12 Correct 98 ms 35672 KB Output is correct
13 Correct 130 ms 35664 KB Output is correct
14 Correct 96 ms 35700 KB Output is correct
15 Runtime error 543 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 99 ms 23912 KB Output is correct
3 Correct 146 ms 23888 KB Output is correct
4 Correct 116 ms 24692 KB Output is correct
5 Correct 154 ms 25268 KB Output is correct
6 Correct 155 ms 25584 KB Output is correct
7 Correct 160 ms 25068 KB Output is correct
8 Correct 156 ms 25680 KB Output is correct
9 Correct 179 ms 25052 KB Output is correct
10 Correct 206 ms 25820 KB Output is correct
11 Correct 180 ms 25308 KB Output is correct
12 Correct 183 ms 25168 KB Output is correct
13 Correct 200 ms 26196 KB Output is correct
14 Correct 239 ms 26704 KB Output is correct
15 Correct 173 ms 26968 KB Output is correct
16 Correct 179 ms 26196 KB Output is correct
17 Correct 186 ms 26192 KB Output is correct
18 Correct 180 ms 25968 KB Output is correct
19 Correct 144 ms 25348 KB Output is correct
20 Correct 150 ms 25352 KB Output is correct
21 Correct 148 ms 25872 KB Output is correct
22 Correct 148 ms 25160 KB Output is correct
23 Correct 165 ms 25168 KB Output is correct
24 Correct 157 ms 25852 KB Output is correct
25 Correct 152 ms 25644 KB Output is correct
26 Correct 149 ms 25680 KB Output is correct
27 Correct 153 ms 25912 KB Output is correct
28 Correct 168 ms 25884 KB Output is correct
29 Correct 173 ms 25424 KB Output is correct
30 Correct 181 ms 25376 KB Output is correct
31 Correct 174 ms 25600 KB Output is correct
32 Correct 181 ms 25940 KB Output is correct
33 Correct 187 ms 27872 KB Output is correct
34 Correct 158 ms 26452 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 5071 ms 26912 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 2 ms 19036 KB Output is correct
3 Correct 2 ms 16984 KB Output is correct
4 Correct 3 ms 17244 KB Output is correct
5 Correct 3 ms 17244 KB Output is correct
6 Correct 2 ms 17244 KB Output is correct
7 Correct 3 ms 17244 KB Output is correct
8 Correct 3 ms 17244 KB Output is correct
9 Correct 4 ms 17244 KB Output is correct
10 Correct 3 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 3 ms 17264 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 17244 KB Output is correct
18 Correct 3 ms 17240 KB Output is correct
19 Correct 2 ms 17244 KB Output is correct
20 Correct 3 ms 17240 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 17240 KB Output is correct
24 Correct 4 ms 17256 KB Output is correct
25 Correct 3 ms 17256 KB Output is correct
26 Correct 3 ms 17256 KB Output is correct
27 Correct 2 ms 19048 KB Output is correct
28 Runtime error 529 ms 1048576 KB Execution killed with signal 9
29 Halted 0 ms 0 KB -