Description

 

Input

Output

 

Sample Input

3 2
5 3 1

Sample Output

4
 
做法:用两个指针维护区间合法。
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #define N 100007
 5 #define rep(i,a,b) for(int i=a;i<=b;i++)
 6 using namespace std;
 7 int n,k,ma;
 8 int a[N];
 9 long long ans;
10 int b[N*2];
11 
12 void Init(){
13     scanf("%d%d",&n,&k);
14     rep(i,1,n) scanf("%d",&a[i]),ma=max(ma,a[i]);
15 }
16 
17 int main(){
18     freopen("drink.in","r",stdin);
19     freopen("drink.out","w",stdout);
20     Init();
21     int l=1,r=1;
22     b[a[1]]++;
23     for(;++r<=n;){
24         if (a[r]>k){
25             for(int i=0;i<=ma;i++){
26                 int p=i*a[r]+k;
27                 if (p>ma) break;
28                 for(;b[p];)    b[a[l++]]--;
29             }
30         }
31         b[a[r]]++;
32         ans+=r-l;
33     }
34     printf("%lld",ans+n);
35 }
View Code