#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 1e6 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 450;
ll n,q,m = 0;
vector<ll>adj[N], row[N], g[N], pos[N];
vector<pll>t[N];
ll idx[N], ps[N], res[N];
vector<ll>topo;
void dfs(ll u, ll par){
m = max(m, idx[u]);
ll pos = -1;
for(int i = 0; i < adj[u].size();i++) if(adj[u][i] == par) pos = i;
for(int i = 0; i < adj[u].size();i++){
ll v = adj[u][i];
if(i < pos) idx[v] = idx[u];
else if(i > pos) idx[v] = idx[u] + 1;
}
for(auto v : adj[u]) if(v != par) dfs(v, u);
for(int i = pos + 1; i < adj[u].size();i++) g[u].pb(adj[u][i]);
for(int i = 0; i < pos;i++) g[u].pb(adj[u][i]);
}
void dfs_topo(ll u){
for(auto v : g[u]) {
topo.pb(v);
row[idx[v]].pb((ll)topo.size() - 1);
dfs_topo(v);
topo.pb(u);
row[idx[v]].pb((ll)topo.size() - 1);
}
}
struct fenwick_tree{
ll n;
vector<ll>ft;
void init(ll _n){
n = _n;
ft.resize(n + 10);
}
void update(ll idx, ll val){
while(idx <= n) ft[idx] = ft[idx] + val, idx += (idx & (-idx));
}
ll get(ll idx){
ll res = 0;
while(idx > 0) res = res + ft[idx], idx -= (idx & (-idx));
return res;
}
} ft;
void to_thic_cau(){
cin >> n >> q;
for(int i = 1; i <= n;i++){
ll k; cin >> k;
vector<ll>tmp;
for(int j = 1; j <= k;j++){
ll x; cin >> x;
tmp.pb(x);
}
for(int j = 1; j < tmp.size();j++) adj[i].pb(tmp[j]);
adj[i].pb(tmp[0]);
}
idx[1] = 1;
topo.pb(0);
dfs(1, 0);
dfs_topo(1);
ll tot = 0;
for(int i = 1; i <= m;i++){
tot += (ll)row[i].size();
ps[i] = ps[i-1] + tot;
}
for(int i = 1; i <= q;i++){
ll k; cin >> k;
ll j = lower_bound(ps + 1, ps + m + 1, k) - ps;
if(j > m){
res[i] = topo[(k - ps[m] - 1) % ((ll)topo.size() - 1) + 1];
}
else t[j].pb({k - ps[j-1], i});
}
ft.init((ll)topo.size());
for(int i = 1; i <= m;i++){
for(auto x : row[i]) ft.update(x, 1);
for(auto x : t[i]){
ll val = x.F, idx = x.S;
ll l = 1, r = (ll)topo.size() - 1, pos = -1;
while(l <= r){
ll mid = (l + r) / 2;
if(ft.get(mid) >= val) pos = mid, r = mid - 1;
else l = mid + 1;
}
res[idx] = topo[pos];
}
}
for(int i = 1; i <= q;i++) cout << res[i] << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
Compilation message
Main.cpp: In function 'void dfs(long long int, long long int)':
Main.cpp:23:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i = 0; i < adj[u].size();i++) if(adj[u][i] == par) pos = i;
| ~~^~~~~~~~~~~~~~~
Main.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < adj[u].size();i++){
| ~~^~~~~~~~~~~~~~~
Main.cpp:30:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = pos + 1; i < adj[u].size();i++) g[u].pb(adj[u][i]);
| ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'void to_thic_cau()':
Main.cpp:67:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j = 1; j < tmp.size();j++) adj[i].pb(tmp[j]);
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
120656 KB |
Output is correct |
2 |
Correct |
30 ms |
123132 KB |
Output is correct |
3 |
Correct |
137 ms |
149688 KB |
Output is correct |
4 |
Correct |
736 ms |
333448 KB |
Output is correct |
5 |
Correct |
1332 ms |
354936 KB |
Output is correct |
6 |
Correct |
1183 ms |
348232 KB |
Output is correct |
7 |
Correct |
304 ms |
171448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
120656 KB |
Output is correct |
2 |
Correct |
20 ms |
120772 KB |
Output is correct |
3 |
Correct |
20 ms |
120912 KB |
Output is correct |
4 |
Correct |
25 ms |
120912 KB |
Output is correct |
5 |
Correct |
23 ms |
120912 KB |
Output is correct |
6 |
Correct |
24 ms |
121000 KB |
Output is correct |
7 |
Correct |
24 ms |
120912 KB |
Output is correct |
8 |
Correct |
24 ms |
120912 KB |
Output is correct |
9 |
Correct |
23 ms |
121044 KB |
Output is correct |
10 |
Correct |
24 ms |
120912 KB |
Output is correct |
11 |
Correct |
26 ms |
120912 KB |
Output is correct |
12 |
Correct |
25 ms |
121052 KB |
Output is correct |
13 |
Correct |
23 ms |
121180 KB |
Output is correct |
14 |
Correct |
25 ms |
121168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
122184 KB |
Output is correct |
2 |
Correct |
54 ms |
128360 KB |
Output is correct |
3 |
Correct |
91 ms |
138132 KB |
Output is correct |
4 |
Correct |
77 ms |
133052 KB |
Output is correct |
5 |
Correct |
599 ms |
221604 KB |
Output is correct |
6 |
Correct |
637 ms |
219180 KB |
Output is correct |
7 |
Correct |
670 ms |
217940 KB |
Output is correct |
8 |
Correct |
676 ms |
226724 KB |
Output is correct |
9 |
Correct |
852 ms |
262508 KB |
Output is correct |
10 |
Correct |
914 ms |
281716 KB |
Output is correct |
11 |
Correct |
488 ms |
323232 KB |
Output is correct |
12 |
Correct |
460 ms |
318480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
120656 KB |
Output is correct |
2 |
Correct |
30 ms |
123132 KB |
Output is correct |
3 |
Correct |
137 ms |
149688 KB |
Output is correct |
4 |
Correct |
736 ms |
333448 KB |
Output is correct |
5 |
Correct |
1332 ms |
354936 KB |
Output is correct |
6 |
Correct |
1183 ms |
348232 KB |
Output is correct |
7 |
Correct |
304 ms |
171448 KB |
Output is correct |
8 |
Correct |
20 ms |
120656 KB |
Output is correct |
9 |
Correct |
20 ms |
120772 KB |
Output is correct |
10 |
Correct |
20 ms |
120912 KB |
Output is correct |
11 |
Correct |
25 ms |
120912 KB |
Output is correct |
12 |
Correct |
23 ms |
120912 KB |
Output is correct |
13 |
Correct |
24 ms |
121000 KB |
Output is correct |
14 |
Correct |
24 ms |
120912 KB |
Output is correct |
15 |
Correct |
24 ms |
120912 KB |
Output is correct |
16 |
Correct |
23 ms |
121044 KB |
Output is correct |
17 |
Correct |
24 ms |
120912 KB |
Output is correct |
18 |
Correct |
26 ms |
120912 KB |
Output is correct |
19 |
Correct |
25 ms |
121052 KB |
Output is correct |
20 |
Correct |
23 ms |
121180 KB |
Output is correct |
21 |
Correct |
25 ms |
121168 KB |
Output is correct |
22 |
Correct |
29 ms |
122184 KB |
Output is correct |
23 |
Correct |
54 ms |
128360 KB |
Output is correct |
24 |
Correct |
91 ms |
138132 KB |
Output is correct |
25 |
Correct |
77 ms |
133052 KB |
Output is correct |
26 |
Correct |
599 ms |
221604 KB |
Output is correct |
27 |
Correct |
637 ms |
219180 KB |
Output is correct |
28 |
Correct |
670 ms |
217940 KB |
Output is correct |
29 |
Correct |
676 ms |
226724 KB |
Output is correct |
30 |
Correct |
852 ms |
262508 KB |
Output is correct |
31 |
Correct |
914 ms |
281716 KB |
Output is correct |
32 |
Correct |
488 ms |
323232 KB |
Output is correct |
33 |
Correct |
460 ms |
318480 KB |
Output is correct |
34 |
Correct |
264 ms |
149120 KB |
Output is correct |
35 |
Correct |
332 ms |
158916 KB |
Output is correct |
36 |
Correct |
492 ms |
170424 KB |
Output is correct |
37 |
Correct |
1028 ms |
241644 KB |
Output is correct |
38 |
Correct |
1191 ms |
243404 KB |
Output is correct |
39 |
Correct |
1067 ms |
240032 KB |
Output is correct |
40 |
Correct |
1147 ms |
248748 KB |
Output is correct |
41 |
Correct |
1518 ms |
294128 KB |
Output is correct |
42 |
Correct |
1519 ms |
348324 KB |
Output is correct |
43 |
Correct |
1144 ms |
354088 KB |
Output is correct |
44 |
Correct |
1117 ms |
348688 KB |
Output is correct |
45 |
Correct |
1437 ms |
304152 KB |
Output is correct |
46 |
Correct |
23 ms |
120656 KB |
Output is correct |