hdu 5722 线段树+扫描线



链接:戳这里


Jewelry

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Problem Description
After all the difficulties, Psyche and Cupid are finally getting married.

No ordinary pearls can hold Cupid's love for Psyche. So he has collected the Jewelry of Gods at the top of Mount Olympus to make her a chain.

There are n beads on the chain. The i-th bead is of the type Ai. 

Being pretty in appearance and generous in her heart, Psyche decides to give one consecutive part of the chain to her mother.

To meet her mother's particular taste, that part must have at least one type of bead that appears x times exactly.

Psyche wants to know the number of ways to choose a part of the chain for her mother.

Note that two parts [L1,R1] and [L2,R2] are different only if L1≠L2 or R1≠R2.
 
Input
The first line of the input contains an integer T (1≤T≤15), which denotes the number of test cases.


For each test case, the first line contains two integers n,x (1≤n≤105,1≤x≤n).

The second line contains n integers, the i-th integer denotes Ai (0≤Ai≤109).
 
Output
For each test case, print an integer which denotes the number of parts Psyche can choose.
 
Sample Input
2
3 1
1 2 1
4 2
2 3 2 2
 
Sample Output
6
3

Hint

In the first example, all solutions all valid.

In the second example, solution $ [1,3], [2,4], [3,4] $ have a type of beed, 2, that appears twice exactly.
 

题意:

给出n个数ai,问存在多少个区间满足区间内至少存在一个数出现恰好x次


思路:

枚举区间的右边界,找出符合条件的左边界(对应一个区间)

n = 10 , x = 2

pos : 1 2 3 4 5 6 7 8 9 10

val  :  . a  . a a  . .  a  a  . 

i=5 那么满足条件的左区间为[1,2] 

i=8 满足条件的左区间为[3,4]

i=9 满足条件的左区间为[5,5]

显然答案取区间并,怎么快速统计呢?

pre[i] 表示当前位置i,上一个 ai 出现的位置

R[i] 表示当前位置为i,已经出现了x个相同的数,R[i]=i-x出现的位置

L[i] 表示当前位置为i,已经出现了x+1个相同的数,L[i]=i-x-1出现的位置

具体的看代码吧


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int T;
int a[100100],b[100100];
int n,X;
ll sum[500100],lazy[500100];
void build(int root,int l,int r){
    sum[root]=0;
    lazy[root]=0;
    if(l==r) return ;
    int mid=(l+r)/2;
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
}
void update(int root,int l,int r,int x,int y,int v){
    //printf("l=%d r=%d x=%d y=%d\n",l,r,x,y);
    if(x==l && y==r) {
        lazy[root]+=v;
        if(lazy[root]) sum[root]=(r-l+1);
        else if(l==r) sum[root]=0;
        else sum[root]=sum[root*2]+sum[root*2+1];
        return ;
    }
    int mid=(l+r)/2;
    if(y<=mid) update(root*2,l,mid,x,y,v);
    else if(x>mid) update(root*2+1,mid+1,r,x,y,v);
    else {
        update(root*2,l,mid,x,mid,v);
        update(root*2+1,mid+1,r,mid+1,y,v);
    }
    if(lazy[root]) sum[root]=r-l+1;
    else sum[root]=sum[root*2]+sum[root*2+1];
}
int L[100100],R[100100],pre[100100];
vector<int> V[100100];
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&X);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
        sort(b+1,b+n+1);
        int cnt=unique(b+1,b+n+1)-(b+1);
        for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+cnt+1,a[i])-b;
        mst(L,0);mst(R,0);mst(pre,0);
        for(int i=1;i<=cnt;i++) V[i].clear();
        for(int i=1;i<=n;i++){
            int x=a[i],num=V[a[i]].size();
            if(num>0) pre[i]=V[x][num-1];
            V[x].push_back(i);
            num++;
            if(num>=X) R[i]=V[x][num-X];
            if(num>=X+1) L[i]=V[x][num-X-1];
        }
        //for(int i=1;i<=n;i++) printf("%d %d %d\n",L[i],R[i],pre[i]);
        build(1,1,n);
        ll ans=0LL;
        for(int i=1;i<=n;i++){
            if(pre[i] && R[pre[i]]) update(1,1,n,L[pre[i]]+1,R[pre[i]],-1);
            if(R[i]) update(1,1,n,L[i]+1,R[i],1);
            ans+=1LL*sum[1];
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
/*
10
4 2
2 3 2 2
*/


R[i] 表示当前位置为i,已经出现了x个相同的数,R[i]=i-x出现的位置
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值