답안 #1111030

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111030 2024-11-11T10:36:17 Z Neco_arc Tourism (JOI23_tourism) C++17
100 / 100
312 ms 23112 KB
#include <bits/stdc++.h>

#define ll long long
#define name "Tourism"
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define all(x) x.begin(), x.end()
#define fi(i, a, b)  for(int i = a; i <= b; ++i)
#define fid(i, a, b) for(int i = a; i <= b; --i)
#define maxn (int) (1e5 + 7)

using namespace std;

int n, m, q, Time;
int a[maxn], ans[maxn];
int In[maxn], nex[maxn], siz[maxn], h[maxn], par[maxn];
vector<int> edges[maxn];
struct dl { int l, r, id; } qr[maxn];

struct BIT {
    int bit[maxn];

    void update(int x, int val) {
        for(; x > 0; x -= (x & -x)) bit[x] += val;
    }

    int get(int x, int ans = 0) {
        for(; x < maxn; x += (x & -x)) ans += bit[x];
        return ans;
    }

} Bit;

struct IT {
    int st[4 * maxn];

    int cb(int x, int y) { return x == y ? x : -1; }
    void down(int id) {
        if(st[id] != -1) st[id * 2] = st[id * 2 + 1] = st[id];
    }

    void update(int u, int v, int val, int id = 1, int l = 1, int r = n) {
        if(l > v || r < u) return;
        if(u <= l && r <= v && st[id] != -1) {
            Bit.update(st[id], -(r - l + 1));
            Bit.update(val, r - l + 1), st[id] = val;

            return;
        }

        down(id);
        int mid = (l + r) >> 1;
        update(u, v, val, _left), update(u, v, val, _right);

        st[id] = cb(st[id * 2], st[id * 2 + 1]);
    }

} St;

int dfs_siz(int u) {
    siz[u] = 1;
    for(int &v : edges[u]) {
        edges[v].erase(find(all(edges[v]), u));
        siz[u] += dfs_siz(v);

        if(siz[v] > siz[edges[u][0]]) swap(v, edges[u][0]);
    }
    return siz[u];
}

void dfs_hld(int u) {
    In[u] = ++Time;
    for(int v : edges[u]) {
        par[v] = u, h[v] = h[u] + 1;
        nex[v] = v == edges[u][0] ? nex[u] : v;

        dfs_hld(v);
    }
}

void update(int u, int v, int val) {
    for(; nex[u] != nex[v]; u = par[nex[u]]) {
        if(h[nex[u]] < h[nex[v]]) swap(u, v);
        St.update(In[nex[u]], In[u], val);
    }
    if(h[u] < h[v]) swap(u, v);
    St.update(In[v], In[u], val);
}

int Query(int x) {
    int ans = Bit.get(x);
    return ans == 0 ? 1 : ans;
}

void solve() {

    cin >> n >> m >> q;
    fi(i, 1, n - 1) {
        int x, y; cin >> x >> y;
        edges[x].push_back(y), edges[y].push_back(x);
    }
    fi(i, 1, m) cin >> a[i];
    fi(i, 1, q) {
        int l, r, id = i; cin >> l >> r;
        qr[i] = {l, r, id};
    }

    nex[1] = 1;
    dfs_siz(1), dfs_hld(1);

    int j = 1;
    sort(qr + 1, qr + 1 + q, [](dl x, dl y) { return x.r < y.r; });

    fi(i, 1, q) {
        while(j < qr[i].r) {
            ++j;
            if(j > 1) update(a[j - 1], a[j], j - 1);
        }
        ans[qr[i].id] = Query(qr[i].l);
    }

    fi(i, 1, q) cout << ans[i] << '\n';


}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    if(fopen(name".inp", "r")) {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }

    solve();

}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 2 ms 6480 KB Output is correct
3 Correct 2 ms 6480 KB Output is correct
4 Correct 2 ms 6480 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 6652 KB Output is correct
7 Correct 2 ms 6480 KB Output is correct
8 Correct 3 ms 6492 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 2 ms 6480 KB Output is correct
11 Correct 2 ms 6648 KB Output is correct
12 Correct 2 ms 6480 KB Output is correct
13 Correct 2 ms 6648 KB Output is correct
14 Correct 2 ms 6480 KB Output is correct
15 Correct 2 ms 6480 KB Output is correct
16 Correct 2 ms 6632 KB Output is correct
17 Correct 2 ms 6480 KB Output is correct
18 Correct 2 ms 6480 KB Output is correct
19 Correct 2 ms 6480 KB Output is correct
20 Correct 2 ms 6480 KB Output is correct
21 Correct 2 ms 6480 KB Output is correct
22 Correct 2 ms 6480 KB Output is correct
23 Correct 2 ms 6480 KB Output is correct
24 Correct 2 ms 6484 KB Output is correct
25 Correct 2 ms 6480 KB Output is correct
26 Correct 2 ms 6480 KB Output is correct
27 Correct 2 ms 6480 KB Output is correct
28 Correct 1 ms 6480 KB Output is correct
29 Correct 2 ms 6652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 2 ms 6480 KB Output is correct
3 Correct 2 ms 6480 KB Output is correct
4 Correct 2 ms 6480 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 6652 KB Output is correct
7 Correct 2 ms 6480 KB Output is correct
8 Correct 3 ms 6492 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 2 ms 6480 KB Output is correct
11 Correct 2 ms 6648 KB Output is correct
12 Correct 2 ms 6480 KB Output is correct
13 Correct 2 ms 6648 KB Output is correct
14 Correct 2 ms 6480 KB Output is correct
15 Correct 2 ms 6480 KB Output is correct
16 Correct 2 ms 6632 KB Output is correct
17 Correct 2 ms 6480 KB Output is correct
18 Correct 2 ms 6480 KB Output is correct
19 Correct 2 ms 6480 KB Output is correct
20 Correct 2 ms 6480 KB Output is correct
21 Correct 2 ms 6480 KB Output is correct
22 Correct 2 ms 6480 KB Output is correct
23 Correct 2 ms 6480 KB Output is correct
24 Correct 2 ms 6484 KB Output is correct
25 Correct 2 ms 6480 KB Output is correct
26 Correct 2 ms 6480 KB Output is correct
27 Correct 2 ms 6480 KB Output is correct
28 Correct 1 ms 6480 KB Output is correct
29 Correct 2 ms 6652 KB Output is correct
30 Correct 3 ms 6736 KB Output is correct
31 Correct 3 ms 6712 KB Output is correct
32 Correct 5 ms 6736 KB Output is correct
33 Correct 4 ms 6736 KB Output is correct
34 Correct 4 ms 6736 KB Output is correct
35 Correct 5 ms 6632 KB Output is correct
36 Correct 4 ms 6736 KB Output is correct
37 Correct 4 ms 6628 KB Output is correct
38 Correct 4 ms 6660 KB Output is correct
39 Correct 3 ms 6904 KB Output is correct
40 Correct 4 ms 6736 KB Output is correct
41 Correct 4 ms 6736 KB Output is correct
42 Correct 4 ms 6736 KB Output is correct
43 Correct 4 ms 6736 KB Output is correct
44 Correct 4 ms 6736 KB Output is correct
45 Correct 5 ms 6840 KB Output is correct
46 Correct 4 ms 6840 KB Output is correct
47 Correct 4 ms 6736 KB Output is correct
48 Correct 4 ms 6904 KB Output is correct
49 Correct 4 ms 6736 KB Output is correct
50 Correct 3 ms 6736 KB Output is correct
51 Correct 3 ms 6736 KB Output is correct
52 Correct 3 ms 6736 KB Output is correct
53 Correct 3 ms 6736 KB Output is correct
54 Correct 3 ms 6736 KB Output is correct
55 Correct 3 ms 6736 KB Output is correct
56 Correct 3 ms 6480 KB Output is correct
57 Correct 2 ms 6736 KB Output is correct
58 Correct 5 ms 6908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 1 ms 6480 KB Output is correct
3 Correct 2 ms 6480 KB Output is correct
4 Correct 124 ms 16456 KB Output is correct
5 Correct 96 ms 19272 KB Output is correct
6 Correct 123 ms 20780 KB Output is correct
7 Correct 168 ms 22884 KB Output is correct
8 Correct 157 ms 23112 KB Output is correct
9 Correct 162 ms 22860 KB Output is correct
10 Correct 161 ms 23112 KB Output is correct
11 Correct 163 ms 22964 KB Output is correct
12 Correct 100 ms 22868 KB Output is correct
13 Correct 101 ms 22856 KB Output is correct
14 Correct 99 ms 22856 KB Output is correct
15 Correct 50 ms 20792 KB Output is correct
16 Correct 158 ms 22500 KB Output is correct
17 Correct 31 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 104 ms 10320 KB Output is correct
3 Correct 167 ms 10912 KB Output is correct
4 Correct 133 ms 11080 KB Output is correct
5 Correct 205 ms 13136 KB Output is correct
6 Correct 205 ms 13136 KB Output is correct
7 Correct 214 ms 13136 KB Output is correct
8 Correct 211 ms 13072 KB Output is correct
9 Correct 216 ms 13256 KB Output is correct
10 Correct 218 ms 13136 KB Output is correct
11 Correct 215 ms 13128 KB Output is correct
12 Correct 213 ms 13128 KB Output is correct
13 Correct 197 ms 13384 KB Output is correct
14 Correct 198 ms 13564 KB Output is correct
15 Correct 209 ms 14940 KB Output is correct
16 Correct 193 ms 13288 KB Output is correct
17 Correct 199 ms 13248 KB Output is correct
18 Correct 190 ms 13392 KB Output is correct
19 Correct 253 ms 13308 KB Output is correct
20 Correct 267 ms 13136 KB Output is correct
21 Correct 271 ms 13136 KB Output is correct
22 Correct 274 ms 13136 KB Output is correct
23 Correct 275 ms 13136 KB Output is correct
24 Correct 276 ms 13276 KB Output is correct
25 Correct 285 ms 13128 KB Output is correct
26 Correct 297 ms 13128 KB Output is correct
27 Correct 297 ms 13140 KB Output is correct
28 Correct 312 ms 13128 KB Output is correct
29 Correct 296 ms 13128 KB Output is correct
30 Correct 311 ms 13640 KB Output is correct
31 Correct 276 ms 13392 KB Output is correct
32 Correct 274 ms 13596 KB Output is correct
33 Correct 284 ms 13968 KB Output is correct
34 Correct 281 ms 15096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6480 KB Output is correct
2 Correct 1 ms 6480 KB Output is correct
3 Correct 2 ms 6480 KB Output is correct
4 Correct 243 ms 12612 KB Output is correct
5 Correct 237 ms 12636 KB Output is correct
6 Correct 286 ms 14296 KB Output is correct
7 Correct 299 ms 15180 KB Output is correct
8 Correct 301 ms 15176 KB Output is correct
9 Correct 292 ms 15220 KB Output is correct
10 Correct 299 ms 15176 KB Output is correct
11 Correct 294 ms 15176 KB Output is correct
12 Correct 295 ms 15176 KB Output is correct
13 Correct 297 ms 15176 KB Output is correct
14 Correct 36 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 2 ms 6480 KB Output is correct
3 Correct 2 ms 6480 KB Output is correct
4 Correct 2 ms 6480 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 6652 KB Output is correct
7 Correct 2 ms 6480 KB Output is correct
8 Correct 3 ms 6492 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 2 ms 6480 KB Output is correct
11 Correct 2 ms 6648 KB Output is correct
12 Correct 2 ms 6480 KB Output is correct
13 Correct 2 ms 6648 KB Output is correct
14 Correct 2 ms 6480 KB Output is correct
15 Correct 2 ms 6480 KB Output is correct
16 Correct 2 ms 6632 KB Output is correct
17 Correct 2 ms 6480 KB Output is correct
18 Correct 2 ms 6480 KB Output is correct
19 Correct 2 ms 6480 KB Output is correct
20 Correct 2 ms 6480 KB Output is correct
21 Correct 2 ms 6480 KB Output is correct
22 Correct 2 ms 6480 KB Output is correct
23 Correct 2 ms 6480 KB Output is correct
24 Correct 2 ms 6484 KB Output is correct
25 Correct 2 ms 6480 KB Output is correct
26 Correct 2 ms 6480 KB Output is correct
27 Correct 2 ms 6480 KB Output is correct
28 Correct 1 ms 6480 KB Output is correct
29 Correct 2 ms 6652 KB Output is correct
30 Correct 3 ms 6736 KB Output is correct
31 Correct 3 ms 6712 KB Output is correct
32 Correct 5 ms 6736 KB Output is correct
33 Correct 4 ms 6736 KB Output is correct
34 Correct 4 ms 6736 KB Output is correct
35 Correct 5 ms 6632 KB Output is correct
36 Correct 4 ms 6736 KB Output is correct
37 Correct 4 ms 6628 KB Output is correct
38 Correct 4 ms 6660 KB Output is correct
39 Correct 3 ms 6904 KB Output is correct
40 Correct 4 ms 6736 KB Output is correct
41 Correct 4 ms 6736 KB Output is correct
42 Correct 4 ms 6736 KB Output is correct
43 Correct 4 ms 6736 KB Output is correct
44 Correct 4 ms 6736 KB Output is correct
45 Correct 5 ms 6840 KB Output is correct
46 Correct 4 ms 6840 KB Output is correct
47 Correct 4 ms 6736 KB Output is correct
48 Correct 4 ms 6904 KB Output is correct
49 Correct 4 ms 6736 KB Output is correct
50 Correct 3 ms 6736 KB Output is correct
51 Correct 3 ms 6736 KB Output is correct
52 Correct 3 ms 6736 KB Output is correct
53 Correct 3 ms 6736 KB Output is correct
54 Correct 3 ms 6736 KB Output is correct
55 Correct 3 ms 6736 KB Output is correct
56 Correct 3 ms 6480 KB Output is correct
57 Correct 2 ms 6736 KB Output is correct
58 Correct 5 ms 6908 KB Output is correct
59 Correct 2 ms 6480 KB Output is correct
60 Correct 1 ms 6480 KB Output is correct
61 Correct 2 ms 6480 KB Output is correct
62 Correct 124 ms 16456 KB Output is correct
63 Correct 96 ms 19272 KB Output is correct
64 Correct 123 ms 20780 KB Output is correct
65 Correct 168 ms 22884 KB Output is correct
66 Correct 157 ms 23112 KB Output is correct
67 Correct 162 ms 22860 KB Output is correct
68 Correct 161 ms 23112 KB Output is correct
69 Correct 163 ms 22964 KB Output is correct
70 Correct 100 ms 22868 KB Output is correct
71 Correct 101 ms 22856 KB Output is correct
72 Correct 99 ms 22856 KB Output is correct
73 Correct 50 ms 20792 KB Output is correct
74 Correct 158 ms 22500 KB Output is correct
75 Correct 31 ms 8784 KB Output is correct
76 Correct 2 ms 6480 KB Output is correct
77 Correct 104 ms 10320 KB Output is correct
78 Correct 167 ms 10912 KB Output is correct
79 Correct 133 ms 11080 KB Output is correct
80 Correct 205 ms 13136 KB Output is correct
81 Correct 205 ms 13136 KB Output is correct
82 Correct 214 ms 13136 KB Output is correct
83 Correct 211 ms 13072 KB Output is correct
84 Correct 216 ms 13256 KB Output is correct
85 Correct 218 ms 13136 KB Output is correct
86 Correct 215 ms 13128 KB Output is correct
87 Correct 213 ms 13128 KB Output is correct
88 Correct 197 ms 13384 KB Output is correct
89 Correct 198 ms 13564 KB Output is correct
90 Correct 209 ms 14940 KB Output is correct
91 Correct 193 ms 13288 KB Output is correct
92 Correct 199 ms 13248 KB Output is correct
93 Correct 190 ms 13392 KB Output is correct
94 Correct 253 ms 13308 KB Output is correct
95 Correct 267 ms 13136 KB Output is correct
96 Correct 271 ms 13136 KB Output is correct
97 Correct 274 ms 13136 KB Output is correct
98 Correct 275 ms 13136 KB Output is correct
99 Correct 276 ms 13276 KB Output is correct
100 Correct 285 ms 13128 KB Output is correct
101 Correct 297 ms 13128 KB Output is correct
102 Correct 297 ms 13140 KB Output is correct
103 Correct 312 ms 13128 KB Output is correct
104 Correct 296 ms 13128 KB Output is correct
105 Correct 311 ms 13640 KB Output is correct
106 Correct 276 ms 13392 KB Output is correct
107 Correct 274 ms 13596 KB Output is correct
108 Correct 284 ms 13968 KB Output is correct
109 Correct 281 ms 15096 KB Output is correct
110 Correct 1 ms 6480 KB Output is correct
111 Correct 1 ms 6480 KB Output is correct
112 Correct 2 ms 6480 KB Output is correct
113 Correct 243 ms 12612 KB Output is correct
114 Correct 237 ms 12636 KB Output is correct
115 Correct 286 ms 14296 KB Output is correct
116 Correct 299 ms 15180 KB Output is correct
117 Correct 301 ms 15176 KB Output is correct
118 Correct 292 ms 15220 KB Output is correct
119 Correct 299 ms 15176 KB Output is correct
120 Correct 294 ms 15176 KB Output is correct
121 Correct 295 ms 15176 KB Output is correct
122 Correct 297 ms 15176 KB Output is correct
123 Correct 36 ms 8784 KB Output is correct
124 Correct 208 ms 15168 KB Output is correct
125 Correct 152 ms 13892 KB Output is correct
126 Correct 232 ms 15328 KB Output is correct
127 Correct 264 ms 15300 KB Output is correct
128 Correct 237 ms 15176 KB Output is correct
129 Correct 254 ms 15184 KB Output is correct
130 Correct 239 ms 15328 KB Output is correct
131 Correct 166 ms 21732 KB Output is correct
132 Correct 157 ms 22856 KB Output is correct
133 Correct 166 ms 19032 KB Output is correct
134 Correct 280 ms 15432 KB Output is correct
135 Correct 298 ms 15252 KB Output is correct
136 Correct 288 ms 15432 KB Output is correct
137 Correct 100 ms 15552 KB Output is correct
138 Correct 108 ms 15552 KB Output is correct
139 Correct 96 ms 15552 KB Output is correct
140 Correct 109 ms 15552 KB Output is correct
141 Correct 95 ms 15456 KB Output is correct
142 Correct 95 ms 15604 KB Output is correct
143 Correct 40 ms 12872 KB Output is correct
144 Correct 221 ms 14920 KB Output is correct