This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define endl '\n'
#define all(x) begin(x), end(x)
struct node {
int l, r;
node *lft, *rht;
int tot;
node(int tl, int tr): l(tl), r(tr), tot(0) {
if (l + 1 == r) {
lft = rht = NULL;
return;
}
lft = new node(l, (l + r) / 2);
rht = new node((l + r) / 2, r);
}
void add(int p) {
if (p < l || p >= r) {
return;
}
tot++;
if (l + 1 == r) return;
lft->add(p); rht->add(p);
}
int fnd(int x) {
if (l + 1 == r) return l;
if (lft->tot >= x) return lft->fnd(x);
return rht->fnd(x - lft->tot);
}
}* root;
int n, q;
int a, b, tb;
vector<int> grph[800069];
int lgc;
vector<int> lg[800069];
vector<pair<int,int>> vctr, qrys;
int ans[800069];
void dfs(int x, int rnd, int lst = -1) {
for (auto i : grph[x]) {
if (i == lst) {
rnd++; continue;
}
vctr.push_back({i, rnd});
dfs(i, rnd, x);
vctr.push_back({x, rnd});
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin>>n>>q;
for (int i = 0; i < n; i++) {
cin>>a; cin>>tb; tb--;
for (int j = 1; j < a; j++) {
cin>>b; b--;
grph[i].push_back(b);
}
grph[i].push_back(tb);
}
dfs(0, 0);
root = new node(0, vctr.size());
for (int i = 0; i < vctr.size(); i++) {
vctr[i].f++;
lg[vctr[i].s].push_back(i);
}
for (int i = 0; i < q; i++) {
cin>>a;
qrys.push_back({a, i});
}
sort(all(qrys));
int cur = 0;
for (auto tmp : qrys) {
tie(a, b) = tmp;
while (a - cur > root->tot && lgc < 800069) {
cur += root->tot;
for (auto i : lg[lgc]) {
root->add(i);
}
lgc++;
}
a -= cur;
a %= root->tot;
if (!a) a = root->tot;
ans[b] = vctr[root->fnd(a)].f;
}
for (int i = 0; i < q; i++) {
cout<<ans[i]<<endl;
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:65:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 0; i < vctr.size(); i++) {
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |