답안 #970525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
970525 2024-04-26T16:24:39 Z windowwife Tourism (JOI23_tourism) C++14
100 / 100
253 ms 30172 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 4 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12728 KB Output is correct
4 Correct 4 ms 12888 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 3 ms 13144 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12972 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12976 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 4 ms 12996 KB Output is correct
13 Correct 4 ms 12892 KB Output is correct
14 Correct 3 ms 12888 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 12912 KB Output is correct
18 Correct 3 ms 12888 KB Output is correct
19 Correct 4 ms 12948 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 3 ms 12980 KB Output is correct
22 Correct 3 ms 12888 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 3 ms 12984 KB Output is correct
26 Correct 3 ms 12892 KB Output is correct
27 Correct 3 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12728 KB Output is correct
4 Correct 4 ms 12888 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 3 ms 13144 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12972 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12976 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 4 ms 12996 KB Output is correct
13 Correct 4 ms 12892 KB Output is correct
14 Correct 3 ms 12888 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 12912 KB Output is correct
18 Correct 3 ms 12888 KB Output is correct
19 Correct 4 ms 12948 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 3 ms 12980 KB Output is correct
22 Correct 3 ms 12888 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 3 ms 12984 KB Output is correct
26 Correct 3 ms 12892 KB Output is correct
27 Correct 3 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12956 KB Output is correct
30 Correct 5 ms 12896 KB Output is correct
31 Correct 5 ms 12956 KB Output is correct
32 Correct 5 ms 13152 KB Output is correct
33 Correct 5 ms 13160 KB Output is correct
34 Correct 5 ms 13152 KB Output is correct
35 Correct 5 ms 13144 KB Output is correct
36 Correct 5 ms 13160 KB Output is correct
37 Correct 5 ms 13160 KB Output is correct
38 Correct 4 ms 13152 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 13064 KB Output is correct
42 Correct 4 ms 13144 KB Output is correct
43 Correct 5 ms 13148 KB Output is correct
44 Correct 5 ms 13332 KB Output is correct
45 Correct 5 ms 13152 KB Output is correct
46 Correct 4 ms 13148 KB Output is correct
47 Correct 4 ms 13148 KB Output is correct
48 Correct 5 ms 13148 KB Output is correct
49 Correct 4 ms 13148 KB Output is correct
50 Correct 4 ms 13148 KB Output is correct
51 Correct 4 ms 13208 KB Output is correct
52 Correct 5 ms 13148 KB Output is correct
53 Correct 4 ms 13148 KB Output is correct
54 Correct 4 ms 13056 KB Output is correct
55 Correct 4 ms 13216 KB Output is correct
56 Correct 4 ms 13160 KB Output is correct
57 Correct 4 ms 12960 KB Output is correct
58 Correct 5 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 3 ms 12888 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 72 ms 23748 KB Output is correct
5 Correct 62 ms 25680 KB Output is correct
6 Correct 73 ms 27216 KB Output is correct
7 Correct 95 ms 30036 KB Output is correct
8 Correct 95 ms 30036 KB Output is correct
9 Correct 97 ms 30044 KB Output is correct
10 Correct 94 ms 30152 KB Output is correct
11 Correct 92 ms 30036 KB Output is correct
12 Correct 92 ms 29812 KB Output is correct
13 Correct 90 ms 29520 KB Output is correct
14 Correct 91 ms 29524 KB Output is correct
15 Correct 45 ms 26068 KB Output is correct
16 Correct 80 ms 27808 KB Output is correct
17 Correct 39 ms 17236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 80 ms 17952 KB Output is correct
3 Correct 124 ms 19016 KB Output is correct
4 Correct 102 ms 18900 KB Output is correct
5 Correct 154 ms 21796 KB Output is correct
6 Correct 159 ms 21912 KB Output is correct
7 Correct 163 ms 22260 KB Output is correct
8 Correct 151 ms 21844 KB Output is correct
9 Correct 161 ms 21844 KB Output is correct
10 Correct 189 ms 21840 KB Output is correct
11 Correct 151 ms 21824 KB Output is correct
12 Correct 156 ms 21936 KB Output is correct
13 Correct 162 ms 22432 KB Output is correct
14 Correct 166 ms 23124 KB Output is correct
15 Correct 171 ms 23316 KB Output is correct
16 Correct 177 ms 22612 KB Output is correct
17 Correct 164 ms 22452 KB Output is correct
18 Correct 160 ms 22356 KB Output is correct
19 Correct 123 ms 18796 KB Output is correct
20 Correct 124 ms 19128 KB Output is correct
21 Correct 124 ms 18720 KB Output is correct
22 Correct 124 ms 18924 KB Output is correct
23 Correct 120 ms 18772 KB Output is correct
24 Correct 124 ms 18772 KB Output is correct
25 Correct 120 ms 18768 KB Output is correct
26 Correct 124 ms 18844 KB Output is correct
27 Correct 122 ms 18876 KB Output is correct
28 Correct 121 ms 18728 KB Output is correct
29 Correct 122 ms 19024 KB Output is correct
30 Correct 128 ms 19052 KB Output is correct
31 Correct 125 ms 19284 KB Output is correct
32 Correct 127 ms 19788 KB Output is correct
33 Correct 139 ms 20660 KB Output is correct
34 Correct 135 ms 20460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 4 ms 13204 KB Output is correct
4 Correct 201 ms 22544 KB Output is correct
5 Correct 193 ms 22492 KB Output is correct
6 Correct 233 ms 25088 KB Output is correct
7 Correct 233 ms 26200 KB Output is correct
8 Correct 253 ms 26320 KB Output is correct
9 Correct 232 ms 26192 KB Output is correct
10 Correct 241 ms 26196 KB Output is correct
11 Correct 244 ms 26116 KB Output is correct
12 Correct 244 ms 26068 KB Output is correct
13 Correct 237 ms 26232 KB Output is correct
14 Correct 39 ms 17236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12728 KB Output is correct
4 Correct 4 ms 12888 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 3 ms 13144 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12972 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12976 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 4 ms 12996 KB Output is correct
13 Correct 4 ms 12892 KB Output is correct
14 Correct 3 ms 12888 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 12912 KB Output is correct
18 Correct 3 ms 12888 KB Output is correct
19 Correct 4 ms 12948 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 3 ms 12980 KB Output is correct
22 Correct 3 ms 12888 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 3 ms 12984 KB Output is correct
26 Correct 3 ms 12892 KB Output is correct
27 Correct 3 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12956 KB Output is correct
30 Correct 5 ms 12896 KB Output is correct
31 Correct 5 ms 12956 KB Output is correct
32 Correct 5 ms 13152 KB Output is correct
33 Correct 5 ms 13160 KB Output is correct
34 Correct 5 ms 13152 KB Output is correct
35 Correct 5 ms 13144 KB Output is correct
36 Correct 5 ms 13160 KB Output is correct
37 Correct 5 ms 13160 KB Output is correct
38 Correct 4 ms 13152 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 13064 KB Output is correct
42 Correct 4 ms 13144 KB Output is correct
43 Correct 5 ms 13148 KB Output is correct
44 Correct 5 ms 13332 KB Output is correct
45 Correct 5 ms 13152 KB Output is correct
46 Correct 4 ms 13148 KB Output is correct
47 Correct 4 ms 13148 KB Output is correct
48 Correct 5 ms 13148 KB Output is correct
49 Correct 4 ms 13148 KB Output is correct
50 Correct 4 ms 13148 KB Output is correct
51 Correct 4 ms 13208 KB Output is correct
52 Correct 5 ms 13148 KB Output is correct
53 Correct 4 ms 13148 KB Output is correct
54 Correct 4 ms 13056 KB Output is correct
55 Correct 4 ms 13216 KB Output is correct
56 Correct 4 ms 13160 KB Output is correct
57 Correct 4 ms 12960 KB Output is correct
58 Correct 5 ms 13148 KB Output is correct
59 Correct 2 ms 12888 KB Output is correct
60 Correct 3 ms 12888 KB Output is correct
61 Correct 3 ms 12892 KB Output is correct
62 Correct 72 ms 23748 KB Output is correct
63 Correct 62 ms 25680 KB Output is correct
64 Correct 73 ms 27216 KB Output is correct
65 Correct 95 ms 30036 KB Output is correct
66 Correct 95 ms 30036 KB Output is correct
67 Correct 97 ms 30044 KB Output is correct
68 Correct 94 ms 30152 KB Output is correct
69 Correct 92 ms 30036 KB Output is correct
70 Correct 92 ms 29812 KB Output is correct
71 Correct 90 ms 29520 KB Output is correct
72 Correct 91 ms 29524 KB Output is correct
73 Correct 45 ms 26068 KB Output is correct
74 Correct 80 ms 27808 KB Output is correct
75 Correct 39 ms 17236 KB Output is correct
76 Correct 3 ms 12892 KB Output is correct
77 Correct 80 ms 17952 KB Output is correct
78 Correct 124 ms 19016 KB Output is correct
79 Correct 102 ms 18900 KB Output is correct
80 Correct 154 ms 21796 KB Output is correct
81 Correct 159 ms 21912 KB Output is correct
82 Correct 163 ms 22260 KB Output is correct
83 Correct 151 ms 21844 KB Output is correct
84 Correct 161 ms 21844 KB Output is correct
85 Correct 189 ms 21840 KB Output is correct
86 Correct 151 ms 21824 KB Output is correct
87 Correct 156 ms 21936 KB Output is correct
88 Correct 162 ms 22432 KB Output is correct
89 Correct 166 ms 23124 KB Output is correct
90 Correct 171 ms 23316 KB Output is correct
91 Correct 177 ms 22612 KB Output is correct
92 Correct 164 ms 22452 KB Output is correct
93 Correct 160 ms 22356 KB Output is correct
94 Correct 123 ms 18796 KB Output is correct
95 Correct 124 ms 19128 KB Output is correct
96 Correct 124 ms 18720 KB Output is correct
97 Correct 124 ms 18924 KB Output is correct
98 Correct 120 ms 18772 KB Output is correct
99 Correct 124 ms 18772 KB Output is correct
100 Correct 120 ms 18768 KB Output is correct
101 Correct 124 ms 18844 KB Output is correct
102 Correct 122 ms 18876 KB Output is correct
103 Correct 121 ms 18728 KB Output is correct
104 Correct 122 ms 19024 KB Output is correct
105 Correct 128 ms 19052 KB Output is correct
106 Correct 125 ms 19284 KB Output is correct
107 Correct 127 ms 19788 KB Output is correct
108 Correct 139 ms 20660 KB Output is correct
109 Correct 135 ms 20460 KB Output is correct
110 Correct 3 ms 12892 KB Output is correct
111 Correct 3 ms 12892 KB Output is correct
112 Correct 4 ms 13204 KB Output is correct
113 Correct 201 ms 22544 KB Output is correct
114 Correct 193 ms 22492 KB Output is correct
115 Correct 233 ms 25088 KB Output is correct
116 Correct 233 ms 26200 KB Output is correct
117 Correct 253 ms 26320 KB Output is correct
118 Correct 232 ms 26192 KB Output is correct
119 Correct 241 ms 26196 KB Output is correct
120 Correct 244 ms 26116 KB Output is correct
121 Correct 244 ms 26068 KB Output is correct
122 Correct 237 ms 26232 KB Output is correct
123 Correct 39 ms 17236 KB Output is correct
124 Correct 161 ms 25072 KB Output is correct
125 Correct 126 ms 22688 KB Output is correct
126 Correct 189 ms 25708 KB Output is correct
127 Correct 185 ms 25812 KB Output is correct
128 Correct 194 ms 25764 KB Output is correct
129 Correct 187 ms 25656 KB Output is correct
130 Correct 183 ms 25684 KB Output is correct
131 Correct 100 ms 28856 KB Output is correct
132 Correct 108 ms 30172 KB Output is correct
133 Correct 98 ms 26256 KB Output is correct
134 Correct 151 ms 22864 KB Output is correct
135 Correct 152 ms 22684 KB Output is correct
136 Correct 159 ms 23168 KB Output is correct
137 Correct 91 ms 26580 KB Output is correct
138 Correct 103 ms 26604 KB Output is correct
139 Correct 93 ms 26568 KB Output is correct
140 Correct 90 ms 26724 KB Output is correct
141 Correct 92 ms 26708 KB Output is correct
142 Correct 97 ms 26700 KB Output is correct
143 Correct 48 ms 18052 KB Output is correct
144 Correct 173 ms 23296 KB Output is correct