QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#777095#7685. Barkley IIdoyoRE 0ms0kbC++201.8kb2024-11-23 22:42:082024-11-23 22:42:08

Judging History

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

  • [2024-11-23 22:42:08]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-23 22:42:08]
  • 提交

answer

#include <bits/stdc++.h>
#define N 500001
#define FOR(i,j,k) for(int i=j;i<=k;++i)
#define For(i,j,k) for(int i=j;i>=k;--i)
#define int long long
using namespace std;
struct note{long long l,r;int id; int ans;}q[N*2];
vector<int> pos[N];
int work(){
	int i,j,s1,s2,L=1,R=0;
    int n,m,numq=0;
    long long sum = 0;
	cin >> n >> m;
    vector<int> c(n + 1);
    vector<int> cnt(m + 1);
    vector<int> b(n + 1); //belong属于哪个块
    //vector<int> ans(numq + 1);
    s1=sqrt(n);s2=1;
    FOR(i,1,n) pos[i].clear();
	for(i=1,j=1;i<=n;i++,j++){
		if(j==s1+1){j=1;s2++;}
		cin >> c[i]; b[i]=s2;
		pos[c[i]].push_back(i);
	}//分块
    
    FOR(i,1,n){
    	int lft = 1;
    	for(auto pl:pos[i]){
    		++numq;
    		q[numq].id = i;
    		q[numq].l = lft;
    		q[numq].r = pl-1;
    		if(q[numq].l>q[numq].r) --numq;
    		lft = pl+1;
		}
		++numq;
		q[numq].id = i;
		q[numq].l = lft;
		q[numq].r = n;
		if(q[numq].l>q[numq].r) --numq;
	}
    auto cmp=[&](note x,note y) -> bool{
        if(b[x.l]^b[y.l]) return b[x.l]<b[y.l];
        else if(b[x.l]&1) return x.r<y.r;
        else return x.r>y.r;
    }; //奇偶分块
    int ans = 0;
    sort(q+1,q + m + 1,cmp);
	for(i=1;i<=m;i++){
		while(L<q[i].l){
            if(cnt[c[L]] == 1) sum--;
            cnt[c[L++]]--;
        }
		while(L>q[i].l){
            cnt[c[--L]]++;
            if(cnt[c[L]] == 1) sum++;
        }
		while(R<q[i].r){
            cnt[c[++R]]++;
            if(cnt[c[R]] == 1) sum++;
        }
		while(R>q[i].r){
            if(cnt[c[R]]== 1) sum--;
            cnt[c[R--]]--;
        }
		q[i].ans = sum-q[i].id;
		ans = max(q[i].ans,ans);
        //ans[q[i].id] = sum;
	}
	cout<<ans<<'\n';
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--){
		work();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

2
5 4
1 2 2 3 4
5 10000
5 2 3 4 1

output:


result: