CF 190D. Non-Secret Cypher


D. Non-Secret Cypher
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the information the berlanders needed so much.
The captured enemy had an array of positive integers. Berland intelligence have long been aware of the flatland code: to convey the message, which contained a number m, the enemies use an array of integers a. The number of its subarrays, in which there are at least k equal numbers, equals m. The number k has long been known in the Berland army so General Touristov has once again asked Corporal Vasya to perform a simple task: to decipher the flatlanders' message.
Help Vasya, given an array of integers a and number k, find the number of subarrays of the array of numbers a, which has at least k equal numbers.
Subarray a[i... j] (1 ≤ i ≤ j ≤ n) of array a = (a1, a2, ..., an) is an array, made from its consecutive elements, starting from the i-th one and ending with the j-th one: a[i... j] = (ai, ai + 1, ..., aj).
Input
The first line contains two space-separated integers n, k (1 ≤ k ≤ n ≤ 4·105), showing how many numbers an array has and how many equal numbers the subarrays are required to have, correspondingly.
The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — elements of the array.
Output
Print the single number — the number of such subarrays of array a, that they have at least k equal integers.
Please do not use the %lld specifier to read or write 64-bit integers in С++. In is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 2
1 2 1 2

Output
3

Input
5 3
1 2 1 1 3

Output
2

Input
3 1
1 1 1

Output
6

Note
In the first sample are three subarrays, containing at least two equal numbers: (1,2,1), (2,1,2) and (1,2,1,2).
In the second sample are two subarrays, containing three equal numbers: (1,2,1,1,3) and (1,2,1,1).
In the third sample any subarray contains at least one 1 number. Overall they are 6: (1), (1), (1), (1,1), (1,1) and (1,1,1).
題意:n個の要素の数列(n<=4 e 5)を与え、どのくらいのサブ区間が少なくとも1個の数値を満たして少なくともK回現れるかを尋ねる.
解法1:
小さい時から大きい時まで、区間の左端点を列挙して、対応する最も左の区間の右端点を探して、最も左の区間の右端点が単調で減らないことを満たすことを発見することができて、だからアルゴリズムの時間の複雑度はO(2*n)=O(n)です
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair pii;
const int INF =0x3f3f3f3f;
const int maxn=4*100000    ;
int n,K;
int a[maxn+10];
mapmp;

ll solve()
{
   int le=1,ri=0;
   bool ok=false;
   ll ans=0;
   while(ri<=n)
   {
       if(ok){
           ans+=n-ri+1;
           if(--mp[a[le++]]==K-1)  ok=false;
       }
       else {
          if( ++mp[a[++ri]]==K )  ok=true;

       }
   }
   return ans;

}
int main()
{
    while(~scanf("%d%d",&n,&K))
    {
        mp.clear();
        for1(i,n)
        {
           scanf("%d",&a[i]);
        }
        printf("%I64d
",solve() ); } return 0; }

解法2:
解法1に対応して,区間右端を列挙するが,最右左端点は単調で減らないわけではない.
まずすべての数値を離散化します.各値xが何回出現し、k回目に出現した位置を記録する.
右端点がiの場合、その最右左端点R[i](存在を仮定)を求めることを考慮し、
a[i]の値がxである場合、xはm回出現し、m−K+1番目のxが出現する位置posを考慮する.
R[i−1]が存在する場合、R[i]=max(R[i−1],pos);
ans=∑(R[i])
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair pii;
const int INF =0x3f3f3f3f;
const int maxn= 400000   ;

mapmp;
int a[maxn+10];
vectorpos[maxn+10];
int num[maxn+10],n,N,K;
int R[maxn+10];
int ID(int x)
{
    if(mp.count(x))
    {
        return mp[x];
    }
    int k=mp.size();
    mp[x]=k;
    return mp[x];
}


void work()
{
    ll ans=0;
    R[0]=INF;
    N=mp.size();
    for0(i,N)
    {
        num[i]=0;
        pos[i].clear();
    }
    for1(i,n)
    {
        int x=a[i];
        pos[x].push_back(i);
        num[x]++;
        if(num[x]