#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> adj[800005];
int num[800005];
struct SegTree {
vector<int> seg;
int MAX;
SegTree(int N) {
int i;
for(i=1;i<2*N;i*=2) {}
seg.resize(i);
MAX = i;
}
void Edit(int n, int a) {
n += MAX/2;
seg[n] += a;
n /= 2;
while(n) {
seg[n] = seg[2*n] + seg[2*n+1];
n /= 2;
}
}
int kth(int k, int n, int ns, int ne) {
if(ns+1==ne) return ns;
int mid = ns + ne >> 1;
if(k < seg[2*n]) return kth(k, 2*n, ns, mid);
else return kth(k-seg[2*n], 2*n+1, mid, ne);
}
};
int ans[800005];
int dfs_cnt = 0;
int A[1600005];
int lev[1600005];
vector<array<int, 2>> V;
void dfs(int c, int p, int d) {
if(p != -1) V.push_back({c, d});
int sz = adj[c].size();
int i, j;
if(p == -1) {
for(i=1;i<sz;i++) {
dfs(adj[c][i], c, d);
V.push_back({c, d});
}
dfs(adj[c][0], c, d);
V.push_back({c, d});
return;
}
int pt = -1;
for(i=0;i<sz;i++) {
if(adj[c][i] == p) pt = i;
}
assert(pt != -1);
for(i=pt+1;i<sz;i++) {
dfs(adj[c][i], c, d+(pt==0?0:1));
V.push_back({c, d+(pt==0?0:1)});
}
if(pt != 0) {
dfs(adj[c][0], c, d+1);
V.push_back({c, d+1});
for(i=1;i<pt;i++) {
dfs(adj[c][i], c, d);
V.push_back({c, d});
}
}
}
vector<int> B[800005];
vector<array<int, 2>> Query;
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, Q;
cin >> N >> Q;
int i, j;
for(i=0;i<N;i++) {
int k;
cin >> k;
for(j=0;j<k;j++) {
int c;
cin >> c;
adj[i].push_back(c-1);
}
num[i] = 1 % k;
}
dfs(0, -1, 1);
//cout << "dfs done\n";
int sz = V.size();
SegTree tree(sz+3);
int MAX = tree.MAX;
int ma = 0;
for(i=0;i<sz;i++) {
A[i] = V[i][0];
ma = max(ma, V[i][1]);
B[V[i][1]].push_back(i);
}
for(i=0;i<Q;i++) {
int a;
cin >> a;
Query.push_back({a-1, i});
}
sort(Query.begin(),Query.end());
int sum = 0;
int cnt = 0;
int qt = 0;
//cout << "Start Finding\n";
//for(i=0;i<sz;i++) cout << A[i] << ' ';
//cout << '\n';
for(i=1;i<=ma;i++) {
for(int n : B[i]) {
tree.Edit(n, 1);
//cout << n << ' ';
sum++;
}
//cout << '\n';
//cout<< i << ":\n";
while(qt<Query.size()&&Query[qt][0] < cnt + sum) {
//cout << qt << " : ";
int v = Query[qt][0] - cnt;
//cout << v << ' ';
//cout << tree.kth(v, 1, 0, MAX/2) << '\n';
ans[Query[qt][1]] = A[tree.kth(v, 1, 0, MAX/2)];
qt++;
}
//cout << '\n';
cnt += sum;
}
while(qt < Query.size()) {
int v = (Query[qt][0] - cnt) % sum;
ans[Query[qt][1]] = A[tree.kth(v, 1, 0, MAX/2)];
qt++;
}
for(i=0;i<Q;i++) cout << ans[i]+1 << '\n';
}
Compilation message
Main.cpp: In member function 'long long int SegTree::kth(long long int, long long int, long long int, long long int)':
Main.cpp:26:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = ns + ne >> 1;
| ~~~^~~~
Main.cpp: In function 'void dfs(long long int, long long int, long long int)':
Main.cpp:39:12: warning: unused variable 'j' [-Wunused-variable]
39 | int i, j;
| ^
Main.cpp: In function 'int main()':
Main.cpp:117:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | while(qt<Query.size()&&Query[qt][0] < cnt + sum) {
| ~~^~~~~~~~~~~~~
Main.cpp:128:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | while(qt < Query.size()) {
| ~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
37844 KB |
Output is correct |
2 |
Correct |
26 ms |
41292 KB |
Output is correct |
3 |
Correct |
118 ms |
71088 KB |
Output is correct |
4 |
Correct |
699 ms |
280604 KB |
Output is correct |
5 |
Correct |
916 ms |
293048 KB |
Output is correct |
6 |
Correct |
909 ms |
290388 KB |
Output is correct |
7 |
Correct |
307 ms |
88216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
37972 KB |
Output is correct |
2 |
Correct |
20 ms |
37952 KB |
Output is correct |
3 |
Correct |
19 ms |
38192 KB |
Output is correct |
4 |
Correct |
20 ms |
38356 KB |
Output is correct |
5 |
Correct |
21 ms |
38356 KB |
Output is correct |
6 |
Correct |
23 ms |
38356 KB |
Output is correct |
7 |
Correct |
20 ms |
38316 KB |
Output is correct |
8 |
Correct |
22 ms |
38356 KB |
Output is correct |
9 |
Correct |
22 ms |
38352 KB |
Output is correct |
10 |
Correct |
20 ms |
38400 KB |
Output is correct |
11 |
Correct |
19 ms |
38420 KB |
Output is correct |
12 |
Correct |
20 ms |
38612 KB |
Output is correct |
13 |
Correct |
20 ms |
38500 KB |
Output is correct |
14 |
Correct |
21 ms |
38480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
40012 KB |
Output is correct |
2 |
Correct |
50 ms |
48316 KB |
Output is correct |
3 |
Correct |
89 ms |
60248 KB |
Output is correct |
4 |
Correct |
74 ms |
55436 KB |
Output is correct |
5 |
Correct |
521 ms |
169720 KB |
Output is correct |
6 |
Correct |
537 ms |
166952 KB |
Output is correct |
7 |
Correct |
525 ms |
165880 KB |
Output is correct |
8 |
Correct |
547 ms |
174204 KB |
Output is correct |
9 |
Correct |
632 ms |
205968 KB |
Output is correct |
10 |
Correct |
660 ms |
218708 KB |
Output is correct |
11 |
Correct |
475 ms |
256064 KB |
Output is correct |
12 |
Correct |
505 ms |
253408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
37844 KB |
Output is correct |
2 |
Correct |
26 ms |
41292 KB |
Output is correct |
3 |
Correct |
118 ms |
71088 KB |
Output is correct |
4 |
Correct |
699 ms |
280604 KB |
Output is correct |
5 |
Correct |
916 ms |
293048 KB |
Output is correct |
6 |
Correct |
909 ms |
290388 KB |
Output is correct |
7 |
Correct |
307 ms |
88216 KB |
Output is correct |
8 |
Correct |
20 ms |
37972 KB |
Output is correct |
9 |
Correct |
20 ms |
37952 KB |
Output is correct |
10 |
Correct |
19 ms |
38192 KB |
Output is correct |
11 |
Correct |
20 ms |
38356 KB |
Output is correct |
12 |
Correct |
21 ms |
38356 KB |
Output is correct |
13 |
Correct |
23 ms |
38356 KB |
Output is correct |
14 |
Correct |
20 ms |
38316 KB |
Output is correct |
15 |
Correct |
22 ms |
38356 KB |
Output is correct |
16 |
Correct |
22 ms |
38352 KB |
Output is correct |
17 |
Correct |
20 ms |
38400 KB |
Output is correct |
18 |
Correct |
19 ms |
38420 KB |
Output is correct |
19 |
Correct |
20 ms |
38612 KB |
Output is correct |
20 |
Correct |
20 ms |
38500 KB |
Output is correct |
21 |
Correct |
21 ms |
38480 KB |
Output is correct |
22 |
Correct |
23 ms |
40012 KB |
Output is correct |
23 |
Correct |
50 ms |
48316 KB |
Output is correct |
24 |
Correct |
89 ms |
60248 KB |
Output is correct |
25 |
Correct |
74 ms |
55436 KB |
Output is correct |
26 |
Correct |
521 ms |
169720 KB |
Output is correct |
27 |
Correct |
537 ms |
166952 KB |
Output is correct |
28 |
Correct |
525 ms |
165880 KB |
Output is correct |
29 |
Correct |
547 ms |
174204 KB |
Output is correct |
30 |
Correct |
632 ms |
205968 KB |
Output is correct |
31 |
Correct |
660 ms |
218708 KB |
Output is correct |
32 |
Correct |
475 ms |
256064 KB |
Output is correct |
33 |
Correct |
505 ms |
253408 KB |
Output is correct |
34 |
Correct |
284 ms |
67220 KB |
Output is correct |
35 |
Correct |
327 ms |
76428 KB |
Output is correct |
36 |
Correct |
428 ms |
87692 KB |
Output is correct |
37 |
Correct |
802 ms |
202076 KB |
Output is correct |
38 |
Correct |
838 ms |
201708 KB |
Output is correct |
39 |
Correct |
927 ms |
202936 KB |
Output is correct |
40 |
Correct |
989 ms |
210248 KB |
Output is correct |
41 |
Correct |
1083 ms |
246712 KB |
Output is correct |
42 |
Correct |
1061 ms |
291376 KB |
Output is correct |
43 |
Correct |
834 ms |
293396 KB |
Output is correct |
44 |
Correct |
846 ms |
290688 KB |
Output is correct |
45 |
Correct |
892 ms |
249072 KB |
Output is correct |
46 |
Correct |
19 ms |
37844 KB |
Output is correct |