QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#859282#9676. Ancestorsorz_z0 165ms37292kbC++146.2kb2025-01-17 17:02:012025-01-17 17:02:08

Judging History

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

  • [2025-01-17 17:02:08]
  • 评测
  • 测评结果:0
  • 用时:165ms
  • 内存:37292kb
  • [2025-01-17 17:02:01]
  • 提交

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;
	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[_];
	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});
	}
	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;
		}
		// 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 ? 1 : 0); });
	// F(i, 1, zkw) {
		// Debug(i, chg[i][0], chg[i][1], chg[i][2], chg[i][3]);
	// }
	// 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);
		// }
	// }
	cdq(1, zkw);
	F(i,1 , m) printf("%d\n", ans[i]);
 	// Try;
    return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 4ms
memory: 32676kb

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: 0
Wrong Answer
time: 165ms
memory: 37292kb

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:

1000
0
0
0
999
0
0
0
998
0
0
0
997
0
0
0
996
0
0
0
995
0
0
0
994
0
0
0
993
0
0
0
992
0
0
0
991
0
0
0
990
0
0
0
989
0
0
0
988
0
0
0
987
0
0
0
986
0
0
0
985
0
0
0
984
0
0
0
983
0
0
0
982
0
0
0
981
0
0
0
980
0
0
0
979
0
0
0
978
0
0
0
977
0
0
0
976
0
0
0
975
0
0
0
974
0
0
0
973
0
0
0
972
0
0
0
971
0
0
0...

result:

wrong answer 1st numbers differ - expected: '452', found: '1000'

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%