답안 #970526

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
970526 2024-04-26T16:25:29 Z vjudge1 Tourism (JOI23_tourism) C++17
100 / 100
254 ms 27280 KB
#include<bits/stdc++.h>
#define ll long long
#define task ""
using namespace std;
const int maxn = 1e5 + 4;
int n, m, a, b, q, l, r, fw[maxn], city[maxn], res[maxn];
int par[maxn], sz[maxn], dep[maxn], nxt[maxn];
int curchain = 1, curpos = 0, head[maxn], chain[maxn], pos[maxn];
bool visited[maxn];
vector<int> adj[maxn];
vector<pair<int, int>> query[maxn];
//basic hld :3
void dfs (int u)
{
    sz[u] = 1;
    visited[u] = true;
    for (int v : adj[u])
        if (!visited[v])
        {
            par[v] = u;
            dep[v] = dep[u] + 1;
            dfs(v);
            sz[u] += sz[v];
            if (nxt[u] == 0 || sz[v] > sz[nxt[u]]) nxt[u] = v;
        }
}
void hld (int u)
{
    if (!head[curchain]) head[curchain] = u;
    chain[u] = curchain;
    pos[u] = ++curpos;
    if (nxt[u] != 0) hld(nxt[u]);
    for (int v : adj[u])
        if (v != par[u] && v != nxt[u])
        {
            curchain++;
            hld(v);
        }
}
//fenwick tree to answer the queries
void upd (int idx, int val)
{
    while (idx <= m)
    {
        fw[idx] += val;
        idx += (idx & (-idx));
    }
}
int get (int idx)
{
    int res = 0;
    while (idx > 0)
    {
        res += fw[idx];
        idx -= (idx & (-idx));
    }
    return res;
}
//updating the path from u to v with the corresponding time
struct Event
{
    int l, r, t;
    bool operator < (const Event& other) const
    {
        return l < other.l;
    }
};
multiset<Event> line[maxn]; //each multiset manages a chain (hopefully i understand this correctly)
void update (int u, int v, int t)
{
    while (chain[u] != chain[v])
    {
        if (chain[u] < chain[v]) swap(u, v);
        Event e = {pos[head[chain[u]]], pos[u], t};
        int id = chain[u];
        while (!line[id].empty())
        {
            auto it = line[id].begin();
            Event f = *it;
            if (f.l > e.r) break;
            upd(f.t, -(f.r - f.l + 1));
            line[id].erase(it);
            if (f.r > e.r)
            {
                f.l = e.r + 1;
                line[id].insert(f);
                upd(f.t, f.r - e.r);
            }
            //remember the event e always start at the head of the chain so e.l <= f.l
        }
        upd(e.t, e.r - e.l + 1);
        line[id].insert(e);
        u = par[head[chain[u]]];
    }
    if (pos[u] > pos[v]) swap(u, v);
    Event e = {pos[u], pos[v], t};
    int id = chain[u];
    while (!line[id].empty())
    {
        auto it = line[id].upper_bound(e);
        if (it == line[id].begin()) break;
        it--;
        Event f = *it;
        if (f.r < e.l) break;
        upd(f.t, -(f.r - f.l + 1));
        line[id].erase(it);
        if (f.r > e.r)
        {
            int prevfl = f.l;
            f.l = e.r + 1;
            upd(f.t, f.r - e.r);
            line[id].insert(f);
            f.r = e.r;
            f.l = prevfl;
        }
        if (f.l < e.l)
        {
            f.r = e.l - 1;
            upd(f.t, f.r - f.l + 1);
            line[id].insert(f);
        }
    }
    while (!line[id].empty())
    {
        auto it = line[id].lower_bound(e);
        if (it == line[id].end()) break;
        Event f = *it;
        if (f.l > e.r) break;
        upd(f.t, -(f.r - f.l + 1));
        line[id].erase(it);
        if (f.r > e.r)
        {
            f.l = e.r + 1;
            upd(f.t, f.r - f.l + 1);
            line[id].insert(f);
        }
        //again, e.l <= f.l
    }
    upd(e.t, e.r - e.l + 1);
    line[id].insert(e);
}
int main ()
{
    //freopen(task".INP", "r", stdin);
    //freopen(task".OUT", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++)
    {
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (int i = 1; i <= m; i++) cin >> city[i];
    for (int i = 1; i <= q; i++)
    {
        cin >> l >> r;
        if (l == r) res[i] = 1;
        else query[r].push_back({l, i});
    }
    dfs(1);
    hld(1);
    for (int i = 2; i <= m; i++)
    {
        update(city[i], city[i - 1], i - 1);
        for (pair<int, int> p : query[i])
            res[p.second] = get(i) - get(p.first - 1);
    }
    for (int i = 1; i <= q; i++) cout << res[i] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 4 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 4 ms 12888 KB Output is correct
5 Correct 4 ms 12888 KB Output is correct
6 Correct 4 ms 12960 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12892 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12972 KB Output is correct
11 Correct 3 ms 12888 KB Output is correct
12 Correct 3 ms 12888 KB Output is correct
13 Correct 3 ms 12892 KB Output is correct
14 Correct 3 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12888 KB Output is correct
18 Correct 4 ms 12988 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 2 ms 12888 KB Output is correct
23 Correct 4 ms 12892 KB Output is correct
24 Correct 3 ms 12952 KB Output is correct
25 Correct 3 ms 12892 KB Output is correct
26 Correct 3 ms 12892 KB Output is correct
27 Correct 2 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 4 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 4 ms 12888 KB Output is correct
5 Correct 4 ms 12888 KB Output is correct
6 Correct 4 ms 12960 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12892 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12972 KB Output is correct
11 Correct 3 ms 12888 KB Output is correct
12 Correct 3 ms 12888 KB Output is correct
13 Correct 3 ms 12892 KB Output is correct
14 Correct 3 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12888 KB Output is correct
18 Correct 4 ms 12988 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 2 ms 12888 KB Output is correct
23 Correct 4 ms 12892 KB Output is correct
24 Correct 3 ms 12952 KB Output is correct
25 Correct 3 ms 12892 KB Output is correct
26 Correct 3 ms 12892 KB Output is correct
27 Correct 2 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12892 KB Output is correct
30 Correct 4 ms 12892 KB Output is correct
31 Correct 4 ms 12892 KB Output is correct
32 Correct 5 ms 13148 KB Output is correct
33 Correct 5 ms 13144 KB Output is correct
34 Correct 6 ms 13148 KB Output is correct
35 Correct 6 ms 13144 KB Output is correct
36 Correct 5 ms 12892 KB Output is correct
37 Correct 5 ms 13088 KB Output is correct
38 Correct 5 ms 13160 KB Output is correct
39 Correct 5 ms 13144 KB Output is correct
40 Correct 4 ms 13148 KB Output is correct
41 Correct 4 ms 13148 KB Output is correct
42 Correct 4 ms 13148 KB Output is correct
43 Correct 4 ms 13148 KB Output is correct
44 Correct 5 ms 12892 KB Output is correct
45 Correct 7 ms 13244 KB Output is correct
46 Correct 5 ms 13148 KB Output is correct
47 Correct 4 ms 12892 KB Output is correct
48 Correct 4 ms 12892 KB Output is correct
49 Correct 5 ms 12892 KB Output is correct
50 Correct 4 ms 13148 KB Output is correct
51 Correct 4 ms 13144 KB Output is correct
52 Correct 4 ms 13148 KB Output is correct
53 Correct 5 ms 13148 KB Output is correct
54 Correct 4 ms 13144 KB Output is correct
55 Correct 4 ms 13148 KB Output is correct
56 Correct 4 ms 12976 KB Output is correct
57 Correct 3 ms 12892 KB Output is correct
58 Correct 5 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 71 ms 21708 KB Output is correct
5 Correct 63 ms 23820 KB Output is correct
6 Correct 73 ms 25172 KB Output is correct
7 Correct 96 ms 27212 KB Output is correct
8 Correct 95 ms 27176 KB Output is correct
9 Correct 96 ms 27272 KB Output is correct
10 Correct 94 ms 27280 KB Output is correct
11 Correct 92 ms 27260 KB Output is correct
12 Correct 90 ms 26996 KB Output is correct
13 Correct 92 ms 26884 KB Output is correct
14 Correct 90 ms 26936 KB Output is correct
15 Correct 42 ms 24148 KB Output is correct
16 Correct 83 ms 25168 KB Output is correct
17 Correct 38 ms 15792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 82 ms 16900 KB Output is correct
3 Correct 122 ms 17888 KB Output is correct
4 Correct 100 ms 17820 KB Output is correct
5 Correct 151 ms 20524 KB Output is correct
6 Correct 198 ms 20052 KB Output is correct
7 Correct 147 ms 20284 KB Output is correct
8 Correct 180 ms 20240 KB Output is correct
9 Correct 147 ms 20240 KB Output is correct
10 Correct 157 ms 20272 KB Output is correct
11 Correct 167 ms 20524 KB Output is correct
12 Correct 163 ms 20360 KB Output is correct
13 Correct 160 ms 20500 KB Output is correct
14 Correct 160 ms 21076 KB Output is correct
15 Correct 180 ms 20492 KB Output is correct
16 Correct 164 ms 20564 KB Output is correct
17 Correct 159 ms 20620 KB Output is correct
18 Correct 165 ms 20580 KB Output is correct
19 Correct 122 ms 17240 KB Output is correct
20 Correct 123 ms 17036 KB Output is correct
21 Correct 120 ms 16976 KB Output is correct
22 Correct 131 ms 17544 KB Output is correct
23 Correct 119 ms 17140 KB Output is correct
24 Correct 128 ms 17236 KB Output is correct
25 Correct 121 ms 16980 KB Output is correct
26 Correct 141 ms 17220 KB Output is correct
27 Correct 129 ms 17228 KB Output is correct
28 Correct 121 ms 17236 KB Output is correct
29 Correct 126 ms 17300 KB Output is correct
30 Correct 137 ms 17444 KB Output is correct
31 Correct 127 ms 17372 KB Output is correct
32 Correct 140 ms 17744 KB Output is correct
33 Correct 136 ms 18260 KB Output is correct
34 Correct 134 ms 17636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Correct 190 ms 20388 KB Output is correct
5 Correct 201 ms 20348 KB Output is correct
6 Correct 232 ms 22356 KB Output is correct
7 Correct 244 ms 23376 KB Output is correct
8 Correct 232 ms 23376 KB Output is correct
9 Correct 234 ms 23356 KB Output is correct
10 Correct 247 ms 23172 KB Output is correct
11 Correct 239 ms 23292 KB Output is correct
12 Correct 231 ms 23284 KB Output is correct
13 Correct 254 ms 23376 KB Output is correct
14 Correct 37 ms 15956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 4 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 4 ms 12888 KB Output is correct
5 Correct 4 ms 12888 KB Output is correct
6 Correct 4 ms 12960 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12892 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12972 KB Output is correct
11 Correct 3 ms 12888 KB Output is correct
12 Correct 3 ms 12888 KB Output is correct
13 Correct 3 ms 12892 KB Output is correct
14 Correct 3 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12888 KB Output is correct
18 Correct 4 ms 12988 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 2 ms 12888 KB Output is correct
23 Correct 4 ms 12892 KB Output is correct
24 Correct 3 ms 12952 KB Output is correct
25 Correct 3 ms 12892 KB Output is correct
26 Correct 3 ms 12892 KB Output is correct
27 Correct 2 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12892 KB Output is correct
30 Correct 4 ms 12892 KB Output is correct
31 Correct 4 ms 12892 KB Output is correct
32 Correct 5 ms 13148 KB Output is correct
33 Correct 5 ms 13144 KB Output is correct
34 Correct 6 ms 13148 KB Output is correct
35 Correct 6 ms 13144 KB Output is correct
36 Correct 5 ms 12892 KB Output is correct
37 Correct 5 ms 13088 KB Output is correct
38 Correct 5 ms 13160 KB Output is correct
39 Correct 5 ms 13144 KB Output is correct
40 Correct 4 ms 13148 KB Output is correct
41 Correct 4 ms 13148 KB Output is correct
42 Correct 4 ms 13148 KB Output is correct
43 Correct 4 ms 13148 KB Output is correct
44 Correct 5 ms 12892 KB Output is correct
45 Correct 7 ms 13244 KB Output is correct
46 Correct 5 ms 13148 KB Output is correct
47 Correct 4 ms 12892 KB Output is correct
48 Correct 4 ms 12892 KB Output is correct
49 Correct 5 ms 12892 KB Output is correct
50 Correct 4 ms 13148 KB Output is correct
51 Correct 4 ms 13144 KB Output is correct
52 Correct 4 ms 13148 KB Output is correct
53 Correct 5 ms 13148 KB Output is correct
54 Correct 4 ms 13144 KB Output is correct
55 Correct 4 ms 13148 KB Output is correct
56 Correct 4 ms 12976 KB Output is correct
57 Correct 3 ms 12892 KB Output is correct
58 Correct 5 ms 12892 KB Output is correct
59 Correct 2 ms 12892 KB Output is correct
60 Correct 3 ms 12892 KB Output is correct
61 Correct 3 ms 12892 KB Output is correct
62 Correct 71 ms 21708 KB Output is correct
63 Correct 63 ms 23820 KB Output is correct
64 Correct 73 ms 25172 KB Output is correct
65 Correct 96 ms 27212 KB Output is correct
66 Correct 95 ms 27176 KB Output is correct
67 Correct 96 ms 27272 KB Output is correct
68 Correct 94 ms 27280 KB Output is correct
69 Correct 92 ms 27260 KB Output is correct
70 Correct 90 ms 26996 KB Output is correct
71 Correct 92 ms 26884 KB Output is correct
72 Correct 90 ms 26936 KB Output is correct
73 Correct 42 ms 24148 KB Output is correct
74 Correct 83 ms 25168 KB Output is correct
75 Correct 38 ms 15792 KB Output is correct
76 Correct 2 ms 12892 KB Output is correct
77 Correct 82 ms 16900 KB Output is correct
78 Correct 122 ms 17888 KB Output is correct
79 Correct 100 ms 17820 KB Output is correct
80 Correct 151 ms 20524 KB Output is correct
81 Correct 198 ms 20052 KB Output is correct
82 Correct 147 ms 20284 KB Output is correct
83 Correct 180 ms 20240 KB Output is correct
84 Correct 147 ms 20240 KB Output is correct
85 Correct 157 ms 20272 KB Output is correct
86 Correct 167 ms 20524 KB Output is correct
87 Correct 163 ms 20360 KB Output is correct
88 Correct 160 ms 20500 KB Output is correct
89 Correct 160 ms 21076 KB Output is correct
90 Correct 180 ms 20492 KB Output is correct
91 Correct 164 ms 20564 KB Output is correct
92 Correct 159 ms 20620 KB Output is correct
93 Correct 165 ms 20580 KB Output is correct
94 Correct 122 ms 17240 KB Output is correct
95 Correct 123 ms 17036 KB Output is correct
96 Correct 120 ms 16976 KB Output is correct
97 Correct 131 ms 17544 KB Output is correct
98 Correct 119 ms 17140 KB Output is correct
99 Correct 128 ms 17236 KB Output is correct
100 Correct 121 ms 16980 KB Output is correct
101 Correct 141 ms 17220 KB Output is correct
102 Correct 129 ms 17228 KB Output is correct
103 Correct 121 ms 17236 KB Output is correct
104 Correct 126 ms 17300 KB Output is correct
105 Correct 137 ms 17444 KB Output is correct
106 Correct 127 ms 17372 KB Output is correct
107 Correct 140 ms 17744 KB Output is correct
108 Correct 136 ms 18260 KB Output is correct
109 Correct 134 ms 17636 KB Output is correct
110 Correct 2 ms 12892 KB Output is correct
111 Correct 2 ms 12892 KB Output is correct
112 Correct 4 ms 12892 KB Output is correct
113 Correct 190 ms 20388 KB Output is correct
114 Correct 201 ms 20348 KB Output is correct
115 Correct 232 ms 22356 KB Output is correct
116 Correct 244 ms 23376 KB Output is correct
117 Correct 232 ms 23376 KB Output is correct
118 Correct 234 ms 23356 KB Output is correct
119 Correct 247 ms 23172 KB Output is correct
120 Correct 239 ms 23292 KB Output is correct
121 Correct 231 ms 23284 KB Output is correct
122 Correct 254 ms 23376 KB Output is correct
123 Correct 37 ms 15956 KB Output is correct
124 Correct 167 ms 22380 KB Output is correct
125 Correct 140 ms 20560 KB Output is correct
126 Correct 200 ms 22992 KB Output is correct
127 Correct 205 ms 23108 KB Output is correct
128 Correct 208 ms 22840 KB Output is correct
129 Correct 190 ms 22832 KB Output is correct
130 Correct 215 ms 22972 KB Output is correct
131 Correct 100 ms 26100 KB Output is correct
132 Correct 103 ms 27268 KB Output is correct
133 Correct 97 ms 23452 KB Output is correct
134 Correct 158 ms 19956 KB Output is correct
135 Correct 162 ms 19796 KB Output is correct
136 Correct 171 ms 19740 KB Output is correct
137 Correct 88 ms 23844 KB Output is correct
138 Correct 87 ms 23756 KB Output is correct
139 Correct 99 ms 23752 KB Output is correct
140 Correct 89 ms 23648 KB Output is correct
141 Correct 99 ms 23864 KB Output is correct
142 Correct 90 ms 23676 KB Output is correct
143 Correct 48 ms 16468 KB Output is correct
144 Correct 181 ms 20620 KB Output is correct