2 条题解

  • -5
    @ 2024-11-18 13:59:23

    spq使用线段树莽过:

    #include<bits/stdc++.h>
    using namespace std;
    const int N=100005;
    int n,k;
    int x;
    int a[N];
    int cnt=1;
    unordered_map<int,int> m;
    long long sum=0;
    int f[N];
    int t[N];
    
    
    struct node{
    	int l,r;
    	int maxx;
    };
    node tr[400005];
    void build(int p,int l,int r){
    	tr[p].l=l;
    	tr[p].r=r;
    	if(l==r){
    		tr[p].maxx=t[l];
    		return ;
    	}
    	int mid=(l+r)/2;
    	build(p*2,l,mid);
    	build(p*2+1,mid+1,r);
    	tr[p].maxx=max(tr[p*2].maxx,tr[p*2+1].maxx);
    }
    void ch(int p,int x,int s){
    	if(tr[p].l==x&&tr[p].r==x){
    		tr[p].maxx=s;
    		return ;
    	}
    	int mid=(tr[p].l+tr[p].r)/2;
    	if(x<=mid){
    		ch(p*2,x,s);
    	}
    	else{
    		ch(p*2+1,x,s);
    	}
    	tr[p].maxx=max(tr[p*2].maxx,tr[p*2+1].maxx);
    }
    int ask(int p,int l,int r){
    	if(l<=tr[p].l&&r>=tr[p].r){
    		return tr[p].maxx;
    	}
    	int mid=(tr[p].l+tr[p].r)/2;
    	int val=0;
    	if(l<=mid){
    		val=max(ask(p*2,l,r),val);
    	}
    	if(r>mid){
    		val=max(ask(p*2+1,l,r),val);
    	} 
    	return val;
    }
    
    
    int main(){
    	//freopen("sb.txt","r",stdin);
    	scanf("%d",&n);
    	scanf("%d",&k);
    	for(int i=1;i<=n;i++){
    		scanf("%d",&x);
    		if(m[x]==0){
    			m[x]=cnt;
    			cnt++;
    		}
    		a[i]=m[x];
    	}
    	cnt--;
    	int l=1,r=1;
    	t[a[l]]++;
    	build(1,1,cnt);
    	while(l<=n&&r<=n){
    		while(ask(1,1,cnt)<k&&r<=n){
    			r++;
    			t[a[r]]++;
    			ch(1,a[r],t[a[r]]);
    		}
    		if(r>n){
    			break;
    		}
    		sum+=n-r+1;
    		t[a[l]]--;
    		ch(1,a[l],t[a[l]]);
    		l++;
    	}
    	cout<<sum;
    	return 0;
    }
    //
    

    信息

    ID
    44
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    56
    已通过
    10
    上传者