首页 > 其他分享 >HDU 5722 Jewelry

HDU 5722 Jewelry

时间:2022-11-09 22:34:59浏览次数:49  
标签:HDU Jewelry her 5722 sum Psyche int include dis

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.  


问恰好有某个数字出现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);
    }
}


标签:HDU,Jewelry,her,5722,sum,Psyche,int,include,dis
From: https://blog.51cto.com/u_15870896/5838891

相关文章

  • HDU 3713 Double Maze
    ProblemDescriptionUnlikesinglemaze,doublemazerequiresacommonsequenceofcommandstosolvebothmazes.Seethefigurebelowforaquickunderst......
  • HDU 3442 Three Kingdoms
    ProblemDescriptionThreeKingdomsisafunnygame.OftenLiuBeiisweakandhastorunaway,sointhegameLiuBeihasaskillcalled"Dunzou".Thist......
  • HDU 4127 Flood-it!
    ProblemDescriptionFlood-itisafascinatingpuzzlegameonGoogle+platform.Thegameinterfaceislikefollows:Atthebeginningofthegame,sys......
  • HDU 2757 Ocean Currents
    ProblemDescriptionForaboatonalargebodyofwater,strongcurrentscanbedangerous,butwithcarefulplanning,theycanbeharnessedtohelpthe......
  • HDU 1252 Hike on a Graph
    ProblemDescription"HikeonaGraph"isagamethatisplayedonaboardonwhichanundirectedgraphisdrawn.Thegraphiscompleteandhasallloops......
  • HDU 3345 War Chess
    ProblemDescriptionWarchessishh'sfavoritegame:Inthisgame,thereisanN*Mbattlemap,andeveryplayerhashisownMovingVal(MV).Ineach......
  • HDU 2660 Accepted Necklace
    ProblemDescriptionIhaveNpreciousstones,andplantouseKofthemtomakeanecklaceformymother,butshewon'tacceptanecklacewhichistooh......
  • HDU 1828 Picture
    ProblemDescriptionAnumberofrectangularposters,photographsandotherpicturesofthesameshapearepastedonawall.Theirsidesareallvertical......
  • HDU 3695 Computer Virus on Planet Pandora
    ProblemDescription    AliensonplanetPandoraalsowritecomputerprogramslikeus.Theirprogramsonlyconsistofcapitalletters(‘A’to‘Z’......
  • HDU 5379 Mahjong tree
    ProblemDescriptionLittlesunisanartist.Todayheisplayingmahjongalone.Hesuddenlyfeelsthatthetreeintheyarddoesn'tlookgood.Sohewant......