#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(vector<Info>(n_, v_));
}
template<class T>
void init(vector<T> init_) {
n = init_.size();
info.assign(4 << __lg(n), Info());
tag.assign(4 << __lg(n), Tag());
function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Tag {
int add = 0;
void apply(const Tag &t) & {
add += t.add;
}
};
constexpr int INF = 1e9;
struct Info {
int mx = -INF;
void apply(const Tag &t) & {
mx += t.add;
}
Info operator+(const Info &b) {
return {max(mx, b.mx)};
}
};
struct Fenwick {
int n;
vector<int> bit;
Fenwick(int n) : n(n), bit(n + 1, 0) {}
void add(int i, int delta) {
for (i++; i <= n; i += i & -i) {
bit[i] += delta;
}
}
int sum(int i) {
int res = 0;
for (; i > 0; i -= i & -i) {
res += bit[i];
}
return res;
}
int sum(int ql, int qr) {
return sum(qr) - sum(ql);
}
};
int sequence(int n, vector<int> a) {
vector<vector<int>> mp(n);
for (int i = 0; i < n; i++) {
a[i]--;
mp[a[i]].push_back(i);
}
auto ok = [&](int x) -> bool {
Fenwick fen(n);
vector<Info> lr(n + 1), rl(n + 1);
for (int i = 0; i <= n; i++) {
lr[i] = {-i};
rl[i] = {i - n};
}
LazySegmentTree<Info, Tag> seg_le_lr(lr);
LazySegmentTree<Info, Tag> seg_le_rl(rl);
LazySegmentTree<Info, Tag> seg_ge_lr(lr);
LazySegmentTree<Info, Tag> seg_ge_rl(rl);
for (int i = 0; i < n; i++) {
seg_ge_lr.rangeApply(i + 1, n + 1, {+2});
seg_ge_rl.rangeApply(0, i + 1, {+2});
}
for (int med = 0; med < n; med++) {
for (int i : mp[med]) {
fen.add(i, +1);
seg_le_lr.rangeApply(i + 1, n + 1, {+2});
seg_le_rl.rangeApply(0, i + 1, {+2});
}
for (int ii = 0; ii + x <= mp[med].size(); ii++) {
int l = mp[med][ii];
int r = mp[med][ii + x - 1] + 1;
int lem = fen.sum(l, r);
int gem = (r - l) - lem + x;
if (2 * lem >= r - l && 2 * gem >= r - l) return true;
if (2 * lem < r - l) {
int val = 2 * lem - r + l; // < 0
int up = seg_le_lr.rangeQuery(r, n + 1).mx - seg_le_lr.rangeQuery(r, r + 1).mx + seg_le_rl.rangeQuery(0, l + 1).mx - seg_le_rl.rangeQuery(l, l + 1).mx;
if (val + up >= 0) return true;
} else {
int val = 2 * gem - r + l; // < 0
int up = seg_ge_lr.rangeQuery(r, n + 1).mx - seg_ge_lr.rangeQuery(r, r + 1).mx + seg_ge_rl.rangeQuery(0, l + 1).mx - seg_ge_rl.rangeQuery(l, l + 1).mx;
if (val + up >= 0) return true;
}
}
for (int i : mp[med]) {
seg_ge_lr.rangeApply(i + 1, n + 1, {-2});
seg_ge_rl.rangeApply(0, i + 1, {-2});
}
}
return false;
};
int ans = [&]() -> int {
int l = 1, r = n;
while (l < r) {
int m = (l + r + 1) >> 1;
if (ok(m)) {
l = m;
} else {
r = m - 1;
}
}
return l;
}();
return ans;
}
Compilation message
sequence.cpp: In lambda function:
sequence.cpp:219:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
219 | for (int ii = 0; ii + x <= mp[med].size(); ii++) {
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
17 ms |
604 KB |
Output is correct |
14 |
Correct |
16 ms |
676 KB |
Output is correct |
15 |
Correct |
15 ms |
912 KB |
Output is correct |
16 |
Correct |
16 ms |
692 KB |
Output is correct |
17 |
Correct |
16 ms |
660 KB |
Output is correct |
18 |
Correct |
15 ms |
604 KB |
Output is correct |
19 |
Correct |
16 ms |
700 KB |
Output is correct |
20 |
Correct |
16 ms |
604 KB |
Output is correct |
21 |
Correct |
17 ms |
700 KB |
Output is correct |
22 |
Correct |
17 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2055 ms |
68924 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
2059 ms |
60992 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2051 ms |
74392 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
17 ms |
604 KB |
Output is correct |
14 |
Correct |
16 ms |
676 KB |
Output is correct |
15 |
Correct |
15 ms |
912 KB |
Output is correct |
16 |
Correct |
16 ms |
692 KB |
Output is correct |
17 |
Correct |
16 ms |
660 KB |
Output is correct |
18 |
Correct |
15 ms |
604 KB |
Output is correct |
19 |
Correct |
16 ms |
700 KB |
Output is correct |
20 |
Correct |
16 ms |
604 KB |
Output is correct |
21 |
Correct |
17 ms |
700 KB |
Output is correct |
22 |
Correct |
17 ms |
700 KB |
Output is correct |
23 |
Correct |
1631 ms |
14904 KB |
Output is correct |
24 |
Correct |
1627 ms |
14732 KB |
Output is correct |
25 |
Correct |
1624 ms |
14776 KB |
Output is correct |
26 |
Correct |
1395 ms |
13600 KB |
Output is correct |
27 |
Correct |
1502 ms |
13888 KB |
Output is correct |
28 |
Correct |
1452 ms |
13696 KB |
Output is correct |
29 |
Correct |
1216 ms |
13440 KB |
Output is correct |
30 |
Correct |
1264 ms |
13744 KB |
Output is correct |
31 |
Correct |
1025 ms |
13524 KB |
Output is correct |
32 |
Correct |
1436 ms |
15780 KB |
Output is correct |
33 |
Correct |
1334 ms |
14828 KB |
Output is correct |
34 |
Correct |
1541 ms |
14656 KB |
Output is correct |
35 |
Correct |
1502 ms |
14524 KB |
Output is correct |
36 |
Correct |
1451 ms |
14844 KB |
Output is correct |
37 |
Correct |
1514 ms |
14696 KB |
Output is correct |
38 |
Correct |
1596 ms |
14692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
17 ms |
604 KB |
Output is correct |
14 |
Correct |
16 ms |
676 KB |
Output is correct |
15 |
Correct |
15 ms |
912 KB |
Output is correct |
16 |
Correct |
16 ms |
692 KB |
Output is correct |
17 |
Correct |
16 ms |
660 KB |
Output is correct |
18 |
Correct |
15 ms |
604 KB |
Output is correct |
19 |
Correct |
16 ms |
700 KB |
Output is correct |
20 |
Correct |
16 ms |
604 KB |
Output is correct |
21 |
Correct |
17 ms |
700 KB |
Output is correct |
22 |
Correct |
17 ms |
700 KB |
Output is correct |
23 |
Execution timed out |
2055 ms |
68924 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |