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;
struct Node {
int mn = 1e9, mx = -1e9, lazy = 0;
friend Node merge(Node a, Node b) {
Node c;
c.mn = min(a.mn, b.mn);
c.mx = max(a.mx, b.mx);
return c;
}
};
struct SegmentTree {
int n;
vector<Node> t;
SegmentTree(int _n) {
n = _n;
t.assign(4 * n, Node());
}
void operate(int x, int v) {
t[x].mn += v;
t[x].mx += v;
t[x].lazy += v;
}
void push(int x, int lx, int rx) {
if (lx == rx) return;
operate(x << 1, t[x].lazy);
operate(x << 1 | 1, t[x].lazy);
t[x].lazy = 0;
}
void build(int x, int lx, int rx) {
if (lx == rx) {
t[x].mn = t[x].mx = 0;
return;
}
int mx = (lx + rx) >> 1;
build(x << 1, lx, mx);
build(x << 1 | 1, mx + 1, rx);
t[x] = merge(t[x << 1], t[x << 1 | 1]);
}
void update(int x, int lx, int rx, int l, int r, int v) {
if (t[x].lazy) push(x, lx, rx);
if (lx > r || l > rx) return;
if (l <= lx && r >= rx) {
operate(x, v);
return;
}
int mx = (lx + rx) >> 1;
update(x << 1, lx, mx, l, r, v);
update(x << 1 | 1, mx + 1, rx, l, r, v);
t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);
t[x].mx = max(t[x << 1].mx, t[x << 1 | 1].mx);
}
Node query(int x, int lx, int rx, int l, int r) {
if (t[x].lazy) push(x, lx, rx);
if (l > rx || lx > r) return Node();
if (l <= lx && r >= rx) return t[x];
int mx = (lx + rx) >> 1;
return merge(query(x << 1, lx, mx, l, r), query(x << 1 | 1, mx + 1, rx, l, r));
}
};
int sequence(int n, vector<int> a) {
vector<int> pos[n + 1];
for (int i = 0; i < n; i++) {
pos[a[i]].push_back(i + 1);
}
SegmentTree st1(n), st2(n);
st1.build(1, 1, n);
st2.build(1, 1, n);
for (int i = 1; i <= n; i++) {
st1.update(1, 1, n, i, n, 1);
st2.update(1, 1, n, i, n, 1);
}
int ans = 0;
for (int x = 1; x <= n; x++) {
for (int i : pos[x]) st1.update(1, 1, n, i, n, -2);
for (int i = 0; i < pos[x].size(); i++) {
while (i + ans < pos[x].size()) {
int x1 = pos[x][i], x2 = pos[x][i + ans];
Node l1 = st1.query(1, 1, n, 1, x1 - 1), r1 = st1.query(1, 1, n, x2, n);
Node l2 = st2.query(1, 1, n, 1, x1 - 1), r2 = st2.query(1, 1, n, x2, n);
l1.mx = max(l1.mx, 0), l2.mx = max(l2.mx, 0);
l1.mn = min(l1.mn, 0), l2.mn = min(l2.mn, 0);
int mn = r1.mn - l1.mx;
int mx = r2.mx - l2.mn;
if (mx >= 0 && mn <= 0) {
ans++;
} else {
break;
}
}
}
for (int i : pos[x]) st2.update(1, 1, n, i, n, -2);
}
return ans;
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 0; i < pos[x].size(); i++) {
| ~~^~~~~~~~~~~~~~~
sequence.cpp:78:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | while (i + ans < pos[x].size()) {
| ~~~~~~~~^~~~~~~~~~~~~~~
# | 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... |