답안 #834225

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
834225 2023-08-22T12:03:34 Z LittleCube Tourism (JOI23_tourism) C++17
100 / 100
249 ms 56680 KB
    #include <bits/stdc++.h>
    #define ll long long
    #define pii pair<int, int>
    #define pll pair<ll, ll>
    #define F first
    #define S second
    using namespace std;
     
    int N, M, Q, K, t, ans[100005], pre[100005], C[100005], dep[100005], head[100005], sz[100005], tableE[200005][20], bit[200005];
    pii io[100005], tableD[100005][20];
    vector<int> E[100005], euler = {-1};
    vector<pii> qu[100005];
    vector<pii> chain[100005];
     
    void modify(int pos, int val)
    {
    	if(pos == 0)
    		return;
    	for (int i = pos; i <= M; i += (i & -i))
    		bit[i] += val;
    }
     
    int query(int pos)
    {
    	int val = 0;
    	for (int i = pos; i > 0; i -= (i & -i))
    		val += bit[i];
    	return val;
    }
     
     
    void dfs(int u)
    {
    	euler.emplace_back(u);
    	io[u].F = ++t;
    	for (auto v : E[u])
    		if(v != pre[u])
    		{
    			pre[v] = u;
    			dep[v] = dep[u] + 1;
    			dfs(v);
    			sz[u] += sz[v];
    			++t;
    			euler.emplace_back(u);
    		}
    	sz[u]++;
    	io[u].S = t;
    }
     
    void hld(int u, int r)
    {
    	head[u] = r;
    	int mxz = 0, mi = 0;
    	for (auto v : E[u])
    		if(v != pre[u])
    			if(sz[v] > mxz)
    				mi = v, mxz = sz[v];
    	for (auto v : E[u])
    		if(v != pre[u])
    			hld(v, (v == mi ? r : v));
    }
     
    signed main()
    {
    	ios::sync_with_stdio(0);
    	cin.tie(0), cout.tie(0);
    	cin >> N >> M >> Q;
    	E[0].emplace_back(1);
    	for (int i = 1; i < N; i++)
    	{
    		int u, v;
    		cin >> u >> v;
    		E[u].emplace_back(v);
    		E[v].emplace_back(u);
    	}
    	for (int i = 1; i <= M; i++)
    		cin >> C[i];
    	pre[0] = -1;
    	dfs(0);
    	hld(0, 0);
    	K = euler.size() - 1;
    	for (int i = 1; i <= K; i++)
    		tableE[i][0] = dep[euler[i]];
    	for (int p = 0; p + 1 < 20; p++)
    		for (int i = 1; i + (1 << p) <= K; i++)
    			tableE[i][p + 1] = min(tableE[i][p], tableE[i + (1 << p)][p]);
    	for (int i = 1; i <= M; i++)
    		tableD[i][0] = io[C[i]];
    	for (int p = 0; p + 1 < 20; p++)
    		for (int i = 1; i + (1 << p) <= M; i++)
    			tableD[i][p + 1].F = min(tableD[i][p].F, tableD[i + (1 << p)][p].F),
    			tableD[i][p + 1].S = max(tableD[i][p].S, tableD[i + (1 << p)][p].S);
    	for (int i = 1; i <= Q; i++)
    	{
    		int L, R;
    		cin >> L >> R;
    		qu[R].emplace_back(pii(L, i));
    	}
    	for (int r = 1; r <= M; r++)
    	{
    		int u = C[r], h;
    		while (u != -1)
    		{
    			h = head[u];
    			while (!chain[h].empty() && dep[chain[h].back().F] <= dep[u])
    			{
    				auto [v, j] = chain[h].back();
    				int val = dep[v] - dep[h] + 1;
    				modify(j, -val);
    				chain[h].pop_back();
    				if (!chain[h].empty())
    				{
    					j = chain[h].back().S;
    					modify(j, val);
    				}			
    			}
    			int val = dep[u] - dep[h] + 1;
    			modify(r, val);
    			if (!chain[h].empty())
    			{
    				int j = chain[h].back().S;
    				modify(j, -val);
    			}
    			chain[h].emplace_back(pii(u, r));
    			u = pre[h];
    		}
    		for (auto [l, idx] : qu[r])
    		{
    			int lg = __lg(r - l + 1);
    			pii iot = pii(min(tableD[l][lg].F, tableD[r - (1 << lg) + 1][lg].F),
    						  max(tableD[l][lg].S, tableD[r - (1 << lg) + 1][lg].S));
    			lg = __lg(iot.S - iot.F + 1);
    			int lca = min(tableE[iot.F][lg], tableE[iot.S - (1 << lg) + 1][lg]);
    			ans[idx] = query(r) - query(l - 1) - lca;
    		}
    	}
    	for (int i = 1; i <= Q; i++)
    		cout << ans[i] << '\n';
     
    }
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7384 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 3 ms 7500 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7512 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 4 ms 7516 KB Output is correct
13 Correct 3 ms 7508 KB Output is correct
14 Correct 3 ms 7508 KB Output is correct
15 Correct 3 ms 7508 KB Output is correct
16 Correct 3 ms 7508 KB Output is correct
17 Correct 3 ms 7508 KB Output is correct
18 Correct 3 ms 7508 KB Output is correct
19 Correct 4 ms 7508 KB Output is correct
20 Correct 4 ms 7512 KB Output is correct
21 Correct 3 ms 7516 KB Output is correct
22 Correct 4 ms 7512 KB Output is correct
23 Correct 4 ms 7512 KB Output is correct
24 Correct 4 ms 7512 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 4 ms 7508 KB Output is correct
27 Correct 3 ms 7376 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 3 ms 7508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7384 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 3 ms 7500 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7512 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 4 ms 7516 KB Output is correct
13 Correct 3 ms 7508 KB Output is correct
14 Correct 3 ms 7508 KB Output is correct
15 Correct 3 ms 7508 KB Output is correct
16 Correct 3 ms 7508 KB Output is correct
17 Correct 3 ms 7508 KB Output is correct
18 Correct 3 ms 7508 KB Output is correct
19 Correct 4 ms 7508 KB Output is correct
20 Correct 4 ms 7512 KB Output is correct
21 Correct 3 ms 7516 KB Output is correct
22 Correct 4 ms 7512 KB Output is correct
23 Correct 4 ms 7512 KB Output is correct
24 Correct 4 ms 7512 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 4 ms 7508 KB Output is correct
27 Correct 3 ms 7376 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 3 ms 7508 KB Output is correct
30 Correct 4 ms 8020 KB Output is correct
31 Correct 5 ms 8020 KB Output is correct
32 Correct 5 ms 8308 KB Output is correct
33 Correct 5 ms 8276 KB Output is correct
34 Correct 6 ms 8288 KB Output is correct
35 Correct 5 ms 8276 KB Output is correct
36 Correct 5 ms 8224 KB Output is correct
37 Correct 5 ms 8276 KB Output is correct
38 Correct 5 ms 8276 KB Output is correct
39 Correct 5 ms 8276 KB Output is correct
40 Correct 5 ms 8288 KB Output is correct
41 Correct 5 ms 8404 KB Output is correct
42 Correct 5 ms 8404 KB Output is correct
43 Correct 5 ms 8288 KB Output is correct
44 Correct 5 ms 8276 KB Output is correct
45 Correct 5 ms 8276 KB Output is correct
46 Correct 5 ms 8284 KB Output is correct
47 Correct 5 ms 8276 KB Output is correct
48 Correct 5 ms 8288 KB Output is correct
49 Correct 5 ms 8276 KB Output is correct
50 Correct 5 ms 8276 KB Output is correct
51 Correct 5 ms 8276 KB Output is correct
52 Correct 5 ms 8228 KB Output is correct
53 Correct 6 ms 8276 KB Output is correct
54 Correct 5 ms 8276 KB Output is correct
55 Correct 5 ms 8276 KB Output is correct
56 Correct 4 ms 7764 KB Output is correct
57 Correct 5 ms 7904 KB Output is correct
58 Correct 5 ms 8276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7776 KB Output is correct
4 Correct 74 ms 40848 KB Output is correct
5 Correct 66 ms 42668 KB Output is correct
6 Correct 78 ms 50068 KB Output is correct
7 Correct 104 ms 56624 KB Output is correct
8 Correct 102 ms 56620 KB Output is correct
9 Correct 109 ms 56656 KB Output is correct
10 Correct 102 ms 56680 KB Output is correct
11 Correct 105 ms 56552 KB Output is correct
12 Correct 87 ms 55896 KB Output is correct
13 Correct 87 ms 55908 KB Output is correct
14 Correct 86 ms 55944 KB Output is correct
15 Correct 58 ms 38732 KB Output is correct
16 Correct 108 ms 56340 KB Output is correct
17 Correct 59 ms 26420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 65 ms 29424 KB Output is correct
3 Correct 86 ms 36184 KB Output is correct
4 Correct 81 ms 34452 KB Output is correct
5 Correct 122 ms 47176 KB Output is correct
6 Correct 122 ms 47120 KB Output is correct
7 Correct 119 ms 47140 KB Output is correct
8 Correct 118 ms 47044 KB Output is correct
9 Correct 137 ms 47100 KB Output is correct
10 Correct 121 ms 47092 KB Output is correct
11 Correct 129 ms 47156 KB Output is correct
12 Correct 124 ms 47168 KB Output is correct
13 Correct 124 ms 47516 KB Output is correct
14 Correct 135 ms 48256 KB Output is correct
15 Correct 173 ms 50804 KB Output is correct
16 Correct 124 ms 47536 KB Output is correct
17 Correct 122 ms 47436 KB Output is correct
18 Correct 121 ms 47500 KB Output is correct
19 Correct 102 ms 46024 KB Output is correct
20 Correct 102 ms 45920 KB Output is correct
21 Correct 100 ms 45952 KB Output is correct
22 Correct 101 ms 45964 KB Output is correct
23 Correct 105 ms 45956 KB Output is correct
24 Correct 102 ms 45884 KB Output is correct
25 Correct 98 ms 45916 KB Output is correct
26 Correct 97 ms 45980 KB Output is correct
27 Correct 98 ms 45988 KB Output is correct
28 Correct 100 ms 46120 KB Output is correct
29 Correct 106 ms 45952 KB Output is correct
30 Correct 101 ms 46092 KB Output is correct
31 Correct 104 ms 46260 KB Output is correct
32 Correct 111 ms 46768 KB Output is correct
33 Correct 131 ms 47760 KB Output is correct
34 Correct 126 ms 49720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7764 KB Output is correct
4 Correct 132 ms 38104 KB Output is correct
5 Correct 135 ms 38884 KB Output is correct
6 Correct 171 ms 46412 KB Output is correct
7 Correct 178 ms 50068 KB Output is correct
8 Correct 185 ms 50008 KB Output is correct
9 Correct 185 ms 50064 KB Output is correct
10 Correct 187 ms 50096 KB Output is correct
11 Correct 185 ms 50068 KB Output is correct
12 Correct 190 ms 50016 KB Output is correct
13 Correct 202 ms 50120 KB Output is correct
14 Correct 62 ms 26316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7384 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 3 ms 7500 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7512 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 4 ms 7516 KB Output is correct
13 Correct 3 ms 7508 KB Output is correct
14 Correct 3 ms 7508 KB Output is correct
15 Correct 3 ms 7508 KB Output is correct
16 Correct 3 ms 7508 KB Output is correct
17 Correct 3 ms 7508 KB Output is correct
18 Correct 3 ms 7508 KB Output is correct
19 Correct 4 ms 7508 KB Output is correct
20 Correct 4 ms 7512 KB Output is correct
21 Correct 3 ms 7516 KB Output is correct
22 Correct 4 ms 7512 KB Output is correct
23 Correct 4 ms 7512 KB Output is correct
24 Correct 4 ms 7512 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 4 ms 7508 KB Output is correct
27 Correct 3 ms 7376 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 3 ms 7508 KB Output is correct
30 Correct 4 ms 8020 KB Output is correct
31 Correct 5 ms 8020 KB Output is correct
32 Correct 5 ms 8308 KB Output is correct
33 Correct 5 ms 8276 KB Output is correct
34 Correct 6 ms 8288 KB Output is correct
35 Correct 5 ms 8276 KB Output is correct
36 Correct 5 ms 8224 KB Output is correct
37 Correct 5 ms 8276 KB Output is correct
38 Correct 5 ms 8276 KB Output is correct
39 Correct 5 ms 8276 KB Output is correct
40 Correct 5 ms 8288 KB Output is correct
41 Correct 5 ms 8404 KB Output is correct
42 Correct 5 ms 8404 KB Output is correct
43 Correct 5 ms 8288 KB Output is correct
44 Correct 5 ms 8276 KB Output is correct
45 Correct 5 ms 8276 KB Output is correct
46 Correct 5 ms 8284 KB Output is correct
47 Correct 5 ms 8276 KB Output is correct
48 Correct 5 ms 8288 KB Output is correct
49 Correct 5 ms 8276 KB Output is correct
50 Correct 5 ms 8276 KB Output is correct
51 Correct 5 ms 8276 KB Output is correct
52 Correct 5 ms 8228 KB Output is correct
53 Correct 6 ms 8276 KB Output is correct
54 Correct 5 ms 8276 KB Output is correct
55 Correct 5 ms 8276 KB Output is correct
56 Correct 4 ms 7764 KB Output is correct
57 Correct 5 ms 7904 KB Output is correct
58 Correct 5 ms 8276 KB Output is correct
59 Correct 3 ms 7380 KB Output is correct
60 Correct 4 ms 7380 KB Output is correct
61 Correct 4 ms 7776 KB Output is correct
62 Correct 74 ms 40848 KB Output is correct
63 Correct 66 ms 42668 KB Output is correct
64 Correct 78 ms 50068 KB Output is correct
65 Correct 104 ms 56624 KB Output is correct
66 Correct 102 ms 56620 KB Output is correct
67 Correct 109 ms 56656 KB Output is correct
68 Correct 102 ms 56680 KB Output is correct
69 Correct 105 ms 56552 KB Output is correct
70 Correct 87 ms 55896 KB Output is correct
71 Correct 87 ms 55908 KB Output is correct
72 Correct 86 ms 55944 KB Output is correct
73 Correct 58 ms 38732 KB Output is correct
74 Correct 108 ms 56340 KB Output is correct
75 Correct 59 ms 26420 KB Output is correct
76 Correct 3 ms 7380 KB Output is correct
77 Correct 65 ms 29424 KB Output is correct
78 Correct 86 ms 36184 KB Output is correct
79 Correct 81 ms 34452 KB Output is correct
80 Correct 122 ms 47176 KB Output is correct
81 Correct 122 ms 47120 KB Output is correct
82 Correct 119 ms 47140 KB Output is correct
83 Correct 118 ms 47044 KB Output is correct
84 Correct 137 ms 47100 KB Output is correct
85 Correct 121 ms 47092 KB Output is correct
86 Correct 129 ms 47156 KB Output is correct
87 Correct 124 ms 47168 KB Output is correct
88 Correct 124 ms 47516 KB Output is correct
89 Correct 135 ms 48256 KB Output is correct
90 Correct 173 ms 50804 KB Output is correct
91 Correct 124 ms 47536 KB Output is correct
92 Correct 122 ms 47436 KB Output is correct
93 Correct 121 ms 47500 KB Output is correct
94 Correct 102 ms 46024 KB Output is correct
95 Correct 102 ms 45920 KB Output is correct
96 Correct 100 ms 45952 KB Output is correct
97 Correct 101 ms 45964 KB Output is correct
98 Correct 105 ms 45956 KB Output is correct
99 Correct 102 ms 45884 KB Output is correct
100 Correct 98 ms 45916 KB Output is correct
101 Correct 97 ms 45980 KB Output is correct
102 Correct 98 ms 45988 KB Output is correct
103 Correct 100 ms 46120 KB Output is correct
104 Correct 106 ms 45952 KB Output is correct
105 Correct 101 ms 46092 KB Output is correct
106 Correct 104 ms 46260 KB Output is correct
107 Correct 111 ms 46768 KB Output is correct
108 Correct 131 ms 47760 KB Output is correct
109 Correct 126 ms 49720 KB Output is correct
110 Correct 3 ms 7380 KB Output is correct
111 Correct 3 ms 7380 KB Output is correct
112 Correct 4 ms 7764 KB Output is correct
113 Correct 132 ms 38104 KB Output is correct
114 Correct 135 ms 38884 KB Output is correct
115 Correct 171 ms 46412 KB Output is correct
116 Correct 178 ms 50068 KB Output is correct
117 Correct 185 ms 50008 KB Output is correct
118 Correct 185 ms 50064 KB Output is correct
119 Correct 187 ms 50096 KB Output is correct
120 Correct 185 ms 50068 KB Output is correct
121 Correct 190 ms 50016 KB Output is correct
122 Correct 202 ms 50120 KB Output is correct
123 Correct 62 ms 26316 KB Output is correct
124 Correct 139 ms 47112 KB Output is correct
125 Correct 109 ms 41996 KB Output is correct
126 Correct 249 ms 50060 KB Output is correct
127 Correct 233 ms 50164 KB Output is correct
128 Correct 215 ms 50224 KB Output is correct
129 Correct 209 ms 50204 KB Output is correct
130 Correct 194 ms 50160 KB Output is correct
131 Correct 127 ms 55432 KB Output is correct
132 Correct 130 ms 56472 KB Output is correct
133 Correct 126 ms 52728 KB Output is correct
134 Correct 164 ms 49112 KB Output is correct
135 Correct 193 ms 49096 KB Output is correct
136 Correct 171 ms 49052 KB Output is correct
137 Correct 158 ms 51096 KB Output is correct
138 Correct 161 ms 51076 KB Output is correct
139 Correct 153 ms 51088 KB Output is correct
140 Correct 158 ms 51064 KB Output is correct
141 Correct 153 ms 51060 KB Output is correct
142 Correct 148 ms 51120 KB Output is correct
143 Correct 91 ms 31076 KB Output is correct
144 Correct 208 ms 49896 KB Output is correct