QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#863046#9676. AncestorsL_Hospital_#0 0ms10348kbC++142.9kb2025-01-19 12:16:002025-01-19 12:16:01

Judging History

你现在查看的是最新测评结果

  • [2025-01-19 12:16:01]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:10348kb
  • [2025-01-19 12:16:00]
  • 提交

answer

#include<bits/stdc++.h>
# define rep(i, j, k) for (int i = j; i <= k; ++i)
# define N 100005
# define B 1
using namespace std;

struct query
{
	int id, l, r, x, b;
	bool operator<(const query &x)const
	{
		return b == x.b ? r > x.r : b < x.b;
	}
} q[1000005];
int n, m, rr, p[N], hd[N], to[N], nxt[N], etimer;
int d[N], t1[N], t2[N], timer, st[N][20];
vector < int > num[N];
int le[N], re[N], lp[N], rp[N];
int rk[N], r;
int big[525], sml[100005];
int stk[100005];
int lpos, rpos;
int ans[1000005];
void addedge(int u, int v){to[++etimer] = v, nxt[etimer] = hd[u], hd[u] = etimer;}
void dfs(int u)
{
	d[u] = d[p[u]] + 1; t1[u] = ++timer; num[d[u]].push_back(u);
	st[u][0] = p[u]; rep(i, 1, 18) st[u][i] = st[st[u][i - 1]][i - 1];
	for (int i = hd[u]; i; i = nxt[i]) dfs(to[i]);
	t2[u] = timer;
}
bool up(int u, int v){return !u || t1[u] <= t1[v] && t2[u] >= t2[v];}
int lca(int u, int v)
{
	if (up(u, v)) return u; if (up(v, u)) return v;
	for (int i = 18; i >= 0; --i) if (!up(st[u][i], v)) u = st[u][i];
	return st[u][0];
}
void add(int x){++big[x >> 8], ++sml[x];}
void del(int x){--big[x >> 8], --sml[x];}
int query(int x){int ans = 0; rep(i, 0, (x >> 8) - 1) ans += big[i]; rep(i, x - (x & 255), x) ans += sml[i]; return ans;}
void jst(int u)
{
	del(min(lp[u], rp[u])); le[re[u]] = le[u], re[le[u]] = re[u], lp[re[u]] = rp[le[u]] = max(lp[u], rp[u]); stk[++r] = u;
}
void bk(int x)
{
	while (r > x)
	{
		const int u = stk[r];
		add(min(lp[u], rp[u])), le[re[u]] = re[le[u]] = u, lp[re[u]] = rp[u], rp[le[u]] = lp[u]; --r;
	}
}
signed main()
{
	freopen("a.in", "r", stdin);
	ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
	cin >> n >> m;
	rep(i, 1, n) {cin >> p[i]; if (p[i]) addedge(p[i], i); else rr = i;}
	dfs(rr);
	rep(i, 1, n) for (int j = 0; j < num[i].size(); ++j) rk[++r] = num[i][j];
	rk[0] = 0, rk[n + 1] = n + 1;
	rep(i, 0, n)
	{
		re[rk[i]] = rk[i + 1], le[rk[i + 1]] = rk[i];
		if (i == 0 || i == n) add(rp[rk[i]] = lp[rk[i + 1]] = i + (i == n));
		else if (d[rk[i]] != d[rk[i + 1]]) add(rp[rk[i]] = lp[rk[i + 1]] = d[rk[i + 1]]);
		else add(rp[rk[i]] = lp[rk[i + 1]] = d[rk[i]] - d[lca(rk[i], rk[i + 1])]);
	//	cerr << rk[i] << ' ' << rk[i + 1] << ' ' << rp[rk[i]] << endl;
	}
	rep(i, 1, m) cin >> q[i].l >> q[i].r >> q[i].x, q[i].id = i, q[i].b = (q[i].l - 1) / B + 1;
	sort(q + 1, q + m + 1);
	
	int pos = 1;
	r = 0; lpos = 1, rpos = n;
//	cerr << n << endl;
	rep(i, 1, n / B + 1)
	{
		while (lpos < (i - 1) * B + 1) jst(lpos++);
		int tmp = r;
		while (pos <= m && q[pos].b == i)
		{
			while (rpos > q[pos].r) jst(rpos--);
			int ttmp = r;
			while (lpos < q[pos].l) jst(lpos++);
			ans[q[pos].id] = q[pos].r - q[pos].l + 1 - query(q[pos].x);
			bk(ttmp); lpos = (i - 1) * B + 1, rpos = q[pos++].r;
		}
		bk(tmp); lpos = (i - 1) * B + 1, rpos = n;
	}
	rep(i, 1, m) cout << ans[i] << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 10348kb

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:

wrong answer Answer contains longer sequence [length = 5], but output contains 0 elements

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 0ms
memory: 10344kb

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:

wrong answer Answer contains longer sequence [length = 200000], but output contains 0 elements

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 0
Wrong Answer
time: 0ms
memory: 10184kb

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:

wrong answer Answer contains longer sequence [length = 1000000], but output contains 0 elements

Subtask #6:

score: 0
Skipped

Dependency #1:

0%