#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 5e5 + 100;
const int M = 2e6;
struct ST{
int N, v = 0;
vector <pair <int, int>> seg;
vector <int> laz;
//vector <int> action;
ST(int n, int ch) {
N = n;
v = ch;
laz.assign(n * 4 + 100, 0);
seg.resize(n * 4 + 100);
}
pair <int, int> compare(pair <int, int> a, pair <int, int> b) {
if ((a < b) == v)
return a;
return b;
}
void build(int id, int l, int r) {
if (l == r) {
seg[id] = {0, l};
return;
}
int m = (l + r) / 2;
build(id * 2, l, m);
build(id * 2 + 1, m + 1, r);
seg[id] = compare(seg[id * 2], seg[id * 2 + 1]);
}
void push(int id) {
int &t = laz[id];
laz[id * 2] += t;
laz[id * 2 + 1] += t;
seg[id * 2].f += t;
seg[id * 2 + 1].f += t;
t = 0;
}
void upd(int id, int l, int r, int u, int v, int c) {
if (l > v || r < u)
return;
if (l >= u && r <= v) {
seg[id].f += c;
laz[id] += c;
return;
}
push(id);
int m = (l + r) / 2;
upd(id * 2, l, m, u, v, c);
upd(id * 2 + 1, m + 1, r, u, v, c);
seg[id] = compare(seg[id * 2], seg[id * 2 + 1]);
}
pair <int, int> get(int id, int l, int r, int u, int v) {
if (u > v)
return {1, 0};
if (l >= u && r <= v)
return seg[id];
push(id);
int m = (l + r) / 2;
if (m + 1 <= u)
return get(id * 2 + 1, m + 1, r, u, v);
if (m >= v)
return get(id * 2, l, m, u, v);
return compare(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
int get(int p) {
return get(1, 0, N, p, p).f;
}
};
struct seg{
int N;
vector <int> st;
seg(int n) {
N = n;
st.assign(n * 4 + 1000, 1e9);
}
void upd(int id, int l, int r, int p, int v, int is) {
if (l > p || r < p)
return;
if (l == r) {
if (is) {
st[id] = v;
} else {
st[id] = min(st[id], v);
}
return;
}
int m = (l + r) / 2;
upd(id * 2, l, m, p, v, is);
upd(id * 2 + 1, m + 1, r, p, v, is);
st[id] = min(st[id * 2], st[id * 2 + 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u)
return 1e9;
if (l >= u && r <= v)
return st[id];
int m = (l + r) / 2;
return min(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
};
vector <int> pos[N];
int a[N], n;
void maximize(int &a, int b) {
if (a < b) {
a = b;
}
}
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll get(ll l, ll h){
return uniform_int_distribution<ll> (l, h)(rd);
}
int get(int add) {
seg seg(n * 2);
ST smx(n, 1), smi(n, 0);
smx.build(1, 0, n);
smi.build(1, 0, n);
for (int i = 1; i <= n; i++) {
smx.upd(1, 0, n, i, n, add);
smi.upd(1, 0, n, i, n, add);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (auto x : pos[i]) {
smx.upd(1, 0, n, x, n, -add);
smi.upd(1, 0, n, x, n, -add);
}
/*vector <int> save;
vector <pair <int, int>> st;
int lst = 0;
for (int j = 0; j < sz(pos[i]); j++) {
int p = smi.get(1, 0, n, lst, pos[i][j] - 1).s;
save.pb(p);
p = smx.get(1, 0, n, lst, pos[i][j] - 1).s;
save.pb(p);
lst = pos[i][j];
}
int p = smx.get(1, 0, n, lst, n).s;
save.pb(p);
p = smi.get(1, 0, n, lst, n).s;
save.pb(p);*/
for (int j = 0; j <= n; j++)
save.pb(j);
uniquev(save);
int j = 0;
for (auto x : save) {
while (j < sz(pos[i]) && pos[i][j] <= x)
j++;
if (x >= 0 && x <= n)
st.pb({j, smx.get(x)});
}
uniquev(st);
sort(all(st), [&] (pair <int, int> &A, pair <int, int> &B) {return A.s < B.s;} );
for (auto x : st) {
maximize(ans, x.f - seg.get(1, 0, n * 2, x.s - x.f + n, n * 2));
seg.upd(1, 0, n * 2, x.s - x.f + n, x.f, 0);
}
for (auto x : st) {
seg.upd(1, 0, n * 2, x.s - x.f + n, 1e9, 1);
}
for (auto x : pos[i]) {
smx.upd(1, 0, n, x, n, -add);
smi.upd(1, 0, n, x, n, -add);
}
}
return ans;
}
int sequence(int N, vector <int> A) {
n = N;
for (int i = 1; i <= n; i++)
pos[i].clear();
for (int i = 1; i <= n; i++) {
a[i] = A[i - 1];
pos[a[i]].pb(i);
}
int ans = max(get(-1), get(1));
return ans;
}
int trau(int n, vector <int> A) {
int ans = 0;
for (int i = 0; i < sz(A); i++) {
vector <int> v;
for (int j = i; j < sz(A); j++) {
v.pb(A[j]);
sort(all(v));
int a = 0, b = -1, c1 = 0, c2 = 0;
a = v[sz(v) / 2];
if (sz(v) % 2 == 0)
b = v[sz(v) / 2 - 1];
for (auto x : v) {
if (x == a)
c1++;
if (x == b)
c2++;
}
ans = max({ans, c1, c2});
}
}
return ans;
}
/*int main() {
int n = 20;
for (int i = 1;; i++) {
vector <int> v;
for (int j = 0; j < n; j++)
v.pb(get(1, n));
if (trau(n, v) != sequence(n, v)) {
cout << "WRONG\n";
cout << n << '\n';
for (auto x : v)
cout << x << " ";
cout << '\n';
cout << "ans " << trau(n, v) << '\n';
cout << "my code " << sequence(n, v) << '\n';
break;
} else {
cout << "OK\n";
}
}
return 0;
}*/
//2 5 7 9 9 9 17
/*
20
19 19 18 11 18 17 10 16 10 16 15 1 1 9 2 5 9 17 7 9
*/
/*
7
4 1 2 4 5 3 4
*/
Compilation message
sequence.cpp: In function 'int get(int)':
sequence.cpp:196:13: error: 'save' was not declared in this scope
196 | save.pb(j);
| ^~~~
sequence.cpp:198:17: error: 'save' was not declared in this scope
198 | uniquev(save);
| ^~~~
sequence.cpp:12:25: note: in definition of macro 'all'
12 | #define all(x) x.begin(), x.end()
| ^
sequence.cpp:198:9: note: in expansion of macro 'uniquev'
198 | uniquev(save);
| ^~~~~~~
sequence.cpp:206:17: error: 'st' was not declared in this scope; did you mean 'std'?
206 | st.pb({j, smx.get(x)});
| ^~
| std
sequence.cpp:209:17: error: 'st' was not declared in this scope; did you mean 'std'?
209 | uniquev(st);
| ^~
sequence.cpp:12:25: note: in definition of macro 'all'
12 | #define all(x) x.begin(), x.end()
| ^
sequence.cpp:209:9: note: in expansion of macro 'uniquev'
209 | uniquev(st);
| ^~~~~~~