2019 Multi-University Training Contest 10 Make Rounddog Happy (rmq + 分治)

Make Rounddog Happy

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 656    Accepted Submission(s): 306


 

Problem Description

Rounddog always has an array a1,a2,⋯,an in his right pocket, satisfying 1≤ai≤n.

A subarray is a non-empty subsegment of the original array. Rounddog defines a good subarray as a subsegment al,al+1,⋯,ar that all elements in it are different and max(al,al+1,…,ar)−(r−l+1)≤k.

Rounddog is not happy today. As his best friend, you want to find all good subarrays of a to make him happy. In this case, please calculate the total number of good subarrays of a.

 

 

Input

The input contains several test cases, and the first line contains a single integer T (1≤T≤20), the number of test cases.

The first line of each test case contains two integers n (1≤n≤300000) and k (1≤k≤300000).

The second line contains n integers, the i-th of which is ai (1≤ai≤n).

It is guaranteed that the sum of n over all test cases never exceeds 1000000.

 

 

Output

One integer for each test case, representing the number of subarrays Rounddog likes.

 

 

Sample Input

 

2 5 3 2 3 2 2 5 10 4 1 5 4 3 6 2 10 8 4 5

 

 

Sample Output

 

7 31

 

 

Source

2019 Multi-University Training Contest 10

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6730 6729 6728 6727 6726 

 题目大意:给出了一个序列,求满足 max(al,al+1,…,ar)−(r−l+1)≤k. 并且区间不能出现相同的数的区间数。

解题思路:用单调栈处理出每个数为最大值的左右区间,然后处理出每个数能拓展到的左右区间(区间不出现重复的数)。

然后化简上边的式子。发现需要每个左端点,然后去确定右端点。 复杂度不够优秀。

考虑分治的做法。

我们找到当前序列的最大值所在的位置,然后处理跨区间的贡献,然后再分别处理左右区间内部的贡献。

分治下去即可。

#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))

using namespace std;
const int N = 3e5+5;
int n,k;
typedef long long ll;

int a[N];
struct RMQ
{
    int c[N][20];
    int  Max(int x,int y)
    {
        return a[x]>=a[y]?x:y;
    }
    void init()
    {
        for(int i=1; i<=n; i++)c[i][0]=i;

        for(int i=1; i<=20; i++)
        {
            for(int j=1; j+(1<<i)-1<=n; j++)
            {
                c[j][i]=Max(c[j][i-1],c[j+(1<<(i-1))][i-1]);
            }
        }
    }

    int ask(int i,int j)
    {
        int k=log2(j-i+1);
        return Max(c[i][k],c[j-(1<<k)+1][k]);
    }

}rmq;

int L[N],R[N];
int vis[N];
void pre_work()
{
    clr(vis);
    R[n+1]=n;
    for(int i=n;i>=1;i--)
    {
        R[i]=n;
        if(vis[a[i]])
        {
            R[i]=vis[a[i]]-1;
            vis[a[i]]=i;
        }
        else vis[a[i]]=i;
        R[i]=min(R[i],R[i+1]);
    }

    clr(vis);
    for(int i=1;i<=n;i++)
    {
        L[i]=1;
        if(vis[a[i]])
        {
            L[i]=vis[a[i]]+1;
            vis[a[i]]=i;
        }
        else vis[a[i]]=i;
        L[i]=max(L[i],L[i-1]);
    }
}

int dfs(int l,int r)
{
    if(l>r)return 0;
    if(l==r) return a[l]-k<=1;
    int pos=rmq.ask(l,r);
    int llen=pos-l+1;
    int rlen=r-pos;
    int ans=0;
    ///li-i+1 >=a[pos]-k
    ///li >= a[pos]-k+i-1
    if(llen<=rlen)
    {
        for(int i=l; i<=pos; i++)
        {
            int ri=min(R[i],r);
            int li=max(pos,a[pos]-k+i-1);
            if(ri>=li)ans+=ri-li+1;
        }
    }
    else
    {
        for(int i=pos; i<=r; i++)
        {
            int li=max(l,L[i]);
            int ri=min(i-(a[pos]-k)+1,pos);
            if(ri>=li)ans+=ri-li+1;
        }
    }
    return ans+dfs(l,pos-1)+dfs(pos+1,r);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1; i<=n; i++)scanf("%d",&a[i]);
        rmq.init();
        pre_work();
        printf("%d\n",dfs(1,n));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值