QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#857976 | #9676. Ancestors | qojszt | 0 | 0ms | 0kb | C++14 | 5.4kb | 2025-01-16 11:37:18 | 2025-01-16 11:37:19 |
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
for (int v = u; u; u = f[top[u]])ODT::assign(dfn[top[u]], dfn[u], v, func);
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);
}
clog << cnt << endl;
//[[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])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
*/
詳細信息
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
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:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Dangerous Syscalls
Test #30:
score: 0
Dangerous Syscalls
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:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Dangerous Syscalls
Test #67:
score: 0
Dangerous Syscalls
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%