Submission #749216

# Submission time Handle Problem Language Result Execution time Memory
749216 2023-05-27T14:50:48 Z SanguineChameleon Sequence (APIO23_sequence) C++17
100 / 100
1144 ms 166748 KB
#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;

struct node {
	int mi, mx, lazy;

	node(): mi(0), mx(0), lazy(0) {};

	node(int _mi, int _mx): mi(_mi), mx(_mx), lazy(0) {};
};

const int maxn = 5e5 + 20;
int a[maxn];
vector<int> pos[maxn];
vector<int> pref[maxn][2];
vector<int> suf[maxn][2];
node tree[maxn * 4];
int n;

node merge(node L, node R) {
	return node(min(L.mi, R.mi), max(L.mx, R.mx));
}

void build(int id, int lt, int rt) {
	if (lt == rt) {
		tree[id] = node(lt, lt);
		return;
	}
	int mt = (lt + rt) >> 1;
	build(id << 1, lt, mt);
	build(id << 1 | 1, mt + 1, rt);
	tree[id] = merge(tree[id << 1], tree[id << 1 | 1]);
}

void push(int id) {
	tree[id << 1].lazy += tree[id].lazy;
	tree[id << 1].mi += tree[id].lazy;
	tree[id << 1].mx += tree[id].lazy;
	tree[id << 1 | 1].lazy += tree[id].lazy;
	tree[id << 1 | 1].mi += tree[id].lazy;
	tree[id << 1 | 1].mx += tree[id].lazy;
	tree[id].lazy = 0;
}


void update(int id, int lt, int rt, int ql, int qr) {
	if (lt == ql && rt == qr) {
		tree[id].lazy -= 2;
		tree[id].mi -= 2;
		tree[id].mx -= 2;
		return;
	}
	push(id);
	int mt = (lt + rt) >> 1;
	if (qr <= mt) {
		update(id << 1, lt, mt, ql, qr);
	}
	else if (ql >= mt + 1) {
		update(id << 1 | 1, mt + 1, rt, ql, qr);
	}
	else {
		update(id << 1, lt, mt, ql, mt);
		update(id << 1 | 1, mt + 1, rt, mt + 1, qr);
	}
	tree[id] = merge(tree[id << 1], tree[id << 1 | 1]);
}

node get(int id, int lt, int rt, int ql, int qr) {
	if (lt == ql && rt == qr) {
		return tree[id];
	}
	push(id);
	int mt = (lt + rt) >> 1;
	if (qr <= mt) {
		return get(id << 1, lt, mt, ql, qr);
	}
	else if (ql >= mt + 1) {
		return get(id << 1 | 1, mt + 1, rt, ql, qr);
	}
	else {
		return merge(get(id << 1, lt, mt, ql, mt), get(id << 1 | 1, mt + 1, rt, mt + 1, qr));
	}
}

bool check(int k) {
	for (int i = 1; i <= n; i++) {
		for (int j = k - 1; j < (int)pos[i].size(); j++) {
			if (suf[i][0][j] >= pref[i][0][j - (k - 1)] && suf[i][1][j] <= pref[i][1][j - (k - 1)]) {
				return true;
			}
		}
	}
	return false;
}

int sequence(int _N, vector<int> _A) {
	n = _N;
	for (int i = 1; i <= n; i++) {
		a[i] = _A[i - 1];
	}
	for (int i = 1; i <= n; i++) {
		pos[a[i]].push_back(i);
	}
	build(1, 0, n);
	for (int i = 1; i <= n; i++) {
		for (auto p: pos[i]) {
			pref[i][0].push_back(get(1, 0, n, 0, p - 1).mi);
			suf[i][0].push_back(get(1, 0, n, p, n).mx);
		}
		for (auto p: pos[i]) {
			update(1, 0, n, p, n);
		}
		for (auto p: pos[i]) {
			pref[i][1].push_back(get(1, 0, n, 0, p - 1).mx);
			suf[i][1].push_back(get(1, 0, n, p, n).mi);
		}
	}
	int res = 1;
	int lt = 1;
	int rt = n;
	while (lt <= rt) {
		int mt = (lt + rt) >> 1;
		if (check(mt)) {
			res = mt;
			lt = mt + 1;
		}
		else {
			rt = mt - 1;
		}
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 42 ms 82508 KB Output is correct
2 Correct 40 ms 82388 KB Output is correct
3 Correct 40 ms 82480 KB Output is correct
4 Correct 47 ms 82380 KB Output is correct
5 Correct 49 ms 82448 KB Output is correct
6 Correct 48 ms 82408 KB Output is correct
7 Correct 38 ms 82380 KB Output is correct
8 Correct 41 ms 82460 KB Output is correct
9 Correct 37 ms 82376 KB Output is correct
10 Correct 37 ms 82492 KB Output is correct
11 Correct 46 ms 82400 KB Output is correct
12 Correct 39 ms 82436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 82508 KB Output is correct
2 Correct 40 ms 82388 KB Output is correct
3 Correct 40 ms 82480 KB Output is correct
4 Correct 47 ms 82380 KB Output is correct
5 Correct 49 ms 82448 KB Output is correct
6 Correct 48 ms 82408 KB Output is correct
7 Correct 38 ms 82380 KB Output is correct
8 Correct 41 ms 82460 KB Output is correct
9 Correct 37 ms 82376 KB Output is correct
10 Correct 37 ms 82492 KB Output is correct
11 Correct 46 ms 82400 KB Output is correct
12 Correct 39 ms 82436 KB Output is correct
13 Correct 42 ms 82632 KB Output is correct
14 Correct 50 ms 82692 KB Output is correct
15 Correct 44 ms 82468 KB Output is correct
16 Correct 45 ms 82536 KB Output is correct
17 Correct 52 ms 82440 KB Output is correct
18 Correct 42 ms 82780 KB Output is correct
19 Correct 57 ms 82684 KB Output is correct
20 Correct 43 ms 82672 KB Output is correct
21 Correct 54 ms 82636 KB Output is correct
22 Correct 48 ms 82672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 82508 KB Output is correct
2 Correct 814 ms 138076 KB Output is correct
3 Correct 865 ms 137892 KB Output is correct
4 Correct 781 ms 99728 KB Output is correct
5 Correct 893 ms 133244 KB Output is correct
6 Correct 829 ms 133192 KB Output is correct
7 Correct 676 ms 99752 KB Output is correct
8 Correct 672 ms 99920 KB Output is correct
9 Correct 658 ms 98588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 82388 KB Output is correct
2 Correct 700 ms 99728 KB Output is correct
3 Correct 663 ms 99264 KB Output is correct
4 Correct 669 ms 99180 KB Output is correct
5 Correct 672 ms 102528 KB Output is correct
6 Correct 696 ms 98496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 913 ms 166748 KB Output is correct
2 Correct 879 ms 166608 KB Output is correct
3 Correct 898 ms 163820 KB Output is correct
4 Correct 964 ms 163748 KB Output is correct
5 Correct 849 ms 147048 KB Output is correct
6 Correct 835 ms 147044 KB Output is correct
7 Correct 916 ms 141024 KB Output is correct
8 Correct 835 ms 139496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 82508 KB Output is correct
2 Correct 40 ms 82388 KB Output is correct
3 Correct 40 ms 82480 KB Output is correct
4 Correct 47 ms 82380 KB Output is correct
5 Correct 49 ms 82448 KB Output is correct
6 Correct 48 ms 82408 KB Output is correct
7 Correct 38 ms 82380 KB Output is correct
8 Correct 41 ms 82460 KB Output is correct
9 Correct 37 ms 82376 KB Output is correct
10 Correct 37 ms 82492 KB Output is correct
11 Correct 46 ms 82400 KB Output is correct
12 Correct 39 ms 82436 KB Output is correct
13 Correct 42 ms 82632 KB Output is correct
14 Correct 50 ms 82692 KB Output is correct
15 Correct 44 ms 82468 KB Output is correct
16 Correct 45 ms 82536 KB Output is correct
17 Correct 52 ms 82440 KB Output is correct
18 Correct 42 ms 82780 KB Output is correct
19 Correct 57 ms 82684 KB Output is correct
20 Correct 43 ms 82672 KB Output is correct
21 Correct 54 ms 82636 KB Output is correct
22 Correct 48 ms 82672 KB Output is correct
23 Correct 211 ms 91284 KB Output is correct
24 Correct 198 ms 91296 KB Output is correct
25 Correct 222 ms 91372 KB Output is correct
26 Correct 165 ms 86240 KB Output is correct
27 Correct 160 ms 86176 KB Output is correct
28 Correct 144 ms 86116 KB Output is correct
29 Correct 141 ms 85024 KB Output is correct
30 Correct 146 ms 85168 KB Output is correct
31 Correct 135 ms 85704 KB Output is correct
32 Correct 162 ms 95872 KB Output is correct
33 Correct 196 ms 90404 KB Output is correct
34 Correct 210 ms 90384 KB Output is correct
35 Correct 175 ms 90060 KB Output is correct
36 Correct 207 ms 90276 KB Output is correct
37 Correct 185 ms 90320 KB Output is correct
38 Correct 216 ms 90444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 82508 KB Output is correct
2 Correct 40 ms 82388 KB Output is correct
3 Correct 40 ms 82480 KB Output is correct
4 Correct 47 ms 82380 KB Output is correct
5 Correct 49 ms 82448 KB Output is correct
6 Correct 48 ms 82408 KB Output is correct
7 Correct 38 ms 82380 KB Output is correct
8 Correct 41 ms 82460 KB Output is correct
9 Correct 37 ms 82376 KB Output is correct
10 Correct 37 ms 82492 KB Output is correct
11 Correct 46 ms 82400 KB Output is correct
12 Correct 39 ms 82436 KB Output is correct
13 Correct 42 ms 82632 KB Output is correct
14 Correct 50 ms 82692 KB Output is correct
15 Correct 44 ms 82468 KB Output is correct
16 Correct 45 ms 82536 KB Output is correct
17 Correct 52 ms 82440 KB Output is correct
18 Correct 42 ms 82780 KB Output is correct
19 Correct 57 ms 82684 KB Output is correct
20 Correct 43 ms 82672 KB Output is correct
21 Correct 54 ms 82636 KB Output is correct
22 Correct 48 ms 82672 KB Output is correct
23 Correct 814 ms 138076 KB Output is correct
24 Correct 865 ms 137892 KB Output is correct
25 Correct 781 ms 99728 KB Output is correct
26 Correct 893 ms 133244 KB Output is correct
27 Correct 829 ms 133192 KB Output is correct
28 Correct 676 ms 99752 KB Output is correct
29 Correct 672 ms 99920 KB Output is correct
30 Correct 658 ms 98588 KB Output is correct
31 Correct 700 ms 99728 KB Output is correct
32 Correct 663 ms 99264 KB Output is correct
33 Correct 669 ms 99180 KB Output is correct
34 Correct 672 ms 102528 KB Output is correct
35 Correct 696 ms 98496 KB Output is correct
36 Correct 913 ms 166748 KB Output is correct
37 Correct 879 ms 166608 KB Output is correct
38 Correct 898 ms 163820 KB Output is correct
39 Correct 964 ms 163748 KB Output is correct
40 Correct 849 ms 147048 KB Output is correct
41 Correct 835 ms 147044 KB Output is correct
42 Correct 916 ms 141024 KB Output is correct
43 Correct 835 ms 139496 KB Output is correct
44 Correct 211 ms 91284 KB Output is correct
45 Correct 198 ms 91296 KB Output is correct
46 Correct 222 ms 91372 KB Output is correct
47 Correct 165 ms 86240 KB Output is correct
48 Correct 160 ms 86176 KB Output is correct
49 Correct 144 ms 86116 KB Output is correct
50 Correct 141 ms 85024 KB Output is correct
51 Correct 146 ms 85168 KB Output is correct
52 Correct 135 ms 85704 KB Output is correct
53 Correct 162 ms 95872 KB Output is correct
54 Correct 196 ms 90404 KB Output is correct
55 Correct 210 ms 90384 KB Output is correct
56 Correct 175 ms 90060 KB Output is correct
57 Correct 207 ms 90276 KB Output is correct
58 Correct 185 ms 90320 KB Output is correct
59 Correct 216 ms 90444 KB Output is correct
60 Correct 1086 ms 137944 KB Output is correct
61 Correct 1139 ms 137912 KB Output is correct
62 Correct 1125 ms 137964 KB Output is correct
63 Correct 827 ms 102912 KB Output is correct
64 Correct 833 ms 102868 KB Output is correct
65 Correct 902 ms 102860 KB Output is correct
66 Correct 712 ms 99744 KB Output is correct
67 Correct 728 ms 99688 KB Output is correct
68 Correct 711 ms 99668 KB Output is correct
69 Correct 793 ms 166512 KB Output is correct
70 Correct 1144 ms 133336 KB Output is correct
71 Correct 1059 ms 132960 KB Output is correct
72 Correct 1036 ms 130504 KB Output is correct
73 Correct 1043 ms 132136 KB Output is correct
74 Correct 1027 ms 130108 KB Output is correct
75 Correct 1016 ms 132268 KB Output is correct