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 pll pair<long long, long long>
#define mp make_pair
#define pb push_back
#define xx first
#define yy second
typedef long long ll;
using namespace std;
struct node{
ll zbir;
ll pref;
ll suf;
ll sred;
node(){}
node(ll x):zbir(x),pref(x),suf(x),sred(x){}
};
ll n, g, a[500100], m[500100], p1, p2;
node tree[2000100];
vector<ll> app[500100];
vector<pll> v1, v2;
void update(ll i, ll x) {
i+=g;
tree[i]=node(x);
i/=2;
while(i>0) {
tree[i].zbir=tree[2*i].zbir+tree[2*i+1].zbir;
tree[i].pref=max(tree[2*i].pref, tree[2*i].zbir+tree[2*i+1].pref);
tree[i].suf=max(tree[2*i+1].suf, tree[2*i].suf+tree[2*i+1].zbir);
tree[i].sred=max(max(tree[2*i].sred, tree[2*i+1].sred), tree[2*i].suf+tree[2*i+1].pref);
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].zbir;
l++;
}
if(r%2==0) {
rez+=tree[r].zbir;
r--;
}
l/=2;
r/=2;
}
return rez;
}
ll prefm(ll l, ll r) {
if(l>r) return 0;
l+=g;
r+=g;
ll rez=0, trez=0;
while(l<=r) {
if(l%2) {
rez=max(rez, trez+tree[l].pref);
trez+=tree[l].zbir;
l++;
}
if(r%2==0) {
rez=max(rez, trez+sum(l-g, r-g-1)+tree[r].pref);
r--;
}
l/=2;
r/=2;
}
return rez;
}
ll sufm(ll l, ll r) {
if(l>r) return 0;
l+=g;
r+=g;
ll rez=0, trez=0;
while(l<=r) {
if(l%2) {
rez=max(rez, trez+sum(l-g+1, r-g)+tree[l].suf);
l++;
}
if(r%2==0) {
rez=max(rez, trez+tree[r].suf);
trez+=tree[r].zbir;
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()||v1.back().yy<prefm(1, app[i][j]-1)) v1.pb(mp(app[i][j], prefm(1, app[i][j]-1)));
}
for(ll j=app[i].size()-1; j>=0; j--) {
if(v2.empty()||v2.back().yy<sufm(app[i][j]+1, n)) v2.pb(mp(app[i][j], sufm(app[i][j]-1, n)));
}
p1=0;
p2=v2.size()-1;
while(p2>=0) {
if(p1>=v1.size()||v1[p1].xx>v2[p2].xx) {
p2--;
continue;
}
if(v1[p1].yy+v2[p2].yy>=sum(1, n)) {
rez=max(rez, m[v2[p2].xx]-m[v1[p1].xx]+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:114: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]
114 | for(ll j=0; j<app[i].size(); j++) {
| ~^~~~~~~~~~~~~~
sequence.cpp:118: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]
118 | for(ll j=0; j<app[i].size(); j++) {
| ~^~~~~~~~~~~~~~
sequence.cpp:121: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]
121 | for(ll j=0; j<app[i].size(); j++) {
| ~^~~~~~~~~~~~~~
sequence.cpp:130:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | if(p1>=v1.size()||v1[p1].xx>v2[p2].xx) {
| ~~^~~~~~~~~~~
# | 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... |