QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#858003 | #9676. Ancestors | qojszt | 0 | 949ms | 130472kb | C++14 | 5.5kb | 2025-01-16 12:05:45 | 2025-01-16 12:05:45 |
Judging History
answer
#include <bits/stdc++.h>
#define now (clock() * 1. / CLOCKS_PER_SEC)
using namespace std;
int n, m, f[100010], rt;
vector<int> ve[100010];
int dep[100010];
namespace ODT {
struct sec {int l, v;mutable int r;inline sec(int a, int b, int c) noexcept : l(a), r(b), v(c) {}};
set<sec> S;inline bool operator < (const sec &a, const sec &b) noexcept {return a.l < b.l;}
void init(){S.clear();S.emplace(1, n, 0);}
typedef set<sec>::iterator iter_t;
iter_t splt(int p){//return [p,\ast)
auto it = --S.upper_bound(sec{p, p, 0});if (p > it->r)return S.end();
if (it->l == p)return it;int r = it->r;it->r = p - 1;return S.emplace_hint(it, p, r, it->v);
}
template <typename _Func>
void assign(int l, int r, int v, _Func func){
iter_t R = splt(r + 1), L = splt(l);
for (auto it = R; it != L; )--it, func(it->r - it->l + 1, it->v);;//right with greater dep
S.erase(L, R);S.emplace(l, r, v);
}
}
namespace HLD {//Heavy-Light Decomposition
using ::ve;using ::n;using ::rt;using ::f;using ::dep;
int dfn[100010], sz[100010], wt[100010], tot, top[100010];
void _dfs1(int k){sz[k] = 1;for (int i : ve[k])dep[i] = dep[k] + 1, _dfs1(i), sz[i] > sz[wt[k]] && (wt[k] = i);}
void _dfs2(int k, int tp){dfn[k] = ++tot;top[k] = tp;if (wt[k])_dfs2(wt[k], tp);for (int i : ve[k])if (i ^ wt[k])_dfs2(i, i);}
void bd(){dep[rt] = 1;_dfs1(rt);_dfs2(rt, rt);}
using ODT::init;
template <typename _Func>
void assign(int u, _Func func){//assign node on list u to rt with value u
//from up to down, (l, v) : a section with length = l and value = v
int ct = 0;
for (int v = u; u; u = f[top[u]])ODT::assign(dfn[top[u]], dfn[u], v, func), ++ct;
assert(ct <= __lg(n) + 2);
func(n, n + 1);
}
}
vector<int> ptlst[100010];//dep
vector<pair<int, int> > lsmdf[100010];//lsmdf[x](p,v) : if = x, then let ls_p <- v
vector<tuple<int, int, int> > qr[100010];//(l,r,id)
namespace CDQ {
int M_tm;
struct mdf {int t, x, y, adv;};vector<mdf> md;
void addmdf(int x, int y, int adv){md.push_back({++M_tm, x, y, adv});}//(x,y) += adv
struct qry {int t, x, y, *dst, ml;};vector<qry> qr;
void addqry(int x, int y, int *dst, int ml){qr.push_back({++M_tm, x, y, dst, ml});}//*dst += ml * (cnt points in ([0,x],[0,y]) )
int n;
namespace bttr {
int vl[100010];
inline void add(int p, int v){for (++p; p <= n + 1; p += p & -p)vl[p] += v;}
inline int qry(int p){int ret = 0;for (++p; p; p -= p & -p)ret += vl[p];return ret;}
}
void solve(int _n){//x and y not exceed n
n = _n;typedef vector<mdf>::iterator iterM;typedef vector<qry>::iterator iterQ;
function<void(int, int, iterM, iterM, iterQ, iterQ)> sol = [&](int tml, int tmr, iterM mdl, iterM mdr, iterQ qrl, iterQ qrr){
if (tml == tmr || mdl == mdr || qrl == qrr)return;
int tmm = tml + tmr >> 1;
iterM mdm = find_if(mdl, mdr, [&tmm](const mdf &md){return md.t > tmm;});
iterQ qrm = find_if(qrl, qrr, [&tmm](const qry &qr){return qr.t > tmm;});
sol(tml, tmm, mdl, mdm, qrl, qrm);sol(tmm + 1, tmr, mdm, mdr, qrm, qrr);
sort(mdl, mdm, [&](const mdf &a, const mdf &b){return make_pair(a.x, a.y) < make_pair(b.x, b.y);});
sort(qrm, qrr, [&](const qry &a, const qry &b){return make_pair(a.x, a.y) < make_pair(b.x, b.y);});
iterQ i = qrm;iterM j = mdl;
while (i != qrr){
if (j != mdm && j->x <= i->x)bttr::add(j->y, j->adv), ++j;
else *(i->dst) += i->ml * bttr::qry(i->y), ++i;
}
for (; j != mdl; )--j, bttr::add(j->y, -j->adv);
};
sol(1, M_tm, md.begin(), md.end(), qr.begin(), qr.end());
}
}
int ls[100010];
int rs[1000010];
int main(){
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i)cin >> f[i], ve[f[i]].emplace_back(i);
rt = ve[0][0];HLD::bd();
//assert(*min_element(dep + 1, dep + n + 1));
//for (int i = 1; i <= n; ++i)clog << dep[i] << " "; clog << endl;
for (int i = 1; i <= n; ++i)ptlst[dep[i]].emplace_back(i);
int cnt = 0;//[[debug]]
for (int d = 1; d <= n; ++d)if (ptlst[d].size()){
HLD::init();
for (int u : ptlst[d]){
int sum = -1;//not include u self
HLD::assign(u, [&](int len, int val){
//ls_{sum+1 ~ sum+len,u}=val
lsmdf[sum + 1].emplace_back(u, val);
++cnt;
sum += len;
});
}
//assert(cnt <= n * __lg(n) * 4);
}
//if (cnt > n * __lg(n) * 8)cout << cnt << endl;//1000 54674?
//[[debug]]for (int i = 1; i <= n; ++i)for (auto [p, v] : lsmdf[i])clog << i << ":" << p << "," << v << endl;
for (int i = 1, l, r, x; i <= m; ++i){cin >> l >> r >> x;qr[x].emplace_back(l, r, i);}
//ls_i=0
for (int i = 1; i <= n; ++i)CDQ::addmdf(i, 0, 1);
for (int x = 1; x <= n; ++x){
for (auto [p, v] : lsmdf[x]){/*if (ls[p])*/CDQ::addmdf(p, ls[p], -1);CDQ::addmdf(p, ls[p] = v, 1);}//ls_p<-v
for (auto [l, r, id] : qr[x])CDQ::addqry(r, l - 1, rs + id, 1), CDQ::addqry(l - 1, l - 1, rs + id, -1);//\sum_{i=l}^r [ls_p < l]
}
CDQ::solve(n);
copy(rs + 1, rs + m + 1, ostream_iterator<int>(cout, "\n"));
clog << now << endl;
return 0;
}
/*
7 5
3 1 0 5 3 5 1
1 3 1
5 7 2
1 5 1
4 7 1
4 7 2
2
1
3
3
1
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 11
Accepted
time: 0ms
memory: 15408kb
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: 11
Accepted
time: 2ms
memory: 15928kb
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:
452 67 611 126 329 486 354 25 559 585 184 265 576 116 489 289 147 287 13 282 151 192 146 141 148 131 43 72 69 29 5 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 2 69 110 286 172 0 40 31 16 105 75 45 8 94 540 115 357 7 11 431 581 37 ...
result:
ok 1000 numbers
Test #3:
score: 0
Runtime Error
input:
1000 1000 594 766 788 546 408 364 152 525 963 359 339 746 747 58 628 60 144 646 222 863 418 1000 494 84 225 21 202 146 81 879 239 526 662 816 483 140 7 834 978 26 370 619 12 112 824 319 855 852 877 32 708 236 296 791 40 102 238 930 734 609 740 309 982 837 272 451 825 977 717 597 761 90 305 224 216 8...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Runtime Error
Test #30:
score: 17
Accepted
time: 286ms
memory: 49828kb
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:
12045 1321 12292 2444 32443 36534 38575 30505 16464 16648 47153 41144 23401 1762 18567 6831 26486 29716 8905 16295 38569 29990 22061 7638 3820 34754 17582 6466 12632 23384 11657 16493 24434 11503 3945 2205 37806 13036 29052 24114 39158 34702 37326 20815 11766 41253 44355 17807 23452 26717 43107 4133...
result:
ok 200000 numbers
Test #31:
score: 0
Runtime Error
input:
50000 200000 13611 7267 47234 19765 39458 30493 19935 34465 46369 11995 35207 5752 13718 47418 12913 13725 27694 47726 25253 30406 27587 25790 21091 12008 13534 8804 26484 34270 33810 49102 25288 37607 11863 23340 22747 40502 4461 24803 31317 25617 46077 6504 46299 34533 6829 17841 2812 35413 16394 ...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Runtime Error
Test #67:
score: 17
Accepted
time: 949ms
memory: 130472kb
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:
52956 18767 1319 13405 11021 455 50595 81481 6813 3640 58937 10991 70 4713 36 9517 39731 1166 67346 74637 2667 45182 4914 6774 1625 4198 52270 30435 60137 48654 29768 2815 6846 73091 21944 49693 9923 46795 29787 6866 2435 20773 2959 34666 4377 2428 4582 7527 38292 7253 3586 63817 28075 43828 20215 1...
result:
ok 1000000 numbers
Test #68:
score: 0
Runtime Error
input:
100000 1000000 82160 95864 48267 17482 19568 35077 14202 20440 4649 64145 148 2227 6969 39096 36508 20991 67700 90300 69215 57284 18492 9246 9629 7538 7845 30368 55600 48445 18542 41242 45052 25380 20894 91677 77040 73134 15572 21966 25343 14501 16227 23870 39207 50024 30787 11148 16884 63700 33205 ...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%