QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#303309 | #7685. Barkley II | ivaziva | TL | 7ms | 22968kb | C++17 | 3.1kb | 2024-01-12 06:09:35 | 2024-01-12 06:09:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define MAXN 500010
long long niz[MAXN];
bool obrisano[MAXN];
vector<long long> pozicije[MAXN];
long long bit[MAXN];
long long last_visit[MAXN];
vector<pair<pair<long long,long long>,long long>> query;
bool cmp(pair<pair<long long,long long>,long long> a,pair<pair<long long,long long>,long long> b)
{
return (a.first.second<b.first.second);
}
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=0;i<MAXN;i++) bit[i]=0;
for (long long i=0;i<MAXN;i++) last_visit[i]=-1;
long long query_container=0;
long long ans[query.size()+10];
for (long long i=0;i<n;i++)
{
if (last_visit[niz[i]]!=-1) update(last_visit[niz[i]]+1,-1,n);
last_visit[niz[i]]=i;
update(i+1,1,n);
while (query_container<query.size() and query[query_container].first.second==i)
{
ans[query_container]=sol(query[query_container].first.second+1)-sol(query[query_container].first.first);
query_container++;
}
}
long long val=-LLONG_MAX;
for (long long i=0;i<query.size();i++) val=max(val,ans[i]-query[i].second);
cout<<val<<endl;
}
int main()
{
long long t;cin>>t;
for (long long z=0;z<t;z++)
{
long long n,m;cin>>n>>m;
for (long long i=0;i<n;i++) cin>>niz[i];
for (long long i=0;i<n;i++) obrisano[niz[i]]=false;
vector<long long> vec;
long long ans=-LLONG_MAX;
for (long long i=0;i<n;i++)
{
if (obrisano[niz[i]]==false)
{
pozicije[niz[i]].clear();
obrisano[niz[i]]=true;
vec.push_back(niz[i]);
}
pozicije[niz[i]].push_back(i);
if (pozicije[niz[i]].size()==1)
{
if (i-1<0) continue;
query.push_back({{0,i-1},niz[i]});
}
else
{
long long siz=pozicije[niz[i]].size();
long long poz1=pozicije[niz[i]][siz-2]+1;
long long poz2=pozicije[niz[i]][siz-1]-1;
if (poz2<poz1) continue;
query.push_back({{poz1,poz2},niz[i]});
}
}
sort(vec.begin(),vec.end());
long long mex_niza=1;
for (long long i=0;i<vec.size();i++)
{
if (mex_niza==vec[i]) mex_niza++;
long long siz=pozicije[vec[i]].size();
long long poz=pozicije[vec[i]][siz-1];
if (n-1<poz+1) continue;
query.push_back({{poz+1,n-1},vec[i]});
}
query.push_back({{0,n-1},mex_niza});
sort(query.begin(),query.end(),cmp);
//for (long long i=0;i<query.size();i++) cout<<query[i].first.first<<" "<<query[i].first.second<<" "<<query[i].second<<endl;
answer_queries(n); query.clear();
}
}
详细
Test #1:
score: 100
Accepted
time: 7ms
memory: 22968kb
input:
2 5 4 1 2 2 3 4 5 10000 5 2 3 4 1
output:
2 3
result:
ok 2 number(s): "2 3"
Test #2:
score: -100
Time Limit Exceeded
input:
50000 10 19 12 6 1 12 11 15 4 1 13 18 10 8 8 7 6 7 6 2 2 3 4 8 10 6 3 2 6 6 5 2 3 4 5 6 10 11 6 3 7 9 2 1 2 10 10 4 10 6 6 1 2 6 1 1 3 4 2 1 10 9 8 5 3 9 1 7 5 5 1 1 10 5 1 4 3 2 5 4 5 3 5 2 10 14 3 8 12 10 4 2 3 13 7 3 10 14 5 5 12 2 8 1 13 9 8 5 10 7 5 5 6 6 1 5 3 7 3 4 10 7 5 1 4 6 1 6 4 3 7 5 10...
output:
6 5 4 4 2 4 3 7 4 4 4 5 2 3 6 6 7 5 7 6 5 5 6 2 6 8 7 2 5 5 6 2 2 3 4 5 3 3 7 3 2 5 6 1 3 5 3 3 3 8 6 6 5 7 4 4 5 4 6 6 6 3 7 3 6 3 3 7 7 6 6 7 4 3 3 4 4 6 3 4 6 6 4 5 5 9 4 5 7 5 3 5 2 2 5 6 6 8 4 3 4 5 5 5 7 7 3 2 6 5 3 5 4 4 5 6 6 5 6 7 7 4 5 7 4 7 3 7 6 6 6 5 4 2 5 4 2 3 6 5 2 6 5 5 4 3 5 6 6 6 ...