이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sequence.h"
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(ll i=a,apio=b;i<apio;i++)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define imp(v) for(auto slkdh:v)cout<<slkdh<<" ";cout<<"\n"
using namespace std;
typedef int ll;
typedef pair<ll,ll> ii;
#pragma GCC optimize("Ofast") // may lead to precision errors
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const ll C=5e5,MAXN=2*C+5;
unordered_map<ll,ll> ft[MAXN+1];
void upd(ll p, ll q, ll v){
p+=C,q+=C;
p=MAXN-1-p; v-=C;
//cout<<"upd "<<p<<","<<q<<" "<<-v<<"\n";
for(ll i=p+1;i<=MAXN;i+=i&-i)
for(ll j=q+1;j<=MAXN;j+=j&-j){
ll &r=ft[i][j];
r=min(r,v);
}
}
ll query(ll p, ll q){
p+=C,q+=C;
p=MAXN-1-p;
//cout<<"query "<<p<<","<<q<<"\n";
ll res=0;
for(ll i=p+1;i;i-=i&-i)
for(ll j=q+1;j;j-=j&-j){
res=min(res,ft[i][j]);
}
//cout<<res<<" res\n";
return res+C;
}
int sequence(int N, std::vector<int> A) {
ll n=N;
vector<ll>a(n);
fore(i,0,n)a[i]=A[i]-1;
vector<ll>pos[n];
fore(i,0,n)pos[a[i]].pb(i);
vector<ll>s(n);
fore(i,0,n)s[i]=1;
ll res=0;
fore(i,0,n)if(SZ(pos[i])){
//cout<<"\nin"<<i<<":\n";
for(auto j:pos[i])s[j]=0;
//imp(s);
fore(j,0,MAXN)ft[j].clear();
upd(0,0,0);
ll sum=0,rew=0;
fore(j,0,n){
sum+=s[j],rew+=a[j]==i;
ll p=sum-rew,q=sum+rew;
res=max(res,rew-query(p,q));
upd(p,q,rew);
//cout<<j<<": "<<sum<<" "<<rew<<"\n";
//cout<<"add "<<p<<","<<q<<" "<<rew<<"\n";
//cout<<res<<"\n\n";
}
//cout<<res<<"\n";
for(auto j:pos[i])s[j]=-1;
}
return res;
}
# | 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... |