QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#859327#9676. Ancestorsorz_z0 23ms34924kbC++146.3kb2025-01-17 17:23:382025-01-17 17:23:45

Judging History

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

  • [2025-01-17 17:23:45]
  • 评测
  • 测评结果:0
  • 用时:23ms
  • 内存:34924kb
  • [2025-01-17 17:23:38]
  • 提交

answer

//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("se,se2,se3,se3,se4,popcnt,abm,mmx,avx")
//#pragma GCC optimize("Ofast,fast-math")
//#pragma GCC target("avx,avx2")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;
#define F(i, a, b) for(int i = a; i <= (b); ++i)
#define F2(i, a, b) for(int i = a; i < (b); ++i)
#define dF(i, a, b) for(int i = a; i >= (b); --i)
template<typename T> void debug(string s, T x) {
    cerr << "[" << s << "] = [" << x << "]\n";
}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {
    for (int i = 0, b = 0; i < (int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++;
    else if (s[i] == ')' || s[i] == '}') b--;
    else if (s[i] == ',' && b == 0) {
        cerr << "[" << s.substr(0, i) << "] = [" << x << "] | ";
        debug(s.substr(s.find_first_not_of(' ', i + 1)), args...);
        break;
    }
}
#ifdef orzz
#define Debug(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define Debug(...)
#endif
#define pb push_back
#define fi first
#define se second
#define Mry fprintf(stderr, "%.3lf MB\n", (&Med - &Mbe) / 1048576.0)
#define Try cerr << 1e3 * clock() / CLOCKs_PER_sEC << " ms\n";
typedef long long ll;
// namespace Fread {const int sIZE = 1 << 17; char buf[sIZE], *s, *T; inline char getchar() {if (s == T) {T = (s = buf) + fread(buf, 1, sIZE, stdin); if (s == T) return '\n';} return *s++;}}
// namespace Fwrite {const int sIZE = 1 << 17; char buf[sIZE], *s = buf, *T = buf + sIZE; inline void flush() {fwrite(buf, 1, s - buf, stdout), s = buf;} inline void putchar(char c) {*s++ = c;if (s == T) flush();} struct NTR {~NTR() {flush();}} ztr;}
// #ifdef ONLINE_JUDGE
// #define getchar Fread::getchar
// #define putchar Fwrite::putchar
// #endif
inline int ri() {
    int x = 0;
    bool t = 0;
    char c = getchar();
    while (c < '0' || c > '9') t |= c == '-', c = getchar();
    while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return t ? -x : x;
}
inline void wi(int x) {
    if (x < 0) {
        putchar('-'), x = -x;
    }
    if (x > 9) wi(x / 10);
    putchar(x % 10 + 48);
}
inline void wi(int x, char s) {
    wi(x), putchar(s);
}
bool Mbe;
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3f;
const int _ = 2e5 + 5, M = 1e6 + 5;

int n, m, fa[_], rt, hson[_], dfn[_], cnt, top[_], mxd[_], dep[_];

vi d[_];

void dfs(int u, int fa) {
	dep[u] = dep[fa] + 1;
	mxd[u] = 1;
	for(int v : d[u]) if(v != fa) {
		dfs(v, u);
		if(mxd[u] < mxd[v] + 1) mxd[u] = mxd[v] + 1, hson[u] = v;
	}
}

vector<pii> mgs[_];

vi cg[_];

array<int, 4> chg[M << 3]; int zkw;

int ans[M];

void dfs2(int u, int tf) {
	top[u] = tf;
	dfn[u] = ++cnt;
	if(hson[u]) dfs2(hson[u], tf);
	for(int v : d[u]) if(!dfn[v]) {
		dfs2(v, v);
		F(j, 1, mxd[v]) {
			// merge dfn[v]+j-1 -> dfn[u]+j, k = j
			// Debug(u, v, j);
			mgs[j].pb(make_pair(dfn[v] + j - 1, dfn[u] + j));
		}
	}
}

set<int> st[_];

int pre[_];

struct Bit {
	int C[_];
	void add(int x, int v) {
		if(!x) {
			C[0] += v;
			return;
		}
		for(; x <= n; x += x & -x) C[x] += v;
	}
	int qry(int x) {
		int res = C[0];
		for(; x; x -= x & -x) res += C[x];
		return res;
	}
} tr;

void cdq(int l, int r) {
	if(l >= r) return;
	int mid = (l + r) >> 1;
	cdq(l, mid), cdq(mid + 1, r);
	// 左边time < 右边
	// 内部按照pos排好序
	// I will use 4进制bit
	int p = l - 1;
	F(i, mid + 1, r) if(chg[i][3] > 0) {
		while(p + 1 <= mid && chg[p + 1][1] <= chg[i][1]) {
			p++;
			if(chg[p][3] <= 0) {
				tr.add(chg[p][2], chg[p][3] == 0 ? 1 : -1);
			}
		}
		ans[chg[i][3] >m ? chg[i][3] - m : chg[i][3]] += tr.qry(chg[i][2] - 1) * (chg[i][3] > m ? -1 : 1);
	}
	while(p >= l) {
		if(chg[p][3] <= 0) 
			tr.add(chg[p][2], chg[p][3] == 0 ? -1 : 1);
		p--;
	}
	assert(tr.qry(n) == 0);
	sort(chg + l, chg + r + 1, [&](array<int, 4> a, array<int, 4> b) { return a[1] < b[1]; });
}

bool Med;
signed main() {
    Mry;
    // freopen("1.txt", "r", stdin);
	n = ri(), m = ri();
	F(i, 1, n) {
		fa[i] = ri();
		if(!fa[i]) rt = i;
		else d[fa[i]].pb(i);
	}
	dfs(rt, 0);
	dfs2(rt, rt);
	F(i, 1, n) st[dfn[i]].insert(i), chg[++zkw] = {0, i, 0, 0}, cg[dep[i]].pb(i);
	// static vector<array<int, 3>> qr[_];
	Debug(n, m);
	F(i, 1, m) {
		int l = ri(), r = ri(), x = ri();
		chg[++zkw] = {x, r, l, i};
		chg[++zkw] = {x, l - 1, l, m+i};
		// ans[i] -= (l - 1);
		// qr[x].pb({l, r, i});
	}
	Debug("fsjfklsjkdlfj", zkw);
	F(k, 1, n) {
		for(pii &v : mgs[k]) {
			// v.fi->v.se
			if(st[v.fi].size() > st[v.se].size()) st[v.fi].swap(st[v.se]);
			for(auto w : st[v.fi]) {
				auto p = st[v.se].upper_bound(w);
				if(p != st[v.se].end()) {
					chg[++zkw] = {k, *p, pre[*p], -1};
					chg[++zkw] = {k, *p, w, 0};
					pre[*p] = w;
				}
				if(p != st[v.se].begin()) {
					p--;
					chg[++zkw] = {k, w, pre[w], -1};
					chg[++zkw] = {k, w, *p, 0};
					pre[w] = *p;
				} else {
					chg[++zkw] = {k, w, pre[w], -1};
					chg[++zkw] = {k, w, 0, 0};
					pre[w] = 0;
				}
				st[v.se].insert(w);
			}
			st[v.fi].clear();
		}
		for(int &v :cg[k]) {
			chg[++zkw] = {k, v, pre[v], -1};
			chg[++zkw] = {k, v, n, 0};
			pre[v] = n;
		}
		Debug(k, zkw);
		// for(auto &v :qr[k]) {
		// 	chg[++zkw] = {k, v[1], v[0], v[2]};
		// }
		// for(int &v : chg[k]) pre[v] = n;
		// for(auto &v : qr[k]) {
		// 	int res = 0;
		// 	F(i, v[0], v[1]) if(pre[i] < v[0]) res++;
		// 	ans[v[2]] = res;
		// }
	}
	sort(chg + 1, chg + zkw + 1, [&](array<int, 4> a, array<int, 4> b) { return (a[0] ^ b[0]) ? a[0] < b[0] : ((a[3] >= 0) < (b[3] >= 0)); });
	// F(i, 1, zkw) {
	// 	Debug(i, chg[i][0], chg[i][1], chg[i][2], chg[i][3]);
	// }
//	Debug(zkw);
	F(i, 1, zkw) if(chg[i][3] > 0) {
		F(j, 1, i - 1) if(chg[j][3] <= 0 && chg[j][1] <= chg[i][1]) {
			ans[chg[i][3] > m ? chg[i][3] - m :chg[i][3]] += (chg[j][2] < chg[i][2]) * (chg[j][3] == 0 ? 1 : -1) * (chg[i][3] > m ? -1 : 1);
		}
//		Debug(i, ans[chg[i][3] > m ? chg[i][3] - m : chg[i][3]]);
	}
	// cdq(1, zkw);
	F(i,1 , m) printf("%d\n", ans[i]);
 	// Try;
    return 0;
}

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: 32592kb

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: 18ms
memory: 34924kb

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
Wrong Answer
time: 23ms
memory: 32752kb

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:

441
362
300
535
197
383
360
29
428
31
473
9
310
242
232
37
96
227
50
146
180
93
108
83
58
106
61
26
61
32
7
25
54
25
59
17
48
42
68
237
83
95
206
339
161
47
377
250
335
136
436
98
6
515
216
132
263
270
137
463
117
292
61
468
237
326
149
0
126
228
353
168
76
332
169
98
75
418
49
133
284
281
282
134
1...

result:

wrong answer 626th numbers differ - expected: '469', found: '460'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #30:

score: 0
Time Limit Exceeded

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
Time Limit Exceeded

Test #67:

score: 0
Time Limit Exceeded

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%