HDU5722-Jewelry

216 篇文章 0 订阅
83 篇文章 1 订阅

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  L1L2  or  R1R2 .
 

Input
The first line of the input contains an integer  T   (1T15) , which denotes the number of test cases.

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

The second line contains  n  integers, the  i -th integer denotes  Ai   (0Ai109) .
 

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.
 

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值