답안 #796654

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796654 2023-07-28T15:22:37 Z vjudge1 Tourism (JOI23_tourism) C++17
100 / 100
499 ms 24996 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 100000 + 10;
const int M = (1 << 17);

int n, m, q;

int T;
int sz[N];
int tin[N];
int tout[N];
int depth[N];
int par[N];
int head[N];
int heavy[N];
vector<int> g[N];
int sum_cnt[2 * M];
bool have[2 * M];
int color[2 * M];

void upd(int pos, int d) {
    for (sum_cnt[pos += M] += d; pos > 1; pos >>= 1)
        sum_cnt[pos >> 1] = sum_cnt[pos] + sum_cnt[pos ^ 1];
}
int get(int ql, int qr) {
    int res = 0;
    for (ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) {
        if (ql & 1)
            res += sum_cnt[ql++];
        if (qr & 1)
            res += sum_cnt[--qr];
    }
    return res;
}
void precalc(int s, int p = 0) {
    sz[s] = 1;
    par[s] = p;
    depth[s] = depth[p] + (p > 0);
    for (int &to : g[s]) {
        if (to == p)
            continue;
        precalc(to, s);
        sz[s] += sz[to];
        if (sz[heavy[s]] < sz[to])
            heavy[s] = to;
    }
}

void decompose(int s, int h) {
    tin[s] = T++;
    head[s] = h;
    if(heavy[s]) decompose(heavy[s], h);
    for(int to : g[s]) {
        if(to == par[s] || to == heavy[s]) continue;
        decompose(to, to);
    }
    tout[s] = T;
}
void recalc(int v) {
    have[v] = (color[v] > 0);
    if(2 * v < 2 * M && have[2 * v]) have[v] = true;
    if(2 * v + 1 < 2 * M && have[2 * v + 1]) have[v] = true;
}
void push(int v) {
    if(!color[v] || v >= M) return;
    color[2 * v] = color[v];
    recalc(2 * v);
    color[2 * v + 1] = color[v];
    recalc(2 * v + 1);
    color[v] = 0;
}   
void set_color(int v, int d, int cl) {
    if(color[v])
        upd(color[v], -d);
    color[v] = cl;
    if(color[v])
        upd(color[v], d);
    recalc(v);
} 
void clear(int l, int r, int v) {
    set_color(v, r - l + 1, 0);
    if(!have[v]) return;
    int mid = (l + r) / 2;
    clear(l, mid, 2 * v);
    clear(mid + 1, r, 2 * v + 1);
    have[v] = false;
}
void update(int ql, int qr, int cl, int l, int r, int v, bool type = 0) {
    if(qr < l || ql > r) return;
    if(ql <= l && r <= qr) {
        clear(l, r, v);
        set_color(v, r - l + 1, cl); 
        return;
    }
    push(v);
    int mid = (l + r) / 2;
    update(ql, qr, cl, l, mid, 2 * v, 0);
    update(ql, qr, cl, mid + 1, r, 2 * v + 1, 0);
    recalc(v);
}
void update(int u, int v, int cl) {
    for(; head[u] != head[v]; v = par[head[v]]) {
        if(depth[head[u]] > depth[head[v]]) swap(u, v);
        update(tin[head[v]], tin[v], cl, 0, M - 1, 1);
    }
    if(depth[u] > depth[v]) swap(u, v);
    update(tin[u], tin[v], cl, 0, M - 1, 1);
}

struct query {
    int l, r, i;
};

vector<query> qr[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> q;
    for(int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int c[m + 1];
    for(int i = 1; i <= m; i++) 
        cin >> c[i];
    
    precalc(1);
    decompose(1, 1);
    // for(int i = 1; i <= n; i++) {
    //     cout << '{' << head[i] << ' ' << tin[i] << '}' << ' ';
    // }
    // cout << '\n';
    for(int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        qr[r].push_back({l, r, i});
    }
    int res[q] = {};
    for(int i = 1; i <= m; i++) {
        if(i > 1) 
            update(c[i], c[i - 1], i - 1);
        for(auto t : qr[i]) {
            if(t.l == t.r) res[t.i] = 1;
            else res[t.i] = get(t.l, t.r);
        }
        // for(int x = 1; x <= m; x++) 
        //     cout << get(x, x) << ' ';
        // cout << '\n';
    }
    for(int i = 0; i < q; i++) 
        cout << res[i] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5168 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 3 ms 5164 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5096 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5168 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 3 ms 5168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5168 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 3 ms 5164 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5096 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5168 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 3 ms 5168 KB Output is correct
30 Correct 8 ms 5332 KB Output is correct
31 Correct 6 ms 5332 KB Output is correct
32 Correct 7 ms 5332 KB Output is correct
33 Correct 6 ms 5412 KB Output is correct
34 Correct 7 ms 5332 KB Output is correct
35 Correct 6 ms 5332 KB Output is correct
36 Correct 7 ms 5332 KB Output is correct
37 Correct 6 ms 5332 KB Output is correct
38 Correct 5 ms 5460 KB Output is correct
39 Correct 5 ms 5436 KB Output is correct
40 Correct 5 ms 5460 KB Output is correct
41 Correct 5 ms 5492 KB Output is correct
42 Correct 5 ms 5460 KB Output is correct
43 Correct 6 ms 5480 KB Output is correct
44 Correct 8 ms 5332 KB Output is correct
45 Correct 8 ms 5372 KB Output is correct
46 Correct 6 ms 5332 KB Output is correct
47 Correct 6 ms 5424 KB Output is correct
48 Correct 7 ms 5412 KB Output is correct
49 Correct 7 ms 5480 KB Output is correct
50 Correct 5 ms 5332 KB Output is correct
51 Correct 5 ms 5332 KB Output is correct
52 Correct 5 ms 5332 KB Output is correct
53 Correct 6 ms 5332 KB Output is correct
54 Correct 5 ms 5380 KB Output is correct
55 Correct 5 ms 5332 KB Output is correct
56 Correct 4 ms 5120 KB Output is correct
57 Correct 3 ms 5172 KB Output is correct
58 Correct 6 ms 5332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 4 ms 5176 KB Output is correct
4 Correct 158 ms 17444 KB Output is correct
5 Correct 128 ms 20044 KB Output is correct
6 Correct 152 ms 21848 KB Output is correct
7 Correct 218 ms 24964 KB Output is correct
8 Correct 216 ms 24888 KB Output is correct
9 Correct 198 ms 24996 KB Output is correct
10 Correct 201 ms 24992 KB Output is correct
11 Correct 206 ms 24960 KB Output is correct
12 Correct 113 ms 24544 KB Output is correct
13 Correct 110 ms 24452 KB Output is correct
14 Correct 110 ms 24524 KB Output is correct
15 Correct 41 ms 20364 KB Output is correct
16 Correct 179 ms 24656 KB Output is correct
17 Correct 63 ms 10340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 147 ms 9932 KB Output is correct
3 Correct 232 ms 11500 KB Output is correct
4 Correct 191 ms 11772 KB Output is correct
5 Correct 288 ms 15004 KB Output is correct
6 Correct 288 ms 14948 KB Output is correct
7 Correct 310 ms 14888 KB Output is correct
8 Correct 270 ms 14816 KB Output is correct
9 Correct 276 ms 14864 KB Output is correct
10 Correct 281 ms 14880 KB Output is correct
11 Correct 285 ms 14952 KB Output is correct
12 Correct 320 ms 14964 KB Output is correct
13 Correct 286 ms 15276 KB Output is correct
14 Correct 304 ms 16248 KB Output is correct
15 Correct 341 ms 19660 KB Output is correct
16 Correct 286 ms 15388 KB Output is correct
17 Correct 280 ms 15288 KB Output is correct
18 Correct 277 ms 15268 KB Output is correct
19 Correct 345 ms 14904 KB Output is correct
20 Correct 343 ms 14892 KB Output is correct
21 Correct 365 ms 14824 KB Output is correct
22 Correct 344 ms 14928 KB Output is correct
23 Correct 340 ms 14828 KB Output is correct
24 Correct 348 ms 14920 KB Output is correct
25 Correct 347 ms 14944 KB Output is correct
26 Correct 348 ms 14988 KB Output is correct
27 Correct 352 ms 14880 KB Output is correct
28 Correct 366 ms 14920 KB Output is correct
29 Correct 347 ms 14924 KB Output is correct
30 Correct 409 ms 15180 KB Output is correct
31 Correct 338 ms 15384 KB Output is correct
32 Correct 352 ms 15816 KB Output is correct
33 Correct 364 ms 17328 KB Output is correct
34 Correct 387 ms 19628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 394 ms 13836 KB Output is correct
5 Correct 385 ms 14100 KB Output is correct
6 Correct 469 ms 16108 KB Output is correct
7 Correct 450 ms 17228 KB Output is correct
8 Correct 446 ms 17212 KB Output is correct
9 Correct 454 ms 17252 KB Output is correct
10 Correct 499 ms 17232 KB Output is correct
11 Correct 437 ms 17140 KB Output is correct
12 Correct 457 ms 17228 KB Output is correct
13 Correct 447 ms 17224 KB Output is correct
14 Correct 57 ms 10312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5168 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 3 ms 5164 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5096 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5168 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 3 ms 5168 KB Output is correct
30 Correct 8 ms 5332 KB Output is correct
31 Correct 6 ms 5332 KB Output is correct
32 Correct 7 ms 5332 KB Output is correct
33 Correct 6 ms 5412 KB Output is correct
34 Correct 7 ms 5332 KB Output is correct
35 Correct 6 ms 5332 KB Output is correct
36 Correct 7 ms 5332 KB Output is correct
37 Correct 6 ms 5332 KB Output is correct
38 Correct 5 ms 5460 KB Output is correct
39 Correct 5 ms 5436 KB Output is correct
40 Correct 5 ms 5460 KB Output is correct
41 Correct 5 ms 5492 KB Output is correct
42 Correct 5 ms 5460 KB Output is correct
43 Correct 6 ms 5480 KB Output is correct
44 Correct 8 ms 5332 KB Output is correct
45 Correct 8 ms 5372 KB Output is correct
46 Correct 6 ms 5332 KB Output is correct
47 Correct 6 ms 5424 KB Output is correct
48 Correct 7 ms 5412 KB Output is correct
49 Correct 7 ms 5480 KB Output is correct
50 Correct 5 ms 5332 KB Output is correct
51 Correct 5 ms 5332 KB Output is correct
52 Correct 5 ms 5332 KB Output is correct
53 Correct 6 ms 5332 KB Output is correct
54 Correct 5 ms 5380 KB Output is correct
55 Correct 5 ms 5332 KB Output is correct
56 Correct 4 ms 5120 KB Output is correct
57 Correct 3 ms 5172 KB Output is correct
58 Correct 6 ms 5332 KB Output is correct
59 Correct 2 ms 5076 KB Output is correct
60 Correct 2 ms 5076 KB Output is correct
61 Correct 4 ms 5176 KB Output is correct
62 Correct 158 ms 17444 KB Output is correct
63 Correct 128 ms 20044 KB Output is correct
64 Correct 152 ms 21848 KB Output is correct
65 Correct 218 ms 24964 KB Output is correct
66 Correct 216 ms 24888 KB Output is correct
67 Correct 198 ms 24996 KB Output is correct
68 Correct 201 ms 24992 KB Output is correct
69 Correct 206 ms 24960 KB Output is correct
70 Correct 113 ms 24544 KB Output is correct
71 Correct 110 ms 24452 KB Output is correct
72 Correct 110 ms 24524 KB Output is correct
73 Correct 41 ms 20364 KB Output is correct
74 Correct 179 ms 24656 KB Output is correct
75 Correct 63 ms 10340 KB Output is correct
76 Correct 3 ms 5076 KB Output is correct
77 Correct 147 ms 9932 KB Output is correct
78 Correct 232 ms 11500 KB Output is correct
79 Correct 191 ms 11772 KB Output is correct
80 Correct 288 ms 15004 KB Output is correct
81 Correct 288 ms 14948 KB Output is correct
82 Correct 310 ms 14888 KB Output is correct
83 Correct 270 ms 14816 KB Output is correct
84 Correct 276 ms 14864 KB Output is correct
85 Correct 281 ms 14880 KB Output is correct
86 Correct 285 ms 14952 KB Output is correct
87 Correct 320 ms 14964 KB Output is correct
88 Correct 286 ms 15276 KB Output is correct
89 Correct 304 ms 16248 KB Output is correct
90 Correct 341 ms 19660 KB Output is correct
91 Correct 286 ms 15388 KB Output is correct
92 Correct 280 ms 15288 KB Output is correct
93 Correct 277 ms 15268 KB Output is correct
94 Correct 345 ms 14904 KB Output is correct
95 Correct 343 ms 14892 KB Output is correct
96 Correct 365 ms 14824 KB Output is correct
97 Correct 344 ms 14928 KB Output is correct
98 Correct 340 ms 14828 KB Output is correct
99 Correct 348 ms 14920 KB Output is correct
100 Correct 347 ms 14944 KB Output is correct
101 Correct 348 ms 14988 KB Output is correct
102 Correct 352 ms 14880 KB Output is correct
103 Correct 366 ms 14920 KB Output is correct
104 Correct 347 ms 14924 KB Output is correct
105 Correct 409 ms 15180 KB Output is correct
106 Correct 338 ms 15384 KB Output is correct
107 Correct 352 ms 15816 KB Output is correct
108 Correct 364 ms 17328 KB Output is correct
109 Correct 387 ms 19628 KB Output is correct
110 Correct 2 ms 5076 KB Output is correct
111 Correct 3 ms 5036 KB Output is correct
112 Correct 3 ms 5204 KB Output is correct
113 Correct 394 ms 13836 KB Output is correct
114 Correct 385 ms 14100 KB Output is correct
115 Correct 469 ms 16108 KB Output is correct
116 Correct 450 ms 17228 KB Output is correct
117 Correct 446 ms 17212 KB Output is correct
118 Correct 454 ms 17252 KB Output is correct
119 Correct 499 ms 17232 KB Output is correct
120 Correct 437 ms 17140 KB Output is correct
121 Correct 457 ms 17228 KB Output is correct
122 Correct 447 ms 17224 KB Output is correct
123 Correct 57 ms 10312 KB Output is correct
124 Correct 293 ms 18664 KB Output is correct
125 Correct 218 ms 16436 KB Output is correct
126 Correct 327 ms 19132 KB Output is correct
127 Correct 332 ms 19184 KB Output is correct
128 Correct 325 ms 19192 KB Output is correct
129 Correct 323 ms 19100 KB Output is correct
130 Correct 322 ms 19136 KB Output is correct
131 Correct 203 ms 23936 KB Output is correct
132 Correct 229 ms 24864 KB Output is correct
133 Correct 232 ms 21812 KB Output is correct
134 Correct 385 ms 19116 KB Output is correct
135 Correct 403 ms 19120 KB Output is correct
136 Correct 398 ms 19136 KB Output is correct
137 Correct 139 ms 18624 KB Output is correct
138 Correct 138 ms 18624 KB Output is correct
139 Correct 142 ms 18664 KB Output is correct
140 Correct 176 ms 19240 KB Output is correct
141 Correct 157 ms 19296 KB Output is correct
142 Correct 139 ms 19188 KB Output is correct
143 Correct 50 ms 14424 KB Output is correct
144 Correct 330 ms 18764 KB Output is correct