Make Rounddog Happy
Problem Description
R o u n d d o g Rounddog Rounddog always has an array a 1 , a 2 , ⋯ , a n a_1,a_2,⋯,a_n a1,a2,⋯,an in his right pocket, satisfying 1 ≤ a i ≤ n 1\leq a_i\leq n 1≤ai≤n.
A subarray is a non-empty subsegment of the original array. R o u n d d o g Rounddog Rounddog defines a good subarray as a subsegment a l , a l + 1 , ⋯ , a r a_l,a_{l+1},⋯,a_r al,al+1,⋯,ar that all elements in it are different and m a x ( a l , a l + 1 , … , a r ) − ( r − l + 1 ) ≤ k max(a_l,a_{l+1},…,a_r)−(r−l+1)\leq k max(al,al+1,…,ar)−(r−l+1)≤k.
R o u n d d o g Rounddog 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 a a.
Input
The input contains several test cases, and the first line contains a single integer T ( 1 ≤ T ≤ 20 ) T (1\leq T\leq 20) T(1≤T≤20), the number of test cases.
The first line of each test case contains two integers n ( 1 ≤ n ≤ 300000 ) n (1\leq n\leq 300000) n(1≤n≤300000) and k ( 1 ≤ k ≤ 300000 ) k (1\leq k\leq 300000) k(1≤k≤300000).
The second line contains n integers, the i-th of which is ai ( 1 ≤ a i ≤ n ) (1\leq a_i\leq n) (1≤ai≤n).
It is guaranteed that the sum of n over all test cases never exceeds 1000000 1000000 1000000.
Output
One integer for each test case, representing the number of subarrays R o u n d d o g Rounddog 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
题意
- 给一个数组,求有多少个子区间 [ l , r ] [l,r] [l,r],使得 m a x ( a l , a l + 1 , … , a r ) − ( r − l + 1 ) ≤ k max(a_l,a_{l+1},…,a_r)−(r−l+1)\leq k max(al,al+1,…,ar)−(r−l+1)≤k且 [ l , r ] [l,r] [l,r]内的数互不相同
题解
- R M Q RMQ RMQ+分治套路题
- 就是预处理 R M Q RMQ RMQ,并且预处理以每一个位置为区间左端点的最右右端点使得区间内的数两两不相同,以及以每一个位置为右端点的最左左端点使得区间内的数两两不相同,然后分治处理就行了,每次枚举一个较小的子区间进行合并,这样期望复杂度是 O ( n log n ) O(n \log n) O(nlogn)的,和上次牛客出的2019牛客第三场G基本差不多
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+10;
#define inf 0x3f3f3f3f
namespace IO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if (p1==pend){
p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
if (pend==p1){IOerror=1;return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline bool read(double &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror) return false;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (ch=='.'){
double tmp=1; ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
}
if (sign)x=-x; return true;
}
template<typename T>
inline bool read(T &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror) return false;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x; return true;
}
inline bool read(char *s){
char ch=nc();
for (;blank(ch);ch=nc());
if (IOerror) return false;
for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
*s=0; return true;
}
};
using namespace IO;
int dpr[maxn],dpl[maxn],cnt[maxn],k;
//st
int n,a[maxn],dp[maxn][20],p[maxn][20],Log[maxn];
void init_st()
{
Log[0]=-1;for(int i=1;i<=n;i++) Log[i]=Log[i>>1]+1;
for(int i=1;i<=n;i++) dp[i][0]=a[i],p[i][0]=i;
for(int j=1;(1<<j)<=n;j++) for(int i=1;i+(1<<j)-1<=n;i++) {
dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
if(dp[i][j-1]>=dp[i+(1<<(j-1))][j-1]) p[i][j]=p[i][j-1];
else p[i][j]=p[i+(1<<(j-1))][j-1];
}
}
int query(int l,int r)
{
int k=Log[r-l+1];
if(dp[l][k]>=dp[r-(1<<k)+1][k]) return p[l][k];
return p[r-(1<<k)+1][k];
}
void init_()
{
for(int i=1;i<=n;i++) cnt[i]=0;
}
long long solve(long long l,long long r)
{
if(r<l) return 0;
return r-l+1;
}
long long get(long long t)
{
return t<=0?1:t;
}
int tot=0;
long long dfs(int l,int r)
{
if(l==r) return a[l]-k<=1;
if(l>r) return 0;
int pos=query(l,r);
long long res=0;
if(pos-l<r-pos) {
for(int i=l;i<=pos;i++) {
res+=solve(max(1LL*pos,i+get(a[pos]-k)-1),min(r,dpr[i]));
}
}else {
for(int i=pos;i<=r;i++) {
res+=solve(max(l,dpl[i]),min(1LL*pos,i-get(a[pos]-k)+1));
}
}
return res+dfs(l,pos-1)+dfs(pos+1,r);
}
int main()
{
//freopen("/Users/wzw/Desktop/ACM/1.in","r",stdin);
int t;read(t);
while(t--) {
read(n);read(k);
for(int i=1;i<=n;i++) read(a[i]);
init_st();
init_();
dpr[n+1]=n;
for(int i=n;i>=1;i--) {
if(cnt[a[i]]) {
dpr[i]=cnt[a[i]]-1;
cnt[a[i]]=i;
}else {
cnt[a[i]]=i;
dpr[i]=n;
}
dpr[i]=min(dpr[i],dpr[i+1]);
}
init_();
for(int i=1;i<=n;i++) {
if(cnt[a[i]]) {
dpl[i]=cnt[a[i]]+1;
cnt[a[i]]=i;
} else {
cnt[a[i]]=i;
dpl[i]=1;
}
dpl[i]=max(dpl[i],dpl[i-1]);
}
printf("%lld\n",dfs(1,n));
}
}