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 <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 5e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll BASE = 137;
int n;
int a[N];
vector<int> pos[N];
struct Node {
int premn, sufmn, premx, sufmx, sum;
};
struct Segment_Tree {
int m;
Node st[N << 2] ;
void init (int n) {
m = n;
}
Node mer (Node lf, Node rt) {
Node cur;
cur.sum = lf.sum + rt.sum;
cur.premn = min(lf.premn, lf.sum + rt.premn);
cur.premx = max(lf.premx, lf.sum + rt.premx);
cur.sufmn = min(rt.sufmn, rt.sum + lf.sufmn);
cur.sufmx = max(rt.sufmx, rt.sum + lf.sufmx);
return cur;
}
void update (int id, int l, int r, int pos, int val) {
if (l > pos || r < pos)
return;
if (l == r) {
st[id].sum = val;
st[id].premn = st[id].sufmn = min(val, 0);
st[id].premx = st[id].sufmx = max(val, 0);
return;
}
int mid = l + r >> 1;
update (id << 1, l, mid, pos, val);
update (id << 1 | 1, mid + 1, r, pos, val);
st[id] = mer(st[id << 1], st[id << 1 | 1]);
}
Node get (int id, int l, int r, int u, int v) {
if (l > v || r < u || u > v) return {0, 0, 0, 0, 0};
if (l >= u && r <= v) return st[id];
int mid = l + r >> 1;
return mer (get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v));
}
void update (int pos, int val) {
update (1, 1, m, pos, val);
}
Node get (int u, int v) {
return get (1, 1, m, u, v);
}
}ST;
bool check (int L, int R, int K) {
Node prep = ST.get (1, L - 1);
Node sufp = ST.get (R + 1, n);
int sum = ST.get (L + 1, R - 1).sum;
pair<int,int> pre = MP(prep.sufmn, prep.sufmx), suf = MP(sufp.premn, sufp.premx);
int sL = pre.fs + suf.fs + sum;
int sR = pre.se + suf.se + sum;
if (K >= abs(sum)) return 1;
// if (K == 3 && L == 0 && R == n + 1) cout <<"hihia: "<<sL <<" "<<sR<<"\n";
if (sL <= K && K <= sR) return 1;
return 0;
}
int sequence (int _n, vector<int> _a) {
//void solution () {
// cin >> n;
// rep (i, 1, n) {
// cin >> a[i];
// }
n = _n;
rep (i, 1, n) a[i] = _a[i - 1];
rep (i, 1, n) {
pos[a[i]].pb(i);
}
ST.init(n);
rep (i, 1, n) ST.update (i, 1);
int res = 0;
rep (x, 1, n) {
vector<int> &curp = pos[x];
iter (&id, curp) ST.update(id, 0);
int ptr = 0;
// cout << x<<":\n";
rep (i, 0, SZ(curp) - 1) {
int cur = curp[i];
if (ptr < i) ptr = i;
while (ptr < SZ(curp) - 1 && check (cur - 1, curp[ptr + 1] + 1, ptr + 1 - i + 1)) ++ptr;
// if (x == 3 && i == 0) cout <<"hihi: "<< check (cur - 1, curp[ptr + 1] + 1, 3) <<"\n";
// cout << i << " "<<ptr <<"\n";
res = max(res, ptr - i + 1);
}
iter (&id, curp)
ST.update(id, -1);
}
// cout << res <<"\n";
return res;
}
//#define file(name) freopen(name".inp","r",stdin); \
//freopen(name".out","w",stdout);
//int main () {
//// file("c");
// ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int num_Test = 1;
//// cin >> num_Test;
// while (num_Test--)
// solution();
//}
/*
no bug challenge +2
2 + 8 * 2 - 9
*/
Compilation message (stderr)
sequence.cpp:129:1: warning: multi-line comment [-Wcomment]
129 | //#define file(name) freopen(name".inp","r",stdin); \
| ^
sequence.cpp: In member function 'void Segment_Tree::update(int, int, int, int, int)':
sequence.cpp:60:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | int mid = l + r >> 1;
| ~~^~~
sequence.cpp: In member function 'Node Segment_Tree::get(int, int, int, int, int)':
sequence.cpp:69:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
69 | int mid = l + r >> 1;
| ~~^~~
# | 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... |