답안 #799872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799872 2023-08-01T07:23:40 Z phoenix Tourism (JOI23_tourism) C++17
100 / 100
480 ms 24992 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 5036 KB Output is correct
2 Correct 2 ms 5032 KB Output is correct
3 Correct 3 ms 5040 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 3 ms 5160 KB Output is correct
11 Correct 3 ms 5132 KB Output is correct
12 Correct 3 ms 5160 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 4 ms 5172 KB Output is correct
17 Correct 3 ms 5076 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 5076 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 5044 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5036 KB Output is correct
2 Correct 2 ms 5032 KB Output is correct
3 Correct 3 ms 5040 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 3 ms 5160 KB Output is correct
11 Correct 3 ms 5132 KB Output is correct
12 Correct 3 ms 5160 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 4 ms 5172 KB Output is correct
17 Correct 3 ms 5076 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 5076 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 5044 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 5 ms 5276 KB Output is correct
31 Correct 6 ms 5312 KB Output is correct
32 Correct 7 ms 5380 KB Output is correct
33 Correct 6 ms 5332 KB Output is correct
34 Correct 7 ms 5332 KB Output is correct
35 Correct 6 ms 5332 KB Output is correct
36 Correct 6 ms 5308 KB Output is correct
37 Correct 6 ms 5376 KB Output is correct
38 Correct 6 ms 5432 KB Output is correct
39 Correct 5 ms 5428 KB Output is correct
40 Correct 5 ms 5460 KB Output is correct
41 Correct 6 ms 5436 KB Output is correct
42 Correct 5 ms 5460 KB Output is correct
43 Correct 5 ms 5452 KB Output is correct
44 Correct 6 ms 5332 KB Output is correct
45 Correct 6 ms 5332 KB Output is correct
46 Correct 6 ms 5332 KB Output is correct
47 Correct 6 ms 5332 KB Output is correct
48 Correct 7 ms 5332 KB Output is correct
49 Correct 6 ms 5332 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 5372 KB Output is correct
53 Correct 5 ms 5332 KB Output is correct
54 Correct 5 ms 5376 KB Output is correct
55 Correct 5 ms 5312 KB Output is correct
56 Correct 3 ms 5204 KB Output is correct
57 Correct 3 ms 5176 KB Output is correct
58 Correct 7 ms 5376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5032 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5180 KB Output is correct
4 Correct 154 ms 17448 KB Output is correct
5 Correct 118 ms 20056 KB Output is correct
6 Correct 150 ms 21836 KB Output is correct
7 Correct 195 ms 24904 KB Output is correct
8 Correct 202 ms 24936 KB Output is correct
9 Correct 191 ms 24908 KB Output is correct
10 Correct 195 ms 24992 KB Output is correct
11 Correct 195 ms 24912 KB Output is correct
12 Correct 109 ms 24460 KB Output is correct
13 Correct 109 ms 24520 KB Output is correct
14 Correct 108 ms 24500 KB Output is correct
15 Correct 40 ms 20300 KB Output is correct
16 Correct 206 ms 24744 KB Output is correct
17 Correct 67 ms 10316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 148 ms 10448 KB Output is correct
3 Correct 231 ms 11640 KB Output is correct
4 Correct 176 ms 11720 KB Output is correct
5 Correct 281 ms 14848 KB Output is correct
6 Correct 284 ms 14892 KB Output is correct
7 Correct 283 ms 14864 KB Output is correct
8 Correct 272 ms 14816 KB Output is correct
9 Correct 297 ms 14792 KB Output is correct
10 Correct 282 ms 14820 KB Output is correct
11 Correct 290 ms 14944 KB Output is correct
12 Correct 275 ms 14924 KB Output is correct
13 Correct 281 ms 15376 KB Output is correct
14 Correct 294 ms 16388 KB Output is correct
15 Correct 311 ms 19688 KB Output is correct
16 Correct 281 ms 15356 KB Output is correct
17 Correct 282 ms 15364 KB Output is correct
18 Correct 266 ms 15312 KB Output is correct
19 Correct 335 ms 14924 KB Output is correct
20 Correct 347 ms 14908 KB Output is correct
21 Correct 362 ms 14996 KB Output is correct
22 Correct 352 ms 14944 KB Output is correct
23 Correct 350 ms 14864 KB Output is correct
24 Correct 347 ms 14848 KB Output is correct
25 Correct 345 ms 14880 KB Output is correct
26 Correct 343 ms 14900 KB Output is correct
27 Correct 358 ms 14888 KB Output is correct
28 Correct 346 ms 14904 KB Output is correct
29 Correct 348 ms 14940 KB Output is correct
30 Correct 359 ms 15144 KB Output is correct
31 Correct 338 ms 15412 KB Output is correct
32 Correct 363 ms 15840 KB Output is correct
33 Correct 367 ms 17276 KB Output is correct
34 Correct 377 ms 19616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5032 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 388 ms 14996 KB Output is correct
5 Correct 398 ms 15004 KB Output is correct
6 Correct 435 ms 17488 KB Output is correct
7 Correct 442 ms 18764 KB Output is correct
8 Correct 443 ms 18748 KB Output is correct
9 Correct 454 ms 18736 KB Output is correct
10 Correct 446 ms 18736 KB Output is correct
11 Correct 480 ms 18744 KB Output is correct
12 Correct 473 ms 18804 KB Output is correct
13 Correct 451 ms 18764 KB Output is correct
14 Correct 57 ms 10344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5036 KB Output is correct
2 Correct 2 ms 5032 KB Output is correct
3 Correct 3 ms 5040 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 3 ms 5160 KB Output is correct
11 Correct 3 ms 5132 KB Output is correct
12 Correct 3 ms 5160 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 4 ms 5172 KB Output is correct
17 Correct 3 ms 5076 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 5076 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 5044 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 5 ms 5276 KB Output is correct
31 Correct 6 ms 5312 KB Output is correct
32 Correct 7 ms 5380 KB Output is correct
33 Correct 6 ms 5332 KB Output is correct
34 Correct 7 ms 5332 KB Output is correct
35 Correct 6 ms 5332 KB Output is correct
36 Correct 6 ms 5308 KB Output is correct
37 Correct 6 ms 5376 KB Output is correct
38 Correct 6 ms 5432 KB Output is correct
39 Correct 5 ms 5428 KB Output is correct
40 Correct 5 ms 5460 KB Output is correct
41 Correct 6 ms 5436 KB Output is correct
42 Correct 5 ms 5460 KB Output is correct
43 Correct 5 ms 5452 KB Output is correct
44 Correct 6 ms 5332 KB Output is correct
45 Correct 6 ms 5332 KB Output is correct
46 Correct 6 ms 5332 KB Output is correct
47 Correct 6 ms 5332 KB Output is correct
48 Correct 7 ms 5332 KB Output is correct
49 Correct 6 ms 5332 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 5372 KB Output is correct
53 Correct 5 ms 5332 KB Output is correct
54 Correct 5 ms 5376 KB Output is correct
55 Correct 5 ms 5312 KB Output is correct
56 Correct 3 ms 5204 KB Output is correct
57 Correct 3 ms 5176 KB Output is correct
58 Correct 7 ms 5376 KB Output is correct
59 Correct 2 ms 5032 KB Output is correct
60 Correct 2 ms 5076 KB Output is correct
61 Correct 3 ms 5180 KB Output is correct
62 Correct 154 ms 17448 KB Output is correct
63 Correct 118 ms 20056 KB Output is correct
64 Correct 150 ms 21836 KB Output is correct
65 Correct 195 ms 24904 KB Output is correct
66 Correct 202 ms 24936 KB Output is correct
67 Correct 191 ms 24908 KB Output is correct
68 Correct 195 ms 24992 KB Output is correct
69 Correct 195 ms 24912 KB Output is correct
70 Correct 109 ms 24460 KB Output is correct
71 Correct 109 ms 24520 KB Output is correct
72 Correct 108 ms 24500 KB Output is correct
73 Correct 40 ms 20300 KB Output is correct
74 Correct 206 ms 24744 KB Output is correct
75 Correct 67 ms 10316 KB Output is correct
76 Correct 2 ms 5076 KB Output is correct
77 Correct 148 ms 10448 KB Output is correct
78 Correct 231 ms 11640 KB Output is correct
79 Correct 176 ms 11720 KB Output is correct
80 Correct 281 ms 14848 KB Output is correct
81 Correct 284 ms 14892 KB Output is correct
82 Correct 283 ms 14864 KB Output is correct
83 Correct 272 ms 14816 KB Output is correct
84 Correct 297 ms 14792 KB Output is correct
85 Correct 282 ms 14820 KB Output is correct
86 Correct 290 ms 14944 KB Output is correct
87 Correct 275 ms 14924 KB Output is correct
88 Correct 281 ms 15376 KB Output is correct
89 Correct 294 ms 16388 KB Output is correct
90 Correct 311 ms 19688 KB Output is correct
91 Correct 281 ms 15356 KB Output is correct
92 Correct 282 ms 15364 KB Output is correct
93 Correct 266 ms 15312 KB Output is correct
94 Correct 335 ms 14924 KB Output is correct
95 Correct 347 ms 14908 KB Output is correct
96 Correct 362 ms 14996 KB Output is correct
97 Correct 352 ms 14944 KB Output is correct
98 Correct 350 ms 14864 KB Output is correct
99 Correct 347 ms 14848 KB Output is correct
100 Correct 345 ms 14880 KB Output is correct
101 Correct 343 ms 14900 KB Output is correct
102 Correct 358 ms 14888 KB Output is correct
103 Correct 346 ms 14904 KB Output is correct
104 Correct 348 ms 14940 KB Output is correct
105 Correct 359 ms 15144 KB Output is correct
106 Correct 338 ms 15412 KB Output is correct
107 Correct 363 ms 15840 KB Output is correct
108 Correct 367 ms 17276 KB Output is correct
109 Correct 377 ms 19616 KB Output is correct
110 Correct 2 ms 5032 KB Output is correct
111 Correct 2 ms 5076 KB Output is correct
112 Correct 3 ms 5204 KB Output is correct
113 Correct 388 ms 14996 KB Output is correct
114 Correct 398 ms 15004 KB Output is correct
115 Correct 435 ms 17488 KB Output is correct
116 Correct 442 ms 18764 KB Output is correct
117 Correct 443 ms 18748 KB Output is correct
118 Correct 454 ms 18736 KB Output is correct
119 Correct 446 ms 18736 KB Output is correct
120 Correct 480 ms 18744 KB Output is correct
121 Correct 473 ms 18804 KB Output is correct
122 Correct 451 ms 18764 KB Output is correct
123 Correct 57 ms 10344 KB Output is correct
124 Correct 303 ms 18808 KB Output is correct
125 Correct 239 ms 16444 KB Output is correct
126 Correct 352 ms 19156 KB Output is correct
127 Correct 369 ms 19180 KB Output is correct
128 Correct 356 ms 19120 KB Output is correct
129 Correct 370 ms 19168 KB Output is correct
130 Correct 339 ms 19140 KB Output is correct
131 Correct 221 ms 24112 KB Output is correct
132 Correct 233 ms 24940 KB Output is correct
133 Correct 245 ms 21832 KB Output is correct
134 Correct 441 ms 19036 KB Output is correct
135 Correct 434 ms 19016 KB Output is correct
136 Correct 437 ms 19088 KB Output is correct
137 Correct 173 ms 18672 KB Output is correct
138 Correct 178 ms 18648 KB Output is correct
139 Correct 152 ms 18664 KB Output is correct
140 Correct 150 ms 19172 KB Output is correct
141 Correct 140 ms 19296 KB Output is correct
142 Correct 158 ms 19208 KB Output is correct
143 Correct 43 ms 14388 KB Output is correct
144 Correct 314 ms 18752 KB Output is correct