HDU 5722 Jewelry

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.
 


问恰好有某个数字出现x次的区间有多少个

先把所给数字排序,并记录下位置。

然后来统计有哪些区间满足条件。

可以找到很多个左端点在[l,r]右端点在[ll,rr]的区间,然后求并集,就是答案

类似于矩形面积并,用扫描线+线段树搞定

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const int N = 1e5 + 10;
const int mod = 998244353;
const int INF = 0x7FFFFFFF;
int T, n, m, tot;
LL ans;

struct point
{
    int x, y;
    point(int x = 0, int y = 0) :x(x), y(y) {};
    bool operator<(const point&a)const { return x == a.x ? y < a.y : x < a.x; }
}a[N];

struct seg
{
    int h, l, r;
    int k;
    bool operator <(const seg&a)const
    {
        return h == a.h ? k > a.k: h < a.h;
    }
}p[N * 2];

struct ST
{
    int dis[N * 4], sum[N * 4];
    void build(int x, int l, int r)
    {
        sum[x] = dis[x] = 0;
        if (l == r) return;
        else
        {
            int mid = l + r >> 1;
            build(x + x, l, mid);
            build(x + x + 1, mid + 1, r);
        }
    }
    void get(int x, int l, int r)
    {
        if (sum[x]) dis[x] = r - l + 1;
        else
        {
            if (l == r) dis[x] = 0;
            else
            {
                dis[x] = dis[x << 1] + dis[x << 1 | 1];
            }
        }
    }
    void insert(int x, int l, int r, int ll, int rr, int v)
    {
        if (ll <= l && r <= rr)
        {
            sum[x] += v;
            if (!sum[x]) 
            {
                dis[x] = dis[x + x] + dis[x + x + 1];
            }
        }
        else
        {
            int mid = l + r >> 1;
            if (sum[x])
            {
                sum[x << 1] += sum[x];
                sum[x << 1 | 1] += sum[x];
                dis[x << 1] = mid - l + 1;
                dis[x << 1 | 1] = r - mid;
                sum[x] = 0;
            }

            if (ll <= mid) insert(x + x, l, mid, ll, rr, v);
            if (rr > mid) insert(x + x + 1, mid + 1, r, ll, rr, v);

            sum[x] = min(sum[x << 1], sum[x << 1 | 1]);
            sum[x << 1] -= sum[x];
            get(x << 1, l, mid);
            sum[x << 1 | 1] -= sum[x];
            get(x << 1 | 1, mid + 1, r);
            if (sum[x]) dis[x] = r - l + 1;
            else dis[x] = dis[x + x] + dis[x + x + 1];
        }
    }
}st;

void insert(int l, int r, int ll, int rr)
{
    p[tot].h = l; p[tot].l = ll;  p[tot].r = rr;
    p[tot++].k = 1;
    p[tot].h = r; p[tot].l = ll;  p[tot].r = rr;
    p[tot++].k = -1;
}

void solve()
{
    sort(p, p + tot);
    ans = 0;
    st.build(1, 1, n);
    for (int i = 0, j; i < tot; i = j)
    {
        ans += 1LL * (p[i].h - p[i - 1].h - 1)*st.dis[1];
        j = i;
        for (; p[j].h == p[i].h&&p[j].k > 0; j++)
        {
            st.insert(1, 1, n, p[j].l, p[j].r, 1);
        }
        ans += st.dis[1];
        for (; p[j].h == p[i].h&&p[j].k < 0; j++)
        {
            st.insert(1, 1, n, p[j].l, p[j].r, -1);
        }
    }
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        tot = 0;
        scanf("%d%d", &n, &m);
        rep(i, 1, n) scanf("%d", &a[i].x), a[i].y = i;
        sort(a + 1, a + n + 1);
        a[0].x = a[n + 1].x = -1;
        rep(i, 1, n)
        {
            if (i + m - 1 <= n&&a[i + m - 1].x == a[i].x)
            {
                int l, r, ll = a[i].y, rr = a[i + m - 1].y;
                if (a[i].x == a[i - 1].x) l = a[i - 1].y + 1; else l = 1;
                if (a[i + m - 1].x == a[i + m].x) r = a[i + m].y - 1; else r = n;
                insert(l, ll, rr, r);
            }
        }
        solve();
        printf("%I64d\n", ans);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值