Submission #932102

#TimeUsernameProblemLanguageResultExecution timeMemory
932102Edu175Sequence (APIO23_sequence)C++17
11 / 100
2077 ms801124 KiB
#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; 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...