This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
}
int x = 2;
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++) {
if (mp[med].empty()) continue;
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) {
x++;
ii--;
continue;
}
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) {
x++;
ii--;
continue;
}
} 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) {
x++;
ii--;
}
}
}
for (int i : mp[med]) {
seg_ge_lr.rangeApply(i + 1, n + 1, {-2});
seg_ge_rl.rangeApply(0, i + 1, {-2});
}
}
return x - 1 ;
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:221:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
221 | for (int ii = 0; ii + x <= mp[med].size(); ii++) {
| ~~~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |