QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#257764#7685. Barkley IIlyzqsRE 0ms5476kbC++201.7kb2023-11-19 12:39:252023-11-19 12:39:25

Judging History

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

  • [2023-11-19 12:39:25]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:5476kb
  • [2023-11-19 12:39:25]
  • 提交

answer

#include <bits/stdc++.h>
#define il inline
#define ll long long
#define int ll
#define Max 500005
using namespace std;
il ll read()
{
	char c=getchar();
	ll x=0,f=1;
	while(c>'9'||c<'0')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
#define lowbit(x) x&-x
int n,m,a[Max];
int t[Max];
void ins(int x,int k)
{
	for(int i=x;i<=n;i+=lowbit(i)) t[i]+=k;
}
int query(int x)
{
	int res=0;
	for(int i=x;i;i-=lowbit(i)) res+=t[i];
	return res;
}
signed main()
{
	int T=read();
	while(T--)
	{
		n=read(),m=read();
		vector<vector<int>> p(n+1); 
		for(int i=1;i<=n;i++) 
		{
			t[i]=0;
			a[i]=read();
			if(a[i]<=n) p[a[i]].push_back(i);
		}
		vector<array<int,3>> qry;
		int tot=0;
		for(int i=1;i<=n;i++)
		{
			if(!p[i].size()) continue;
			int l=1;
			for(auto r:p[i])
			{
				if(l<r) qry.push_back({r-1,l,tot++});
				l=r+1;
			}
			if(l<=n) qry.push_back({n,l,tot++});
		}
		sort(qry.begin(),qry.end());
		vector<int> vst(n+1,0);
		vector<array<int,2>> ans;
		int nw=0;
		for(int i=1;i<=n;i++)
		{
			if(vst[a[i]])
			{
				ins(vst[a[i]],-1);
			}
			ins(i,1);vst[a[i]]=i;
			while(nw<qry.size()&&qry[nw][0]==i)
			{
				int l=qry[nw][1],r=qry[nw][0];
				ans.push_back({qry[nw][2],query(r)-query(l-1)});
			//	cout<<l<<' '<<r<<' '<<query(r)-query(l-1)<<" qwq\n";
				nw++;
			}
		}
		sort(ans.begin(),ans.end());
		tot=0;
		int res=-1;
		for(int i=1;i<=n;i++)
		{
			if(!p[i].size()) continue;
			int l=1;
			for(auto r:p[i])
			{
				if(l<r) res=max(res,ans[tot++][1]-i);
				l=r+1;
			}
			if(l<=n) res=max(res,ans[tot++][1]-i);
		}
		cout<<res<<"\n";
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5476kb

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
Runtime Error

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:


result: