Jewelry
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 300 Accepted Submission(s): 121
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 .
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) .
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 3HintIn 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.
Source
Recommend
wange2014
题意:给出一个序列,问有多少个区间,其中有至少一种种类的宝珠出现的次数恰好为x次。
解题思路:先预处理出每一个位置的宝珠下一个出现与其同种类的宝珠位置next和上一个出现与其同种类的位置pre。考虑在第i个位置的宝珠,要使其出现恰好x次,可以找到在i之后恰好出现了x次的位置j,那么对于区间[l,r],l∈[pre[i],i],r∈[j,next[j]],都满足题目要求的答案。统计可以把每次可行的区间看作一个矩形,底为[pre[i],i],高位[j,next[j]],那么最后的答案就是这些矩形并的面积
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n,m,cnt;
int a[100009],pre[100009],nt[100009];
map<int,int>mp;
struct node
{
int lx,rx,y,val;
friend bool operator<(node a,node b)
{
return a.y<b.y;
}
}x[2000009];
int sum[4000009],lazy[4000009];
LL ans;
void update(int k,int l,int r,int ll,int rr,int p)
{
if(r<=ll||l>=rr) return ;
if(l>=ll&&r<=rr)
{
lazy[k]+=x[p].val;
if(lazy[k]) sum[k]=r-l;
else sum[k]=sum[k<<1]+sum[k<<1|1];
return ;
}
int mid=(l+r)>>1;
update(k<<1,l,mid,ll,rr,p);
update(k<<1|1,mid,r,ll,rr,p);
if(lazy[k]) sum[k]=r-l;
else sum[k]=sum[k<<1]+sum[k<<1|1];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n+1;i++) pre[i]=0,nt[i]=n+1;
mp.clear();
memset(sum,0,sizeof sum);
memset(lazy,0,sizeof lazy);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
nt[mp[a[i]]]=i;
pre[i]=mp[a[i]];
mp[a[i]]=i;
}
mp.clear();
cnt=0;
for(int i=1;i<=n;i++)
{
if(!mp[a[i]])
{
mp[a[i]]=i;
for(int j=1;j<m;j++) mp[a[i]]=nt[mp[a[i]]];
}
else mp[a[i]]=nt[mp[a[i]]];
if(mp[a[i]]!=n+1)
{
x[cnt].lx=pre[i],x[cnt].rx=i,x[cnt].y=mp[a[i]],x[cnt++].val=1;
x[cnt].lx=pre[i],x[cnt].rx=i,x[cnt].y=nt[mp[a[i]]],x[cnt++].val=-1;
}
}
sort(x,x+cnt);
ans=0;
for(int i=0;i<cnt-1;i++)
{
update(1,0,n+1,x[i].lx,x[i].rx,i);
ans+=1LL*(x[i+1].y-x[i].y)*sum[1];
}
printf("%lld\n",ans);
}
return 0;
}