Submission #789305

# Submission time Handle Problem Language Result Execution time Memory
789305 2023-07-21T09:05:44 Z 이성호(#10043) Tourism (JOI23_tourism) C++17
100 / 100
663 ms 31064 KB
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct FenWick
{
    vector<int> tree;
    int n;
    void init(int N)
    {
        n = N;
        tree.resize(N+1);
    }
    void upd(int i, int x)
    {
        for (; i <= n; i += i&-i) tree[i] += x;
    }
    int query(int i)
    {
        int ret = 0;
        for (; i; ret += tree[i], i -= i&-i);
        return ret;
    }
};
int N, M, Q, sz[100005], dep[100005], nxt[100005], in[100005], out[100005], par[100005];
vector<int> adj[100005], g[100005];
FenWick fenWick;
set<int> st;
pair<int, int> mmr[100005]; //i���� �����ϴ� (����, ��)
 //(����, (��, ��))

void dfs_sz(int v, int p)
{
    par[v] = p;
    dep[v] = dep[p] + 1;
    sz[v] = 1;
    for (int i:adj[v]) {
        if (i != p) {
            dfs_sz(i, v);
            g[v].push_back(i);
            if (sz[g[v][0]] < sz[g[v].back()]) swap(g[v][0], g[v].back());
        }
    }
}

int pv = 0;

void dfs_hld(int v)
{
    in[v] = pv++;
    for (int i:g[v]) {
        nxt[i] = i == g[v][0] ? nxt[v] : i;
        dfs_hld(i);
    }
    out[v] = pv;
}

void upd(int l, int r, int x)
{
    if (l > r) return;
    auto it = st.lower_bound(l+1); --it;
    if (*it < l) {
        if (mmr[*it].first >= r) {
            if (mmr[*it].first > r) {
                st.insert(r+1); mmr[r+1] = mmr[*it];
            }
            mmr[*it].first = l - 1;
            fenWick.upd(mmr[*it].second, -(r-l+1));
            mmr[l] = make_pair(r, x);
            st.insert(l);
            fenWick.upd(x, r-l+1);
            return;
        }
        else {
            fenWick.upd(mmr[*it].second, -(mmr[*it].first - (l - 1)));
            mmr[*it].first = l - 1;
        }
        ++it;
    }
    while (1) {
        if (mmr[*it].first >= r) {
            if (mmr[*it].first > r) {
                int tmp = *it; st.erase(it);
                st.insert(r+1); mmr[r+1] = mmr[tmp];
                fenWick.upd(mmr[tmp].second, -(r - tmp + 1));
            }
            else {
                int tmp = *it; st.erase(it);
                fenWick.upd(mmr[tmp].second, -(r - tmp + 1));
            }
            break;
        }
        int tmp = *it; ++it;
        fenWick.upd(mmr[tmp].second, -(mmr[tmp].first - tmp + 1));
        st.erase(tmp);
    }
    mmr[l] = make_pair(r, x);
    fenWick.upd(x, r-l+1);
    st.insert(l);
}

int lca(int u, int v)
{
    while (nxt[u] != nxt[v]) {
        if (dep[nxt[u]] < dep[nxt[v]]) swap(u, v);
        u = par[nxt[u]];
    }
    return dep[u] < dep[v] ? u : v;
}

void update(int u, int p, int x)
{
    while (1) {
        if (nxt[u] != nxt[p]) {
            upd(in[nxt[u]], in[u], x);
            u = par[nxt[u]];
        }
        else {
            upd(in[p] + 1, in[u], x);
            return;
        }
    }
}

void path_query(int u, int v, int x)
{
    int p = lca(u, v);
    update(u, p, x); update(v, p, x);
}

vector<pair<int, int>> query[100005];
int C[100005], ans[100005];

int main()
{
    cin >> N >> M >> Q;
    for (int i = 1; i < N; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    fenWick.init(N+1);
    dfs_sz(1, 0);
    nxt[1] = 1;
    dfs_hld(1);
    for (int i = 1; i <= M; i++) cin >> C[i];
    mmr[1] = make_pair(N-1, M);
    st.insert(1);
    for (int i = 1; i <= Q; i++) {
        int l, r; cin >> l >> r;
        query[l].push_back(make_pair(r-1, i));
    }
    fenWick.init(M);
    fenWick.upd(M, N-1);
    for (int i = M - 1; i >= 1; i--) {
        path_query(C[i], C[i+1], i);
        for (pair<int, int> p:query[i]) {
            ans[p.second] = fenWick.query(p.first);
        }
    }
    for (int i = 1; i <= Q; i++) cout << ans[i] + 1 << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7308 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 5 ms 7420 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7420 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7316 KB Output is correct
12 Correct 4 ms 7420 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 3 ms 7420 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 3 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 5 ms 7380 KB Output is correct
23 Correct 5 ms 7372 KB Output is correct
24 Correct 3 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 5 ms 7380 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7380 KB Output is correct
29 Correct 3 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7308 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 5 ms 7420 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7420 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7316 KB Output is correct
12 Correct 4 ms 7420 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 3 ms 7420 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 3 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 5 ms 7380 KB Output is correct
23 Correct 5 ms 7372 KB Output is correct
24 Correct 3 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 5 ms 7380 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7380 KB Output is correct
29 Correct 3 ms 7380 KB Output is correct
30 Correct 7 ms 7508 KB Output is correct
31 Correct 7 ms 7604 KB Output is correct
32 Correct 8 ms 7636 KB Output is correct
33 Correct 9 ms 7656 KB Output is correct
34 Correct 11 ms 7728 KB Output is correct
35 Correct 8 ms 7636 KB Output is correct
36 Correct 8 ms 7636 KB Output is correct
37 Correct 8 ms 7636 KB Output is correct
38 Correct 7 ms 7636 KB Output is correct
39 Correct 9 ms 7648 KB Output is correct
40 Correct 6 ms 7764 KB Output is correct
41 Correct 6 ms 7720 KB Output is correct
42 Correct 6 ms 7692 KB Output is correct
43 Correct 6 ms 7636 KB Output is correct
44 Correct 10 ms 7692 KB Output is correct
45 Correct 10 ms 7636 KB Output is correct
46 Correct 9 ms 7636 KB Output is correct
47 Correct 9 ms 7636 KB Output is correct
48 Correct 8 ms 7636 KB Output is correct
49 Correct 6 ms 7636 KB Output is correct
50 Correct 6 ms 7608 KB Output is correct
51 Correct 6 ms 7636 KB Output is correct
52 Correct 6 ms 7636 KB Output is correct
53 Correct 6 ms 7636 KB Output is correct
54 Correct 6 ms 7636 KB Output is correct
55 Correct 9 ms 7680 KB Output is correct
56 Correct 5 ms 7380 KB Output is correct
57 Correct 5 ms 7524 KB Output is correct
58 Correct 8 ms 7672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 127 ms 19888 KB Output is correct
5 Correct 122 ms 23760 KB Output is correct
6 Correct 146 ms 25704 KB Output is correct
7 Correct 177 ms 28340 KB Output is correct
8 Correct 180 ms 28348 KB Output is correct
9 Correct 187 ms 28432 KB Output is correct
10 Correct 196 ms 28436 KB Output is correct
11 Correct 201 ms 28328 KB Output is correct
12 Correct 171 ms 27972 KB Output is correct
13 Correct 172 ms 27964 KB Output is correct
14 Correct 164 ms 27964 KB Output is correct
15 Correct 108 ms 25412 KB Output is correct
16 Correct 188 ms 28080 KB Output is correct
17 Correct 88 ms 10800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 240 ms 14048 KB Output is correct
3 Correct 349 ms 14932 KB Output is correct
4 Correct 278 ms 15576 KB Output is correct
5 Correct 494 ms 19368 KB Output is correct
6 Correct 438 ms 19404 KB Output is correct
7 Correct 441 ms 19300 KB Output is correct
8 Correct 408 ms 19320 KB Output is correct
9 Correct 422 ms 19304 KB Output is correct
10 Correct 457 ms 20240 KB Output is correct
11 Correct 410 ms 19304 KB Output is correct
12 Correct 451 ms 19392 KB Output is correct
13 Correct 424 ms 20796 KB Output is correct
14 Correct 445 ms 21372 KB Output is correct
15 Correct 476 ms 25892 KB Output is correct
16 Correct 416 ms 20960 KB Output is correct
17 Correct 442 ms 21540 KB Output is correct
18 Correct 418 ms 20560 KB Output is correct
19 Correct 390 ms 19344 KB Output is correct
20 Correct 325 ms 18932 KB Output is correct
21 Correct 345 ms 18208 KB Output is correct
22 Correct 336 ms 17864 KB Output is correct
23 Correct 310 ms 17848 KB Output is correct
24 Correct 335 ms 17840 KB Output is correct
25 Correct 374 ms 17836 KB Output is correct
26 Correct 318 ms 18948 KB Output is correct
27 Correct 346 ms 19532 KB Output is correct
28 Correct 345 ms 19628 KB Output is correct
29 Correct 351 ms 19672 KB Output is correct
30 Correct 332 ms 19080 KB Output is correct
31 Correct 366 ms 19136 KB Output is correct
32 Correct 354 ms 19316 KB Output is correct
33 Correct 421 ms 21468 KB Output is correct
34 Correct 363 ms 24108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7340 KB Output is correct
3 Correct 6 ms 7428 KB Output is correct
4 Correct 476 ms 18160 KB Output is correct
5 Correct 505 ms 19644 KB Output is correct
6 Correct 599 ms 23184 KB Output is correct
7 Correct 613 ms 23048 KB Output is correct
8 Correct 627 ms 25276 KB Output is correct
9 Correct 623 ms 25468 KB Output is correct
10 Correct 630 ms 24692 KB Output is correct
11 Correct 640 ms 25468 KB Output is correct
12 Correct 663 ms 23084 KB Output is correct
13 Correct 658 ms 25240 KB Output is correct
14 Correct 71 ms 12060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7308 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 5 ms 7420 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7420 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7316 KB Output is correct
12 Correct 4 ms 7420 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 3 ms 7420 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 3 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 5 ms 7380 KB Output is correct
23 Correct 5 ms 7372 KB Output is correct
24 Correct 3 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 5 ms 7380 KB Output is correct
27 Correct 3 ms 7380 KB Output is correct
28 Correct 3 ms 7380 KB Output is correct
29 Correct 3 ms 7380 KB Output is correct
30 Correct 7 ms 7508 KB Output is correct
31 Correct 7 ms 7604 KB Output is correct
32 Correct 8 ms 7636 KB Output is correct
33 Correct 9 ms 7656 KB Output is correct
34 Correct 11 ms 7728 KB Output is correct
35 Correct 8 ms 7636 KB Output is correct
36 Correct 8 ms 7636 KB Output is correct
37 Correct 8 ms 7636 KB Output is correct
38 Correct 7 ms 7636 KB Output is correct
39 Correct 9 ms 7648 KB Output is correct
40 Correct 6 ms 7764 KB Output is correct
41 Correct 6 ms 7720 KB Output is correct
42 Correct 6 ms 7692 KB Output is correct
43 Correct 6 ms 7636 KB Output is correct
44 Correct 10 ms 7692 KB Output is correct
45 Correct 10 ms 7636 KB Output is correct
46 Correct 9 ms 7636 KB Output is correct
47 Correct 9 ms 7636 KB Output is correct
48 Correct 8 ms 7636 KB Output is correct
49 Correct 6 ms 7636 KB Output is correct
50 Correct 6 ms 7608 KB Output is correct
51 Correct 6 ms 7636 KB Output is correct
52 Correct 6 ms 7636 KB Output is correct
53 Correct 6 ms 7636 KB Output is correct
54 Correct 6 ms 7636 KB Output is correct
55 Correct 9 ms 7680 KB Output is correct
56 Correct 5 ms 7380 KB Output is correct
57 Correct 5 ms 7524 KB Output is correct
58 Correct 8 ms 7672 KB Output is correct
59 Correct 3 ms 7380 KB Output is correct
60 Correct 3 ms 7380 KB Output is correct
61 Correct 5 ms 7380 KB Output is correct
62 Correct 127 ms 19888 KB Output is correct
63 Correct 122 ms 23760 KB Output is correct
64 Correct 146 ms 25704 KB Output is correct
65 Correct 177 ms 28340 KB Output is correct
66 Correct 180 ms 28348 KB Output is correct
67 Correct 187 ms 28432 KB Output is correct
68 Correct 196 ms 28436 KB Output is correct
69 Correct 201 ms 28328 KB Output is correct
70 Correct 171 ms 27972 KB Output is correct
71 Correct 172 ms 27964 KB Output is correct
72 Correct 164 ms 27964 KB Output is correct
73 Correct 108 ms 25412 KB Output is correct
74 Correct 188 ms 28080 KB Output is correct
75 Correct 88 ms 10800 KB Output is correct
76 Correct 3 ms 7380 KB Output is correct
77 Correct 240 ms 14048 KB Output is correct
78 Correct 349 ms 14932 KB Output is correct
79 Correct 278 ms 15576 KB Output is correct
80 Correct 494 ms 19368 KB Output is correct
81 Correct 438 ms 19404 KB Output is correct
82 Correct 441 ms 19300 KB Output is correct
83 Correct 408 ms 19320 KB Output is correct
84 Correct 422 ms 19304 KB Output is correct
85 Correct 457 ms 20240 KB Output is correct
86 Correct 410 ms 19304 KB Output is correct
87 Correct 451 ms 19392 KB Output is correct
88 Correct 424 ms 20796 KB Output is correct
89 Correct 445 ms 21372 KB Output is correct
90 Correct 476 ms 25892 KB Output is correct
91 Correct 416 ms 20960 KB Output is correct
92 Correct 442 ms 21540 KB Output is correct
93 Correct 418 ms 20560 KB Output is correct
94 Correct 390 ms 19344 KB Output is correct
95 Correct 325 ms 18932 KB Output is correct
96 Correct 345 ms 18208 KB Output is correct
97 Correct 336 ms 17864 KB Output is correct
98 Correct 310 ms 17848 KB Output is correct
99 Correct 335 ms 17840 KB Output is correct
100 Correct 374 ms 17836 KB Output is correct
101 Correct 318 ms 18948 KB Output is correct
102 Correct 346 ms 19532 KB Output is correct
103 Correct 345 ms 19628 KB Output is correct
104 Correct 351 ms 19672 KB Output is correct
105 Correct 332 ms 19080 KB Output is correct
106 Correct 366 ms 19136 KB Output is correct
107 Correct 354 ms 19316 KB Output is correct
108 Correct 421 ms 21468 KB Output is correct
109 Correct 363 ms 24108 KB Output is correct
110 Correct 3 ms 7380 KB Output is correct
111 Correct 4 ms 7340 KB Output is correct
112 Correct 6 ms 7428 KB Output is correct
113 Correct 476 ms 18160 KB Output is correct
114 Correct 505 ms 19644 KB Output is correct
115 Correct 599 ms 23184 KB Output is correct
116 Correct 613 ms 23048 KB Output is correct
117 Correct 627 ms 25276 KB Output is correct
118 Correct 623 ms 25468 KB Output is correct
119 Correct 630 ms 24692 KB Output is correct
120 Correct 640 ms 25468 KB Output is correct
121 Correct 663 ms 23084 KB Output is correct
122 Correct 658 ms 25240 KB Output is correct
123 Correct 71 ms 12060 KB Output is correct
124 Correct 445 ms 24736 KB Output is correct
125 Correct 317 ms 22448 KB Output is correct
126 Correct 486 ms 25120 KB Output is correct
127 Correct 488 ms 25196 KB Output is correct
128 Correct 486 ms 25164 KB Output is correct
129 Correct 489 ms 25124 KB Output is correct
130 Correct 487 ms 25232 KB Output is correct
131 Correct 196 ms 30008 KB Output is correct
132 Correct 184 ms 31064 KB Output is correct
133 Correct 191 ms 27228 KB Output is correct
134 Correct 394 ms 23660 KB Output is correct
135 Correct 395 ms 23608 KB Output is correct
136 Correct 397 ms 23676 KB Output is correct
137 Correct 217 ms 25032 KB Output is correct
138 Correct 204 ms 25028 KB Output is correct
139 Correct 206 ms 25020 KB Output is correct
140 Correct 210 ms 25140 KB Output is correct
141 Correct 230 ms 25096 KB Output is correct
142 Correct 210 ms 24960 KB Output is correct
143 Correct 95 ms 17588 KB Output is correct
144 Correct 488 ms 24984 KB Output is correct