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 <cassert>
#include <cstdio>
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define xx first
#define yy second
typedef long long ll;
using namespace std;
ll n, g, tree[1000100], a[500100], m[500100], p1, p2;
vector<ll> app[500100], v1, v2;
void update(ll i, ll x) {
i+=g;
tree[i]=x;
i/=2;
while(i>0) {
tree[i]=tree[2*i]+tree[2*i+1];
i/=2;
}
}
ll sum(ll l, ll r) {
if(l>r) return 0;
l+=g;
r+=g;
ll rez=0;
while(l<=r) {
if(l%2) {
rez+=tree[l];
l++;
}
if(r%2==0) {
rez+=tree[r];
r--;
}
l/=2;
r/=2;
}
return rez;
}
ll solve() {
g=1;
while(g<n) g*=2;
g--;
for(ll i=1; i<=n; i++) {
app[a[i]].pb(i);
m[i]=app[a[i]].size();
}
ll rez=0;
for(ll i=1; i<=n; i++) update(i, 1);
for(ll i=1; i<=n; i++) {
if(app[i].empty()) continue;
for(ll j=0; j<app[i].size(); j++) {
update(app[i][j], 0);
}
if(sum(1, n)<0) return rez;
for(ll j=0; j<app[i].size(); j++) {
update(app[i][j], -1);
}
for(ll j=0; j<app[i].size(); j++) {
if(v1.empty()||sum(1, v1.back()-1)<sum(1, app[i][j]-1)) v1.pb(app[i][j]);
}
for(ll j=app[i].size()-1; j>=0; j--) {
if(v2.empty()||sum(1, v2.back()-1)<sum(1, app[i][j]-1)) v2.pb(app[i][j]);
}
p1=0;
p2=v2.size()-1;
while(p2>=0) {
if(p1>=v1.size()||v1[p1]>v2[p2]) {
p2--;
continue;
}
if(sum(v1[p1], v2[p2])<=0) {
rez=max(rez, m[v2[p2]]-m[v1[p1]]+1);
p2--;
}
else p1++;
}
v1.clear();
v2.clear();
}
return rez;
}
int sequence(int N, vector<int> A) {
ll rez=0;
n=N;
for(ll i=0; i<N; i++) a[i+1]=A[i];
rez=solve();
for(ll i=1; i<=n; i++) {
app[i].clear();
}
for(ll i=0; i<N; i++) a[i+1]=N+1-A[i];
rez=max(rez, solve());
for(ll i=1; i<=n; i++) {
app[i].clear();
}
return rez;
}
Compilation message (stderr)
sequence.cpp: In function 'll solve()':
sequence.cpp:57:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(ll j=0; j<app[i].size(); j++) {
| ~^~~~~~~~~~~~~~
sequence.cpp:61:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(ll j=0; j<app[i].size(); j++) {
| ~^~~~~~~~~~~~~~
sequence.cpp:64:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(ll j=0; j<app[i].size(); j++) {
| ~^~~~~~~~~~~~~~
sequence.cpp:73:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | if(p1>=v1.size()||v1[p1]>v2[p2]) {
| ~~^~~~~~~~~~~
# | 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... |