#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) % (2 * n - 2) + 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 |
20 ms |
120656 KB |
Output is correct |
2 |
Correct |
29 ms |
123140 KB |
Output is correct |
3 |
Correct |
138 ms |
149688 KB |
Output is correct |
4 |
Correct |
765 ms |
332972 KB |
Output is correct |
5 |
Correct |
1293 ms |
354940 KB |
Output is correct |
6 |
Correct |
1292 ms |
348320 KB |
Output is correct |
7 |
Correct |
327 ms |
171556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
120656 KB |
Output is correct |
2 |
Correct |
22 ms |
120660 KB |
Output is correct |
3 |
Correct |
23 ms |
121080 KB |
Output is correct |
4 |
Correct |
23 ms |
120916 KB |
Output is correct |
5 |
Correct |
23 ms |
120936 KB |
Output is correct |
6 |
Correct |
23 ms |
120916 KB |
Output is correct |
7 |
Correct |
23 ms |
120916 KB |
Output is correct |
8 |
Correct |
22 ms |
120916 KB |
Output is correct |
9 |
Correct |
24 ms |
120916 KB |
Output is correct |
10 |
Correct |
24 ms |
121108 KB |
Output is correct |
11 |
Correct |
24 ms |
121132 KB |
Output is correct |
12 |
Correct |
24 ms |
121172 KB |
Output is correct |
13 |
Correct |
23 ms |
121172 KB |
Output is correct |
14 |
Correct |
26 ms |
121120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
122052 KB |
Output is correct |
2 |
Correct |
54 ms |
128444 KB |
Output is correct |
3 |
Correct |
97 ms |
138168 KB |
Output is correct |
4 |
Correct |
79 ms |
133048 KB |
Output is correct |
5 |
Correct |
566 ms |
234084 KB |
Output is correct |
6 |
Correct |
642 ms |
232100 KB |
Output is correct |
7 |
Correct |
627 ms |
230344 KB |
Output is correct |
8 |
Correct |
658 ms |
238984 KB |
Output is correct |
9 |
Correct |
818 ms |
274880 KB |
Output is correct |
10 |
Correct |
1035 ms |
293924 KB |
Output is correct |
11 |
Correct |
537 ms |
335484 KB |
Output is correct |
12 |
Correct |
528 ms |
330848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
120656 KB |
Output is correct |
2 |
Correct |
29 ms |
123140 KB |
Output is correct |
3 |
Correct |
138 ms |
149688 KB |
Output is correct |
4 |
Correct |
765 ms |
332972 KB |
Output is correct |
5 |
Correct |
1293 ms |
354940 KB |
Output is correct |
6 |
Correct |
1292 ms |
348320 KB |
Output is correct |
7 |
Correct |
327 ms |
171556 KB |
Output is correct |
8 |
Correct |
23 ms |
120656 KB |
Output is correct |
9 |
Correct |
22 ms |
120660 KB |
Output is correct |
10 |
Correct |
23 ms |
121080 KB |
Output is correct |
11 |
Correct |
23 ms |
120916 KB |
Output is correct |
12 |
Correct |
23 ms |
120936 KB |
Output is correct |
13 |
Correct |
23 ms |
120916 KB |
Output is correct |
14 |
Correct |
23 ms |
120916 KB |
Output is correct |
15 |
Correct |
22 ms |
120916 KB |
Output is correct |
16 |
Correct |
24 ms |
120916 KB |
Output is correct |
17 |
Correct |
24 ms |
121108 KB |
Output is correct |
18 |
Correct |
24 ms |
121132 KB |
Output is correct |
19 |
Correct |
24 ms |
121172 KB |
Output is correct |
20 |
Correct |
23 ms |
121172 KB |
Output is correct |
21 |
Correct |
26 ms |
121120 KB |
Output is correct |
22 |
Correct |
31 ms |
122052 KB |
Output is correct |
23 |
Correct |
54 ms |
128444 KB |
Output is correct |
24 |
Correct |
97 ms |
138168 KB |
Output is correct |
25 |
Correct |
79 ms |
133048 KB |
Output is correct |
26 |
Correct |
566 ms |
234084 KB |
Output is correct |
27 |
Correct |
642 ms |
232100 KB |
Output is correct |
28 |
Correct |
627 ms |
230344 KB |
Output is correct |
29 |
Correct |
658 ms |
238984 KB |
Output is correct |
30 |
Correct |
818 ms |
274880 KB |
Output is correct |
31 |
Correct |
1035 ms |
293924 KB |
Output is correct |
32 |
Correct |
537 ms |
335484 KB |
Output is correct |
33 |
Correct |
528 ms |
330848 KB |
Output is correct |
34 |
Correct |
286 ms |
155592 KB |
Output is correct |
35 |
Correct |
367 ms |
166336 KB |
Output is correct |
36 |
Correct |
574 ms |
180412 KB |
Output is correct |
37 |
Correct |
1145 ms |
260948 KB |
Output is correct |
38 |
Correct |
1059 ms |
262764 KB |
Output is correct |
39 |
Correct |
970 ms |
260388 KB |
Output is correct |
40 |
Correct |
1201 ms |
270244 KB |
Output is correct |
41 |
Correct |
1588 ms |
316196 KB |
Output is correct |
42 |
Correct |
1765 ms |
370464 KB |
Output is correct |
43 |
Correct |
1198 ms |
375612 KB |
Output is correct |
44 |
Correct |
1089 ms |
370340 KB |
Output is correct |
45 |
Correct |
1281 ms |
322724 KB |
Output is correct |
46 |
Correct |
23 ms |
120652 KB |
Output is correct |