QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#650214#7685. Barkley IIyldWA 124ms5852kbC++203.0kb2024-10-18 14:01:262024-10-18 14:01:28

Judging History

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

  • [2024-10-18 14:01:28]
  • 评测
  • 测评结果:WA
  • 用时:124ms
  • 内存:5852kb
  • [2024-10-18 14:01:26]
  • 提交

answer

#include<bits/stdc++.h>
// #define int long long
#define endl '\n'
using namespace std;
const int MAXN=5e+5;
struct SegmentTree
{
    int l,r,ls,rs;
    int a;
}tr[MAXN*40];
int tot,root[MAXN],lst[MAXN];
int cnt[MAXN],num,belong[MAXN];
int id,a[MAXN];
int n,q;
void clear()
{
    for(int i=1;i<=id;i++) cnt[i]=0;
    id=tot=num=0;
}
inline bool cmp(array<int,3> a,array<int,3> b)
{
	return belong[a[0]]^belong[b[0]] ? belong[a[0]]<belong[b[0]] : (belong[a[0]]&1) ? a[1]<b[1] : a[1]>b[1];
}
int modify(int pre,int l,int r,int pos,int x)
{
    int rt=++tot;
    tr[rt]=tr[pre];
    if(l==r) {tr[rt].a=x;return rt;}
    int mid=l+r>>1;
    if(pos<=mid) tr[rt].ls=modify(tr[pre].ls,l,mid,pos,x);
    else tr[rt].rs=modify(tr[rt].rs,mid+1,r,pos,x);
    tr[rt].a=min(tr[tr[rt].ls].a,tr[tr[rt].rs].a);
    return rt;
}
int query(int rt,int l,int r,int x)
{
    if(!rt || l==r) return lst[l];
    int mid=l+r>>1;
    if(tr[tr[rt].ls].a>=x) return query(tr[rt].rs,mid+1,r,x);
    return query(tr[rt].ls,l,mid,x);
}
inline void add(int x)
{
    cnt[a[x]]++;
    if(cnt[a[x]]==1) num++; 
}
inline void del(int x)
{
    cnt[a[x]]--;
	if(cnt[a[x]]==0) num--;
}
void solve()
{
    cin>>n>>q;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];lst[++id]=a[i];
        lst[++id]=a[i]+1;
    }
    sort(lst+1,lst+id+1);
    id=unique(lst+1,lst+1+id)-lst-1;
    for(int i=1;i<=n;i++)
    {
        a[i]=lower_bound(lst+1,lst+1+id,a[i])-lst;
        root[i]=modify(root[i-1],1,id,a[i],i);
    }
    vector<array<int,3>> ask;
    vector<vector<int>> pos(id+1);
    for(int i=1;i<=n;i++)
        pos[a[i]].push_back(i);
    // for(int i=1;i<=n;i++) cerr<<a[i]<<' ';
    // cerr<<endl;
    // for(int i=1;i<=id;i++)
    // {
    //     for(auto x:pos[i]) cerr<<x<<' ';
    //     cerr<<endl;
    // }
    for(int i=1;i<=id;i++)
    {
        if(pos[i].size()) 
        {
            if(pos[i][0]-1>=1)ask.push_back({1,pos[i][0]-1,i});
            if(pos[i].back()+1<=n) ask.push_back({pos[i].back()+1,n,i});
        }
        if(pos[i].size()>=2)
            for(int j=0;j<pos[i].size()-1;j++)
            {
                if(pos[i][j]+1<=pos[i][j+1]-1)
                ask.push_back({pos[i][j]+1,pos[i][j+1]-1,i});
            }
    }
    sort(ask.begin(),ask.end(),cmp);
    int t=sqrt(id);
    for(int i=1;i<=id;i++) belong[i]=i/t+1;
    int ans=0;
    int L=1,R=0;
    while(ask.size())
    {
        auto [l,r,k]=ask.back();ask.pop_back();
        // ans=max(query(root[r],1,id,l)-k,ans);
        // cout<<l<<' '<<r<<' '<<k<<endl;
        // cout<<query(root[r],1,id,l)<<endl;
        while(L>l) add(--L);
        while(R<r) add(++R);
        while(L<l) del(L++);
        while(R>r) del(R--);
        // cout<<num<<endl;
        ans=max(ans,num-query(root[r],1,id,l));
    }
    cout<<ans<<endl;
    clear();
}
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t=1;cin>>t;
    while(t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5852kb

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
Wrong Answer
time: 124ms
memory: 5616kb

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:

5
1
2
4
2
3
3
3
3
3
4
5
2
3
0
3
3
4
7
6
3
3
1
2
4
4
6
2
3
0
6
1
2
2
2
5
3
3
3
3
2
5
2
1
3
4
2
3
2
7
3
2
4
4
3
2
5
3
2
1
4
3
3
3
2
3
0
6
7
6
2
4
4
3
3
4
0
6
3
3
3
3
4
0
2
1
4
5
4
5
3
1
1
2
1
3
3
5
4
3
4
2
1
4
4
6
3
0
5
4
3
5
4
4
4
4
6
4
5
6
3
4
5
3
4
3
3
3
3
0
3
2
1
2
5
1
2
1
5
4
2
2
4
5
4
3
0
6
6
3
...

result:

wrong answer 1st numbers differ - expected: '6', found: '5'