QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#876895 | #9676. Ancestors | Felix72 | 0 | 283ms | 60368kb | C++14 | 6.0kb | 2025-01-31 14:40:13 | 2025-01-31 14:40:13 |
Judging History
answer
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
using namespace std;
const int N = 100010, M = 1000010;
int n, m, rt, fa[N], res[N]; vector < int > tr[N];
struct Opt {int id, l, r, x, val;} op[M + N * 20]; int opn;
inline bool cmp_l(Opt u, Opt v)
{
if(u.l != v.l) return u.l < v.l;
else if(u.r != v.r) return u.r < v.r;
else if(u.x != v.x) return u.x < v.x;
else return u.id < v.id;
}
int dfn[N], dfid, seq[N * 2][21], sn, dep[N], lft[N];
vector < int > d_con[N];
inline void init(int now, int prt)
{
dfn[now] = ++dfid, seq[++sn][0] = now;
dep[now] = dep[prt] + 1; d_con[dep[now]].push_back(now);
for(int to : tr[now])
{
init(to, now);
seq[++sn][0] = now;
}
}
inline void init_LCA()
{
for(int i = 1; (1 << i) <= sn; ++i)
{
for(int j = 1; j + (1 << i) - 1 <= sn; ++j)
{
if(dep[seq[j][i - 1]] <= dep[seq[j + (1 << (i - 1))][i - 1]])
seq[j][i] = seq[j][i - 1];
else seq[j][i] = seq[j + (1 << (i - 1))][i - 1];
}
}
for(int i = sn; i >= 1; --i) lft[seq[i][0]] = i;
}
inline int LCA(int x, int y)
{
if(lft[x] > lft[y]) swap(x, y);
int k = __lg(lft[y] - lft[x] + 1);
if(dep[seq[lft[x]][k]] <= dep[seq[lft[y] - (1 << k) + 1][k]])
return seq[lft[x]][k];
else return seq[lft[y] - (1 << k) + 1][k];
}
namespace Get_opt
{
vector < int > virtr[N];
set < int > con[N];
int ps[N * 2], psn, lst_l[N], lst_d[N], siz[N];
struct Table
{
int state[N]; queue < int > q; bool vis[N];
inline void refresh(int p) {if(!vis[p]) {vis[p] = true, q.push(p);}}
inline void modify(int p, int num)
{refresh(p); state[p] = num;}
inline int query(int p) {return state[p];}
inline void clear() {while(!q.empty()) state[q.front()] = vis[q.front()] = 0, q.pop();}
}; Table tb;
inline bool cmp(int x, int y) {return dfn[x] < dfn[y];}
inline void get(int x, int y, int d)
{
if(lst_l[y] != -1)
{
op[++opn] = {0, lst_l[y] + 1, y, d, -1};
op[++opn] = {0, y + 1, y, d, 1};
}
op[++opn] = {0, x + 1, y, d, 1};
op[++opn] = {0, y + 1, y, d, -1};
lst_l[y] = x, lst_d[y] = d;
}
inline void tree_comb(int now, int D)
{
con[now].clear();
int son = 0; siz[now] = (virtr[now].size() == 0);
for(int to : virtr[now])
{
tree_comb(to, D); siz[now] += siz[to];
if(!son || siz[to] > siz[son]) son = to;
}
if(!son)
{
get(0, now, 1);
con[now].insert(now);
}
else
{
vector < int > chk;
con[now].swap(con[son]);
for(int to : virtr[now])
{
if(to == son) continue;
for(int x : con[to])
{
tb.modify(x, to); chk.push_back(x);
con[now].insert(x);
}
}
for(int x : chk)
{
auto p = con[now].find(x);
if(p != con[now].begin())
{
int pr = *prev(p);
if(tb.query(pr) != tb.query(x))
get(pr, x, D - dep[now]);
}
if(next(p) != con[now].end())
{
int nx = *next(p);
if(tb.query(nx) == 0)
get(x, nx, D - dep[now]);
}
}
tb.clear();
}
}
inline void build_tree(int D)
{
psn = 0;
for(int x : d_con[D]) ps[++psn] = x;
sort(ps + 1, ps + psn + 1, cmp);
for(int i = 1, s_psn = psn; i < s_psn; ++i)
ps[++psn] = LCA(ps[i], ps[i + 1]);
sort(ps + 1, ps + psn + 1, cmp);
psn = unique(ps + 1, ps + psn + 1) - ps - 1;
for(int i = 1; i <= psn; ++i)
{
virtr[ps[i]].clear();
lst_l[ps[i]] = lst_d[ps[i]] = -1;
}
stack < int > sta;
for(int i = 1; i <= psn; ++i)
{
while(!sta.empty() && LCA(sta.top(), ps[i]) != sta.top()) sta.pop();
if(!sta.empty()) virtr[sta.top()].push_back(ps[i]);
sta.push(ps[i]);
}
// cerr << "build tree : " << D << '\n';
// for(int i = 1; i <= psn; ++i)
// {
// cerr << ps[i] << " : ";
// for(int x : virtr[ps[i]])
// cerr << x << " ";
// cerr << '\n';
// }
}
inline void work(int D)
{
build_tree(D);
tree_comb(ps[1], D);
for(int x : d_con[D])
{
if(lst_l[x] != -1)
{
op[++opn] = {0, lst_l[x] + 1, x, D, -1};
op[++opn] = {0, x + 1, x, D, 1};
}
}
}
}
namespace Cdq
{
struct BIT
{
int c[N];
inline void add(int p, int num)
{for(int i = p; i <= n; i += lowbit(i)) c[i] += num;}
inline int query(int p)
{
int res = 0;
for(int i = p; i; i -= lowbit(i)) res += c[i];
return res;
}
}; BIT bit;
inline void divide(int l, int r)
{
if(l == r) return ;
int mid = (l + r) >> 1;
divide(l, mid), divide(mid + 1, r);
// cerr << "divide " << l << " " << r << " " << mid << " +++++++++++++++++++++++++\n";
// for(int i = l; i <= r; ++i) cerr << "cdqop " << op[i].id << " " << op[i].l << " " << op[i].r << " " << op[i].x << " " << op[i].val << '\n';
// for(int i = 1; i <= 3; ++i) cerr << bit.query(i) << " "; cerr << '\n';
int pl = l, pr = mid + 1;
vector < Opt > sve;
while(pl <= mid || pr <= r)
{
if(pr > r || (pl <= mid && op[pl].r <= op[pr].r && op[pl].x <= op[pr].x))
{
if(op[pl].id == 0) bit.add(op[pl].x, op[pl].val);
sve.push_back(op[pl]); ++pl;
}
else
{
if(op[pr].id != 0) op[pr].val += bit.query(op[pr].x);
sve.push_back(op[pr]); ++pr;
}
}
for(int i = l; i <= mid; ++i) if(op[i].id == 0) bit.add(op[i].x, -op[i].val);
for(int i = l; i <= r; ++i) op[i] = sve[i - l];
}
inline void work()
{
sort(op + 1, op + opn + 1, cmp_l);
divide(1, opn);
}
}
int main()
{
// freopen("text.in", "r", stdin);
// freopen("prog.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; ++i)
{
cin >> fa[i];
if(!fa[i]) rt = i;
else tr[fa[i]].push_back(i);
}
for(int i = 1; i <= m; ++i)
op[++opn].id = i, cin >> op[i].l >> op[i].r >> op[i].x;
init(rt, 0), init_LCA();
for(int i = 1; d_con[i].size(); ++i) Get_opt::work(i);
// for(int i = 1; i <= opn; ++i) cerr << "op " << op[i].id << " " << op[i].l << " " << op[i].r << " " << op[i].x << " " << op[i].val << '\n';
Cdq::work();
for(int i = 1; i <= opn; ++i) if(op[i].id) res[op[i].id] = op[i].val;
for(int i = 1; i <= m; ++i) cout << res[i] << '\n';
return 0;
}
/*
3 1
0 1 1
2 3 1
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 2ms
memory: 20168kb
input:
7 5 3 1 0 5 3 5 1 1 3 1 5 7 2 1 5 1 4 7 1 4 7 2
output:
2 1 3 3 1
result:
ok 5 number(s): "2 1 3 3 1"
Test #2:
score: 0
Wrong Answer
time: 3ms
memory: 19492kb
input:
1000 1000 686 337 192 336 405 0 108 485 350 762 258 780 179 939 25 657 571 662 119 786 604 224 935 494 685 575 369 178 249 740 954 204 598 592 68 771 498 86 55 38 298 704 239 292 993 286 16 813 719 187 14 476 792 49 944 52 227 720 310 470 900 243 663 950 627 300 728 189 45 610 673 548 873 95 48 841 ...
output:
448 91 601 143 337 479 356 73 546 567 137 229 549 22 465 260 86 261 -79 258 87 150 104 99 120 102 -140 18 23 -5 -281 15 57 10 9 16 87 162 19 217 232 24 178 334 103 139 293 400 299 351 529 632 592 296 640 678 715 708 52 465 322 731 -82 130 422 424 558 -101 -9 479 45 53 527 123 -88 167 623 137 384 107...
result:
wrong answer 1st numbers differ - expected: '452', found: '448'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 283ms
memory: 60368kb
input:
50000 200000 42574 43129 47328 17982 40521 6668 12729 32377 201 11940 8599 11734 18349 41045 26854 22540 9897 33419 7463 1243 47272 27135 49050 49111 22435 42539 39924 20272 5843 9308 45963 3283 31185 13692 38952 20583 15885 24802 4773 953 49907 28689 36942 23550 19449 8970 33340 31665 5407 46023 18...
output:
11962 1227 12134 2227 32215 36272 38315 30158 15935 16082 46921 40880 22869 738 17858 5796 25886 29166 7676 15226 38204 29326 21148 6177 2097 34273 16369 4671 11081 22306 9944 15015 23340 9570 1330 -602 37307 10910 27993 22684 38742 34034 36799 19098 9267 40912 44123 15747 21850 25387 42860 41040 32...
result:
wrong answer 1st numbers differ - expected: '12045', found: '11962'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Runtime Error
Test #67:
score: 0
Runtime Error
input:
100000 1000000 6457 23693 90928 23592 90440 75018 16865 3342 83718 16731 95103 31510 38719 27886 29093 41955 6596 46409 51839 10527 91993 61074 14405 34833 53674 42363 11490 43757 46191 6058 59164 96938 57858 40178 97523 84164 21582 72243 11267 47368 97058 6637 95208 60092 53943 16441 28363 64965 52...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%