QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#859503 | #9676. Ancestors | lfxxx | 0 | 263ms | 39872kb | C++17 | 3.3kb | 2025-01-17 19:58:38 | 2025-01-17 19:58:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
bool be;
constexpr int N = 1e5 + 5, Q = 1e6 + 5;
int n, m, b, rt, fa[N], dfn[N], dep[N], cnt, st[N][17], p[N], in[N], pre[N], nxt[N], ans[Q];
vector<int>e[N];
struct Query {
int l, r, x, id;
}q[Q];
struct ds {
int a[N], sum[350], in[N], b;
void init()
{
b = sqrt(n);
for (int i = 1; i <= n; ++i) in[i] = (i + b - 1) / b;
}
void add(int x, int v)
{
a[x] += v;
sum[in[x]] += v;
}
int query(int x)
{
int ans = 0;
for (int i = 1; i < in[x]; ++i) ans += sum[i];
for (int i = (in[x] - 1) * b + 1; i <= x; ++i) ans += a[i];
return ans;
}
}d;
void dfs(int u)
{
dfn[u] = ++cnt;
st[cnt][0] = fa[u];
dep[u] = dep[fa[u]] + 1;
for (int v : e[u]) {
dfs(v);
}
}
int get(int u, int v)
{
return dfn[u] < dfn[v] ? u : v;
}
int LCA(int u, int v)
{
if (u == v) return u;
if (dfn[u] > dfn[v]) swap(u, v);
int k = __lg(dfn[v] - dfn[u]);
return get(st[dfn[u] + 1][k], st[dfn[v] - (1 << k) + 1][k]);
}
void work(int x, int y, int z)
{
if (y == 0) return;
if (x == 0 || dep[x] != dep[y]) d.add(dep[y], z);
else d.add(dep[x] - dep[LCA(x, y)], z);
}
void del(int x)
{
work(pre[x], x, -1);
work(x, nxt[x], -1);
work(pre[x], nxt[x], 1);
pre[nxt[x]] = pre[x];
nxt[pre[x]] = nxt[x];
}
void add(int x)
{
work(pre[x], x, 1);
work(x, nxt[x], 1);
work(pre[x], nxt[x], -1);
pre[nxt[x]] = x;
nxt[pre[x]] = x;
}
bool en;
int main()
{
#ifdef IAKIOI
cerr << (&be - &en) / 1024.0 / 1024 << " MB\n----------------------------" << endl;
freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> fa[i];
if (fa[i] == 0) rt = i;
else e[fa[i]].emplace_back(i);
}
dfs(rt);
for (int j = 1; j <= __lg(n); ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
st[i][j] = get(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
d.init();
b = max(1, (int)(n / sqrt(m)));
for (int i = 1; i <= n; ++i) {
in[i] = (i + b - 1) / b;
}
iota(p + 1, p + 1 + n, 1);
sort(p + 1, p + 1 + n, [](int x, int y) {
if (dep[x] != dep[y]) return dep[x] < dep[y];
return dfn[x] < dfn[y];
});
for (int i = 1; i <= n; ++i) {
pre[p[i]] = p[i - 1];
nxt[p[i]] = p[i + 1];
}
for (int i = 1; i <= m; ++i) {
cin >> q[i].l >> q[i].r >> q[i].x;
q[i].id = i;
}
sort(q + 1, q + 1 + m, [](Query a, Query b) {
if (in[a.l] != in[b.l]) return a.l < b.l;
return a.r > b.r;
});
int l = 0, r = -1;
for (int i = 1; i <= m; ++i) {
if (in[l] != in[q[i].l]) {
while (r >= l) del(r--);
for (int j = l; j <= n; ++j) pre[j] = nxt[j] = 0;
l = (in[q[i].l] - 1) * b + 1;
r = n;
int lst = 0;
for (int j = 1; j <= n; ++j) {
if (l <= p[j] && p[j] <= r) {
work(lst, p[j], 1);
pre[p[j]] = lst;
nxt[lst] = p[j];
lst = p[j];
}
}
}
// while (q[i].r < r) del(r--);
// int pl = l;
// while (q[i].l > l) del(l++);
// ans[q[i].id] = r - l + 1 - d.query(q[i].x);
// while (pl < l) add(--l);
}
for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 14068kb
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:
0 0 0 0 0
result:
wrong answer 1st numbers differ - expected: '2', found: '0'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 263ms
memory: 22612kb
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:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 1st numbers differ - expected: '12045', found: '0'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 0
Wrong Answer
time: 244ms
memory: 39872kb
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:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 1st numbers differ - expected: '52956', found: '0'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%