QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#352057#5017. 相等树链zyc07041930 5073ms417804kbC++144.4kb2024-03-12 20:08:512024-03-12 20:08:53

Judging History

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

  • [2024-03-12 20:08:53]
  • 评测
  • 测评结果:30
  • 用时:5073ms
  • 内存:417804kb
  • [2024-03-12 20:08:51]
  • 提交

answer

#include <bits/stdc++.h>
#define PII pair<int, int>
#define ull unsigned long long
#define ll long long
using namespace std;
const int N = 2e5 + 10;
const int M = 2e5 + 3;

mt19937_64 rnd(chrono :: steady_clock :: now().time_since_epoch().count());
inline int read() {
	char ch = getchar(); int x = 0;
	while (!isdigit(ch)) {ch = getchar();}
	while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
	return x;
}

int n;
vector<int> g[N], G[N];

namespace subtask1 {
	int ans;
	ull val[N];
	vector<ull> vec[2];
	
	void dfs1(int x, int fa, ull now) {
		now ^= val[x];
		vec[0].push_back(now);
		for (auto y : g[x])
			if (y != fa) dfs1(y, x, now);
	}
	
	void dfs2(int x, int fa, ull now) {
		now ^= val[x];
		vec[1].push_back(now);
		for (auto y : G[x])
			if (y != fa) dfs2(y, x, now);
	}
	
	void solve() {
		vec[0].reserve(n * n);
		vec[1].reserve(n * n);
		for (int i = 1; i <= n; ++i) val[i] = rnd();
		for (int i = 1; i <= n; ++i) dfs1(i, i, 0);
		for (int i = 1; i <= n; ++i) dfs2(i, i, 0);
		sort(vec[0].begin(), vec[0].end());
		sort(vec[1].begin(), vec[1].end());
		for (int i = 0, j = 0; i < vec[0].size(); ++i) {
			while (j < vec[1].size() && vec[1][j] < vec[0][i]) j++;
			if (j == vec[1].size()) break;
			if (j < vec[1].size() && vec[0][i] == vec[1][j]) ans++;
		}
		ans = (ans + n) / 2;
		printf("%d\n", ans);
	}
}

namespace subtask2 {
	int id[N], a[N], cnt[N * 3];
	ll ans;
	vector<PII> vec[N][3];
	
	bool check() {
		for (int i = 1; i <= n; ++i)
			if (g[i].size() > 2 || G[i].size() > 2) return false;
		return true;
	}
	
	void dfs1(int x, int fa, int d) {
		id[x] = d;
		for (auto y : g[x])
			if (y != fa) dfs1(y, x, d + 1);
	}
	
	void dfs2(int x, int fa, int d) {
		a[d] = id[x];
		for (auto y : G[x])
			if (y != fa) dfs2(y, x, d + 1);
	}
	
	void work(int l, int r) {
		if (l > r) return;
		if (l == r) return ans++, void();
		int mid = (l + r) >> 1;
		for (int i = mid, mn = 1e9, mx = -1e9, p = mid, q = mid; i >= l; --i) {
			mn = min(mn, a[i]); mx = max(mx, a[i]);
			while (p < r && a[p + 1] >= mn) p++;
			while (q < r && a[q + 1] <= mx) q++;
			for (int o = 0, posl, posr; o < 4; ++o) {
				posl = mid + 1; posr = r;
				if (o & 1) posl = max(posl, p + 1);
				else posr = min(posr, p);
				if (o & 2) posl = max(posl, q + 1);
				else posr = min(posr, q);
				if (posl > posr) continue;
				if (o == 0) ans += (posl <= i + mx - mn && i + mx - mn <= posr);
				else if (o == 1) vec[posl - 1][0].push_back(make_pair(mx + i, -1)), vec[posr][0].push_back(make_pair(mx + i, 1));
				else if (o == 2) vec[posl - 1][1].push_back(make_pair(i - mn, -1)), vec[posr][1].push_back(make_pair(i - mn, 1));
				else vec[posl - 1][2].push_back(make_pair(i, -1)), vec[posr][2].push_back(make_pair(i, 1));
			}
		}
		for (int i = mid + 1, mn = 1e9; i <= r; ++i) {
			mn = min(mn, a[i]);
			cnt[i + mn + M]++;
			for (auto tmp : vec[i][0]) ans += tmp.second * cnt[tmp.first + M];
		}
		for (int i = mid + 1, mn = 1e9; i <= r; ++i) {
			mn = min(mn, a[i]);
			cnt[i + mn + M]--;
		}
		for (int i = mid + 1, mx = -1e9; i <= r; ++i) {
			mx = max(mx, a[i]);
			cnt[i - mx + M]++;
			for (auto tmp : vec[i][1]) ans += tmp.second * cnt[tmp.first + M];
		}
		for (int i = mid + 1, mx = -1e9; i <= r; ++i) {
			mx = max(mx, a[i]);
			cnt[i - mx + M]--;
		}
		for (int i = mid + 1, mn = 1e9, mx = -1e9; i <= r; ++i) {
			mn = min(mn, a[i]); mx = max(mx, a[i]);
			cnt[i + mn - mx + M]++;
			for (auto tmp : vec[i][2]) ans += tmp.second * cnt[tmp.first + M];
		}
		for (int i = mid + 1, mn = 1e9, mx = -1e9; i <= r; ++i) {
			mn = min(mn, a[i]); mx = max(mx, a[i]);
			cnt[i + mn - mx + M]--;
		}
		for (int i = mid; i <= r; ++i) vec[i][0].clear(), vec[i][1].clear(), vec[i][2].clear();
		work(l, mid); work(mid + 1, r);
	}
	
	void solve() {
		for (int i = 1; i <= n; ++i)
			if (g[i].size() == 1) dfs1(i, i, 1);
		for (int i = 1; i <= n; ++i)
			if (G[i].size() == 1) dfs2(i, i, 1);
		work(1, n);
		printf("%lld\n", ans);
	}
}

int main() {
	#ifdef zyc
		freopen("in.in", "r", stdin);
		freopen("mine.out", "w", stdout);
	#endif
	n = read();
	for (int i = 2, x; i <= n; ++i) x = read(), g[x].push_back(i), g[i].push_back(x);
	for (int i = 2, x; i <= n; ++i) x = read(), G[x].push_back(i), G[i].push_back(x);
	if (n <= 5000) subtask1 :: solve();
	else if (subtask2 :: check()) subtask2 :: solve();
	return 0;
}

詳細信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 5037ms
memory: 417804kb

input:

5000
1296 1400 867 4533 1296 2007 2059 115 821 2960 3187 1597 2409 2708 4743 4778 1345 3967 911 3400 4249 3793 339 3145 3490 607 4148 3513 3264 3852 568 775 828 1348 423 3678 305 1938 1096 1373 2662 1048 4328 4208 203 779 3103 3372 4523 192 264 792 4943 2211 2494 3513 3555 4935 3277 3390 4624 128 18...

output:

76002

result:

ok 1 number(s): "76002"

Test #2:

score: 0
Accepted
time: 5020ms
memory: 417616kb

input:

5000
1820 281 610 3735 3580 3994 2060 2424 3338 2859 281 532 1286 1771 825 3738 3793 2260 556 4068 3793 4169 4411 4941 122 4270 4711 524 4037 2508 50 3343 2030 1151 4002 533 2994 1440 1762 3851 3050 4470 555 1979 3178 3933 3793 281 4810 520 3793 3535 2526 4422 2859 1561 1544 649 4544 2882 1236 2749 ...

output:

604316

result:

ok 1 number(s): "604316"

Test #3:

score: 0
Accepted
time: 5073ms
memory: 417552kb

input:

5000
4333 51 707 3055 3433 1451 1305 1431 3081 302 1633 88 2024 441 120 4650 3927 4970 2578 3170 4245 4204 2102 4954 3140 1039 360 3173 4203 4927 4437 4337 4502 1712 3598 2968 2 4884 1260 1768 585 1815 4346 2938 4638 4886 4482 1095 1452 298 2702 2257 1375 2819 4482 711 220 396 3907 4792 2798 4445 42...

output:

912032

result:

ok 1 number(s): "912032"

Test #4:

score: 0
Accepted
time: 4907ms
memory: 417740kb

input:

5000
362 4710 4997 4405 4728 3964 4258 3568 4997 2924 2931 4997 1094 2174 2220 127 4739 260 2591 4130 4916 1614 1408 324 2924 3272 4997 4020 2924 4216 2924 2931 2924 4783 271 1101 2924 246 4953 2553 4588 2924 1770 3738 4617 2508 2486 2137 1348 4847 2632 596 1011 1442 1287 4665 2924 2203 4411 726 109...

output:

1176721

result:

ok 1 number(s): "1176721"

Test #5:

score: 0
Accepted
time: 5033ms
memory: 417740kb

input:

5000
3579 3530 3328 388 4864 4954 4597 3600 2428 1610 4533 1797 427 1296 3595 3861 4703 2914 4194 3195 451 585 3600 1134 1649 470 2049 2843 2845 3473 26 845 484 3301 1929 1342 1937 2003 1543 832 2301 2543 1889 1211 1619 1937 4471 585 4440 3600 1398 4687 2931 3982 2334 589 388 4012 873 66 2406 3861 1...

output:

1226857

result:

ok 1 number(s): "1226857"

Subtask #2:

score: 20
Accepted

Test #6:

score: 20
Accepted
time: 259ms
memory: 86648kb

input:

200000
13177 40498 104798 83659 186055 32788 86489 72123 13521 134868 158968 60124 166316 163390 120935 103000 83938 57807 97940 40447 137723 154683 191864 59080 102808 3969 21451 165592 128776 49468 4101 26441 139317 59503 18053 118809 187783 149816 21609 98521 165692 52964 60425 23437 29614 106886...

output:

5859368

result:

ok 1 number(s): "5859368"

Test #7:

score: 0
Accepted
time: 277ms
memory: 85824kb

input:

200000
161252 109349 161307 131176 54282 35989 53345 116328 52886 20845 166068 198634 185607 110703 32172 153437 50060 194586 193712 73284 32556 105087 55275 157714 22357 182552 31342 100889 145473 91759 18280 144489 108133 130988 11561 20028 121278 138065 83647 33848 33634 31990 198971 110781 12801...

output:

110388948

result:

ok 1 number(s): "110388948"

Test #8:

score: 0
Accepted
time: 244ms
memory: 81072kb

input:

200000
36915 117643 88659 78272 142053 101722 71138 149291 152470 118051 45210 31645 187500 22733 178345 55170 28768 44890 26946 76823 76271 9886 197447 130337 74747 175940 118067 191159 19884 113644 73935 160371 97288 153196 50668 47567 113533 73075 90904 115114 191694 127924 127338 41621 134964 59...

output:

469103910

result:

ok 1 number(s): "469103910"

Test #9:

score: 0
Accepted
time: 254ms
memory: 83200kb

input:

200000
3943 160214 22824 98337 873 3550 102218 67841 56961 130137 87920 154401 45794 144615 52487 75188 13477 151928 41794 147148 88519 25499 59155 187395 70572 37799 7846 166650 165689 178923 110784 68004 124416 7070 37566 126445 23236 78630 190578 145179 81517 809 99830 98383 67869 158370 182186 1...

output:

254891707

result:

ok 1 number(s): "254891707"

Test #10:

score: 0
Accepted
time: 247ms
memory: 81996kb

input:

200000
78377 9603 105868 5816 97565 17017 11229 64332 152282 115911 5141 119594 138303 67697 62645 28928 113832 166252 170769 60777 39110 85804 122988 117490 80461 169830 15334 189378 9037 191383 143689 123124 18788 113025 35138 63649 116803 33050 135937 99323 119570 44477 174794 28051 74975 174331 ...

output:

779879990

result:

ok 1 number(s): "779879990"

Test #11:

score: 0
Accepted
time: 293ms
memory: 75472kb

input:

200000
31706 198038 102731 72443 44408 116386 129202 193795 176464 175136 12293 17325 194955 2759 172903 37032 60623 73343 55344 138068 10675 29053 29280 94350 175071 73192 10795 127030 18516 28564 170635 88693 143311 110487 10208 57489 1052 33420 156977 149595 34056 171577 39262 71741 71633 61355 1...

output:

4797642624

result:

ok 1 number(s): "4797642624"

Subtask #3:

score: 0
Wrong Answer

Dependency #2:

100%
Accepted

Test #12:

score: 0
Wrong Answer
time: 49ms
memory: 39676kb

input:

200000
62936 42114 49454 95737 154735 83651 12241 12518 111465 87130 38023 12482 194231 193238 50051 69033 102675 40262 72917 146819 56538 159148 35426 119935 46694 63476 37721 177034 120832 10487 177187 12093 118464 95232 28721 165669 13308 116990 16648 187886 3227 181605 10993 174426 186874 45794 ...

output:


result:

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

Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

0%