Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MN = 1e5+5;
  5. long long N, M, i, x, hsh, ans, lol, cnt[MN], pw[MN]={1};
  6. map<long long,int> mp;
  7.  
  8. int main(){
  9.     mp[0] ++;
  10.     for(i=1;i<MN;i++) pw[i]=pw[i-1]*100003;
  11.     for(scanf("%lld%lld",&N,&M),i=1;i<=N;i++){
  12.         scanf("%lld",&x);
  13.         cnt[x] ++;
  14.         if(cnt[x] == 1) lol ++;
  15.         if(lol == M){
  16.             hsh = 0;
  17.             for(int j=1;j<=M;j++){
  18.                 cnt[j] --;
  19.                 if(!cnt[j]) lol--;
  20.                 hsh += cnt[j]*pw[j];
  21.             }
  22.         }
  23.         else{
  24.             hsh -= (cnt[x]-1)*pw[x];
  25.             hsh += cnt[x]*pw[x];
  26.         }
  27.         ans += mp[hsh];
  28.         mp[hsh]++;
  29.     }
  30.     printf("%lld\n",ans);
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement