QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303521 | #7685. Barkley II | ivaziva | RE | 0ms | 0kb | C++17 | 2.5kb | 2024-01-12 17:46:26 | 2024-01-12 17:46:26 |
answer
#include <bits/stdc++.h>
using namespace std;
#define MAXN 500010
long long niz[MAXN];
vector<long long> pozicije[MAXN];
long long bit[MAXN];
long long last_visit[MAXN];
vector<pair<long long,long long>> query[MAXN];
void update(long long x,long long val,long long n)
{
for (long long i=x;i<=n;i+=(i&(-i))) bit[i]+=val;
}
long long sol(long long x)
{
long long br=0;
for (long long i=x;i>=1;i-=(i&(-i))) br+=bit[i];
return br;
}
void answer_queries(long long n)
{
for (long long i=1;i<=n;i++) bit[i]=0;
for (long long i=1;i<=n;i++) last_visit[i]=-1;
long long ans=-LLONG_MAX;
for (long long i=1;i<=n;i++)
{
if (last_visit[niz[i]]!=-1) update(last_visit[niz[i]],-1,n);
last_visit[niz[i]]=i;update(i,1,n);
long long s=query[i].size();
for (long long j=0;j<s;j++)
{
long long l=query[i][j].first;
long long mex=query[i][j].second;
long long val=sol(i)-sol(l-1);
ans=max(ans,val-mex);
}
}
cout<<ans<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;cin>>t;
for (long long z=0;z<t;z++)
{
///ZASTO OVO DAJE TLE JEBENI JEBAO VAS TL
long long n,m;cin>>n>>m;
for (long long i=1;i<=n;i++) cin>>niz[i];
for (long long i=1;i<=n;i++) pozicije[niz[i]].clear();
for (long long i=1;i<=n;i++) query[i].clear();
for (long long i=1;i<=n;i++) pozicije[niz[i]].push_back(i);
set<long long> sett;
for (long long i=1;i<=n;i++) sett.insert(niz[i]);
for (long long i=1;i<=n;i++)
{
if (sett.find(niz[i])==sett.end()) continue;
long long s=pozicije[i].size();
for (long long j=1;j<s;j++)
{
long long poz1=pozicije[i][j-1];
long long poz2=pozicije[i][j];
if (poz2-1>=poz1+1) query[poz2-1].push_back({poz1+1,i});
}
long long poz1=pozicije[i][0];
if (poz1-1>=1) query[poz1-1].push_back({1,i});
long long poz2=pozicije[i][s-1];
if (n>=poz2+1) query[n].push_back({poz2+1,i});
}
long long mex_niza=1;
for (auto p:sett)
{
if (p==mex_niza) mex_niza++;
else break;
}
query[n].push_back({1,mex_niza});
answer_queries(n);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 5 4 1 2 2 3 4 5 10000 5 2 3 4 1