QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#303314#7685. Barkley IIivazivaTL 4ms35808kbC++172.9kb2024-01-12 06:29:142024-01-12 06:29:14

Judging History

你现在查看的是最新测评结果

  • [2024-01-12 06:29:14]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:35808kb
  • [2024-01-12 06:29:14]
  • 提交

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<long long,long long>> query[MAXN];

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 ans=-LLONG_MAX;
    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);
        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+1)-sol(l);
            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++)
    {
        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;
        for (long long i=1;i<=n;i++) query[i].clear();
        vector<long long> vec;
        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[i-1].push_back({0,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[poz2].push_back({poz1,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[n-1].push_back({poz+1,niz[i]});
        }
        query[n-1].push_back({0,mex_niza});
        answer_queries(n);
    }
}


詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 35808kb

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
5
3
4
3
7
5
4
4
5
3
3
6
6
7
5
7
6
5
5
6
2
6
8
7
2
5
5
5
2
3
3
4
5
5
3
7
3
3
5
6
1
4
5
3
3
3
8
6
6
5
7
4
4
5
5
6
6
6
3
7
3
6
2
3
7
6
6
6
7
4
3
5
3
4
6
3
4
6
6
3
5
5
9
4
4
7
5
4
5
2
0
5
6
6
8
4
3
4
5
5
5
7
7
3
2
6
6
3
5
4
4
5
6
6
6
6
7
7
4
5
7
4
7
4
7
6
6
6
5
4
2
5
4
2
3
6
5
3
6
5
5
4
3
5
6
6
6
...

result: