QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#650343 | #7685. Barkley II | yld | WA | 120ms | 5792kb | C++20 | 2.7kb | 2024-10-18 14:43:35 | 2024-10-18 14:43:36 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define lowbit(x) (x&(-x))
using namespace std;
const int MAXN=5e5+5;
int n,m,num=0;
int a[MAXN],cnt[MAXN],belong[MAXN];
struct node{
int l,r;
bool operator <(node b)const{return belong[l]^belong[b.l]?belong[l]<belong[b.l]: (belong[l]&1)?r<b.r:r>b.r;}
};
struct Fenwick{
int n;
vector<int> tr;
Fenwick(int n):n(n),tr(n){}
void modify(int x,int c){
for(int i=x+1;i<=n;i+=lowbit(i)) tr[i-1]+=c;
}
int query(int x){
int res=0;
for(int i=min(x+1,n);i;i-=lowbit(i)) res+=tr[i-1];
return res;
}
};
void solve()
{
cin>>n>>m;
vector<int> X;
for(int i=1;i<=n;i++)
{
cin>>a[i];
X.push_back(a[i]);
X.push_back(a[i]+1);
}
X.push_back(1);
sort(X.begin(),X.end());
X.erase(unique(X.begin(),X.end()),X.end());
int siz=X.size();
vector<vector<int>> pos(siz+1);
for(int i=1;i<=n;i++)
{
a[i]=lower_bound(X.begin(),X.end(),a[i])-X.begin()+1;
pos[a[i]].push_back(i);
}
vector<node> q(1);
for(int i=1;i<=siz;i++)
{
if(pos[i].size())
{
if(pos[i][0]-1>=1) q.push_back({1,pos[i][0]-1});
if(pos[i].back()+1<=n) q.push_back({pos[i].back()+1,n});
}
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)
q.push_back({pos[i][j]+1,pos[i][j+1]-1});
}
}
q.push_back({1,n});
int t=sqrt((int)q.size());
for(int i=1;i<q.size();i++)
belong[i]=i/t+1;
sort(q.begin()+1,q.end());
Fenwick tr(siz+1);
auto add=[&](int x)
{
cnt[a[x]]++;
if(cnt[a[x]]==1)
{
num++;
tr.modify(a[x],1);
}
};
auto del=[&](int x)
{
cnt[a[x]]--;
if(!cnt[a[x]])
{
num--;
tr.modify(a[x],-1);
}
};
auto getmin=[&]()
{
int T=sqrt(siz)+1,pos=0;
for(int i=T;i>=0;i--)
{
int tmp=pos+(1<<i);
if(tmp>siz) continue;
if(tr.query(tmp)!=tmp) continue;
pos=tmp;
}
return pos;
};
int L=1,R=0,ans=-1;
for(int i=1;i<q.size();i++)
{
auto [l,r]=q[i];
while(R<r) add(++R);
while(L>l) add(--L);
while(R>r) del(R--);
while(L<l) del(L++);
int minn=getmin()+1;
ans=max(ans,num-minn);
}
cout<<ans<<endl;
}
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: 5792kb
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: 120ms
memory: 5760kb
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 7 7 6 6 9 8 10 9 9 9 9 9 9 11 10 12 11 11 10 10 10 10 10 11 11 13 11 12 11 12 11 11 11 11 11 12 11 11 11 11 11 11 11 11 12 11 11 11 12 12 11 11 11 11 11 11 12 11 12 12 12 12 12 12 12 12 13 12 12 12 13 12 12 12 12 12 12 12 12 12 12 12 12 12 13 12 12 12 12 12 12 12 12 12 12 12 14 13 13 13 13 13 13 1...
result:
wrong answer 2nd numbers differ - expected: '5', found: '7'