답안 #1094713

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1094713 2024-09-30T10:36:07 Z _8_8_ Tourism (JOI23_tourism) C++17
100 / 100
715 ms 34644 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 T[N * 4];

void upd(int pos, int val) {
    if(!pos) return;
    while(pos <= m) {
        T[pos] += val;
        pos += pos & -pos;
    }
}
int get1(int i) {
    int ret = 0;
    while(i) {
        ret += T[i];
        i -= i & -i;
    }
    return ret;
}
int get2(int l, int r) {
    return get1(r) - get1(l - 1);
}
set<array<int, 3>> st;
void add(int l, int r, int i) {
    auto del = [&](int l, int r, int i) -> void{
        if(i) upd(i, - (r - l + 1));
        st.erase({l, r, i});
    };
    auto ad = [&](int l, int r, int i) -> void{
        upd(i, (r - l + 1));
        st.insert({l, r, i});
    };
    // cout << l << ' ' << r << ' ' << i << '\n';
    while(true) {
        auto it = st.upper_bound({l, (int)1e9, (int)1e9});
        if(it == st.end() || (*it)[0] > r) break;
        if((*it)[1] > r) {
            ad(r + 1, (*it)[1], (*it)[2]);
            del((*it)[0], (*it)[1], (*it)[2]);  
            break;
        } 
        del((*it)[0], (*it)[1], (*it)[2]);  
    }
    auto it = st.upper_bound({l, (int)1e9, (int)1e9});
    if(it != st.begin()) {
        it--;
        if((*it)[1] >= l && (*it)[0] <= l) {
            if((*it)[0] < l) {
                ad((*it)[0], l - 1, (*it)[2]);
            }
            if((*it)[1] > r) {
                ad(r + 1, (*it)[1], (*it)[2]);
            }
            del((*it)[0], (*it)[1], (*it)[2]);  
        }
    }
    st.insert({l , r, i});
    upd(i, r - l + 1);
    // for(auto [_l, _r, k]:st) {
    //     cout << _l << ' ' << _r << ' ' << k << "x\n";
    // }
    // cout << '\n';
}
void nv(int v, int i) {
    do {
        add(tin[head[v]], tin[v], i);
        v = up[head[v]][0];
    }while(v > 1);
    add(1, 1, i);
}
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);
    if(m != 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});
    }
    st.insert({1, n, 0});
    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] = get2(l, i) - 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 3 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5176 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 3 ms 5184 KB Output is correct
12 Correct 3 ms 5288 KB Output is correct
13 Correct 4 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5176 KB Output is correct
17 Correct 2 ms 5208 KB Output is correct
18 Correct 3 ms 5112 KB Output is correct
19 Correct 3 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5180 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 3 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5176 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 3 ms 5184 KB Output is correct
12 Correct 3 ms 5288 KB Output is correct
13 Correct 4 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5176 KB Output is correct
17 Correct 2 ms 5208 KB Output is correct
18 Correct 3 ms 5112 KB Output is correct
19 Correct 3 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5180 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 6 ms 5468 KB Output is correct
31 Correct 6 ms 5468 KB Output is correct
32 Correct 8 ms 5468 KB Output is correct
33 Correct 7 ms 5644 KB Output is correct
34 Correct 7 ms 5532 KB Output is correct
35 Correct 7 ms 5668 KB Output is correct
36 Correct 7 ms 5604 KB Output is correct
37 Correct 7 ms 5720 KB Output is correct
38 Correct 5 ms 5724 KB Output is correct
39 Correct 5 ms 5724 KB Output is correct
40 Correct 5 ms 5720 KB Output is correct
41 Correct 4 ms 5724 KB Output is correct
42 Correct 4 ms 5528 KB Output is correct
43 Correct 5 ms 5532 KB Output is correct
44 Correct 7 ms 5648 KB Output is correct
45 Correct 7 ms 5516 KB Output is correct
46 Correct 5 ms 5468 KB Output is correct
47 Correct 5 ms 5468 KB Output is correct
48 Correct 5 ms 5524 KB Output is correct
49 Correct 5 ms 5568 KB Output is correct
50 Correct 5 ms 5612 KB Output is correct
51 Correct 6 ms 5724 KB Output is correct
52 Correct 5 ms 5724 KB Output is correct
53 Correct 5 ms 5724 KB Output is correct
54 Correct 5 ms 5720 KB Output is correct
55 Correct 7 ms 5724 KB Output is correct
56 Correct 4 ms 5212 KB Output is correct
57 Correct 3 ms 5468 KB Output is correct
58 Correct 7 ms 5444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 4 ms 5212 KB Output is correct
3 Correct 4 ms 5212 KB Output is correct
4 Correct 113 ms 23524 KB Output is correct
5 Correct 89 ms 27304 KB Output is correct
6 Correct 100 ms 30804 KB Output is correct
7 Correct 152 ms 34348 KB Output is correct
8 Correct 139 ms 34644 KB Output is correct
9 Correct 143 ms 34456 KB Output is correct
10 Correct 147 ms 34388 KB Output is correct
11 Correct 141 ms 34384 KB Output is correct
12 Correct 111 ms 33664 KB Output is correct
13 Correct 115 ms 33616 KB Output is correct
14 Correct 120 ms 33688 KB Output is correct
15 Correct 50 ms 27988 KB Output is correct
16 Correct 100 ms 32080 KB Output is correct
17 Correct 87 ms 11856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 230 ms 16848 KB Output is correct
3 Correct 341 ms 19536 KB Output is correct
4 Correct 275 ms 20564 KB Output is correct
5 Correct 437 ms 26960 KB Output is correct
6 Correct 468 ms 26708 KB Output is correct
7 Correct 441 ms 26708 KB Output is correct
8 Correct 441 ms 26948 KB Output is correct
9 Correct 426 ms 26708 KB Output is correct
10 Correct 429 ms 26804 KB Output is correct
11 Correct 431 ms 26960 KB Output is correct
12 Correct 438 ms 26964 KB Output is correct
13 Correct 429 ms 27220 KB Output is correct
14 Correct 449 ms 28236 KB Output is correct
15 Correct 443 ms 28500 KB Output is correct
16 Correct 436 ms 27324 KB Output is correct
17 Correct 440 ms 27312 KB Output is correct
18 Correct 422 ms 27216 KB Output is correct
19 Correct 275 ms 22868 KB Output is correct
20 Correct 269 ms 22948 KB Output is correct
21 Correct 302 ms 22984 KB Output is correct
22 Correct 290 ms 22864 KB Output is correct
23 Correct 287 ms 22792 KB Output is correct
24 Correct 274 ms 22864 KB Output is correct
25 Correct 276 ms 22864 KB Output is correct
26 Correct 288 ms 23028 KB Output is correct
27 Correct 276 ms 22864 KB Output is correct
28 Correct 283 ms 22864 KB Output is correct
29 Correct 295 ms 23156 KB Output is correct
30 Correct 318 ms 23124 KB Output is correct
31 Correct 294 ms 23660 KB Output is correct
32 Correct 290 ms 23856 KB Output is correct
33 Correct 312 ms 24940 KB Output is correct
34 Correct 339 ms 24660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 3 ms 5212 KB Output is correct
4 Correct 574 ms 22736 KB Output is correct
5 Correct 572 ms 23088 KB Output is correct
6 Correct 660 ms 28752 KB Output is correct
7 Correct 715 ms 31492 KB Output is correct
8 Correct 672 ms 31436 KB Output is correct
9 Correct 683 ms 31344 KB Output is correct
10 Correct 673 ms 31316 KB Output is correct
11 Correct 694 ms 31312 KB Output is correct
12 Correct 672 ms 31312 KB Output is correct
13 Correct 671 ms 31316 KB Output is correct
14 Correct 77 ms 11916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 3 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5176 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 3 ms 5184 KB Output is correct
12 Correct 3 ms 5288 KB Output is correct
13 Correct 4 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5176 KB Output is correct
17 Correct 2 ms 5208 KB Output is correct
18 Correct 3 ms 5112 KB Output is correct
19 Correct 3 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5180 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 6 ms 5468 KB Output is correct
31 Correct 6 ms 5468 KB Output is correct
32 Correct 8 ms 5468 KB Output is correct
33 Correct 7 ms 5644 KB Output is correct
34 Correct 7 ms 5532 KB Output is correct
35 Correct 7 ms 5668 KB Output is correct
36 Correct 7 ms 5604 KB Output is correct
37 Correct 7 ms 5720 KB Output is correct
38 Correct 5 ms 5724 KB Output is correct
39 Correct 5 ms 5724 KB Output is correct
40 Correct 5 ms 5720 KB Output is correct
41 Correct 4 ms 5724 KB Output is correct
42 Correct 4 ms 5528 KB Output is correct
43 Correct 5 ms 5532 KB Output is correct
44 Correct 7 ms 5648 KB Output is correct
45 Correct 7 ms 5516 KB Output is correct
46 Correct 5 ms 5468 KB Output is correct
47 Correct 5 ms 5468 KB Output is correct
48 Correct 5 ms 5524 KB Output is correct
49 Correct 5 ms 5568 KB Output is correct
50 Correct 5 ms 5612 KB Output is correct
51 Correct 6 ms 5724 KB Output is correct
52 Correct 5 ms 5724 KB Output is correct
53 Correct 5 ms 5724 KB Output is correct
54 Correct 5 ms 5720 KB Output is correct
55 Correct 7 ms 5724 KB Output is correct
56 Correct 4 ms 5212 KB Output is correct
57 Correct 3 ms 5468 KB Output is correct
58 Correct 7 ms 5444 KB Output is correct
59 Correct 2 ms 5212 KB Output is correct
60 Correct 4 ms 5212 KB Output is correct
61 Correct 4 ms 5212 KB Output is correct
62 Correct 113 ms 23524 KB Output is correct
63 Correct 89 ms 27304 KB Output is correct
64 Correct 100 ms 30804 KB Output is correct
65 Correct 152 ms 34348 KB Output is correct
66 Correct 139 ms 34644 KB Output is correct
67 Correct 143 ms 34456 KB Output is correct
68 Correct 147 ms 34388 KB Output is correct
69 Correct 141 ms 34384 KB Output is correct
70 Correct 111 ms 33664 KB Output is correct
71 Correct 115 ms 33616 KB Output is correct
72 Correct 120 ms 33688 KB Output is correct
73 Correct 50 ms 27988 KB Output is correct
74 Correct 100 ms 32080 KB Output is correct
75 Correct 87 ms 11856 KB Output is correct
76 Correct 2 ms 5212 KB Output is correct
77 Correct 230 ms 16848 KB Output is correct
78 Correct 341 ms 19536 KB Output is correct
79 Correct 275 ms 20564 KB Output is correct
80 Correct 437 ms 26960 KB Output is correct
81 Correct 468 ms 26708 KB Output is correct
82 Correct 441 ms 26708 KB Output is correct
83 Correct 441 ms 26948 KB Output is correct
84 Correct 426 ms 26708 KB Output is correct
85 Correct 429 ms 26804 KB Output is correct
86 Correct 431 ms 26960 KB Output is correct
87 Correct 438 ms 26964 KB Output is correct
88 Correct 429 ms 27220 KB Output is correct
89 Correct 449 ms 28236 KB Output is correct
90 Correct 443 ms 28500 KB Output is correct
91 Correct 436 ms 27324 KB Output is correct
92 Correct 440 ms 27312 KB Output is correct
93 Correct 422 ms 27216 KB Output is correct
94 Correct 275 ms 22868 KB Output is correct
95 Correct 269 ms 22948 KB Output is correct
96 Correct 302 ms 22984 KB Output is correct
97 Correct 290 ms 22864 KB Output is correct
98 Correct 287 ms 22792 KB Output is correct
99 Correct 274 ms 22864 KB Output is correct
100 Correct 276 ms 22864 KB Output is correct
101 Correct 288 ms 23028 KB Output is correct
102 Correct 276 ms 22864 KB Output is correct
103 Correct 283 ms 22864 KB Output is correct
104 Correct 295 ms 23156 KB Output is correct
105 Correct 318 ms 23124 KB Output is correct
106 Correct 294 ms 23660 KB Output is correct
107 Correct 290 ms 23856 KB Output is correct
108 Correct 312 ms 24940 KB Output is correct
109 Correct 339 ms 24660 KB Output is correct
110 Correct 2 ms 5212 KB Output is correct
111 Correct 2 ms 5212 KB Output is correct
112 Correct 3 ms 5212 KB Output is correct
113 Correct 574 ms 22736 KB Output is correct
114 Correct 572 ms 23088 KB Output is correct
115 Correct 660 ms 28752 KB Output is correct
116 Correct 715 ms 31492 KB Output is correct
117 Correct 672 ms 31436 KB Output is correct
118 Correct 683 ms 31344 KB Output is correct
119 Correct 673 ms 31316 KB Output is correct
120 Correct 694 ms 31312 KB Output is correct
121 Correct 672 ms 31312 KB Output is correct
122 Correct 671 ms 31316 KB Output is correct
123 Correct 77 ms 11916 KB Output is correct
124 Correct 420 ms 30508 KB Output is correct
125 Correct 323 ms 26924 KB Output is correct
126 Correct 512 ms 30936 KB Output is correct
127 Correct 514 ms 31024 KB Output is correct
128 Correct 517 ms 31060 KB Output is correct
129 Correct 491 ms 31060 KB Output is correct
130 Correct 504 ms 30920 KB Output is correct
131 Correct 164 ms 33364 KB Output is correct
132 Correct 167 ms 34388 KB Output is correct
133 Correct 182 ms 30548 KB Output is correct
134 Correct 328 ms 27216 KB Output is correct
135 Correct 343 ms 27172 KB Output is correct
136 Correct 356 ms 26964 KB Output is correct
137 Correct 252 ms 32460 KB Output is correct
138 Correct 242 ms 32456 KB Output is correct
139 Correct 245 ms 32460 KB Output is correct
140 Correct 258 ms 32464 KB Output is correct
141 Correct 274 ms 32512 KB Output is correct
142 Correct 252 ms 32456 KB Output is correct
143 Correct 54 ms 20236 KB Output is correct
144 Correct 462 ms 28456 KB Output is correct