Cod sursa(job #2677148)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 25 noiembrie 2020 21:27:38
Problema Secventa 5 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.49 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>	
#define debug(x) cerr << #x << " " << x << "\n"
#define debugsp(x) cerr << #x << " " << x << " "

using namespace std;

const int INF = 2e9;
const int N = (1 << 20);

class HashMap {
private:
  const static int MOD = 666013;
  vector <int> table[MOD];

public:
  void Clear() {
    for(int i = 0; i < MOD; i++)
      table[i].clear();
  }

  bool Find(int elem) {
    int buck = elem % MOD;

    for(auto it : table[buck])
      if(it == elem) return true;
    return false;
  }

  void Insert(int elem) {
    int buck = elem % MOD;
    
    table[buck].push_back(elem);
  }

  void Erase(int elem) {
    int buck = elem % MOD;

    for(vector <int> :: iterator it = table[buck].begin(); it != table[buck].end(); it++)
      if(*it == elem) {
        table[buck].erase(it);
        return;
      }
  }

  int operator [](int elem) {
    int buck = elem % MOD;
    int cnt(0);

    for(auto it : table[buck])
      if(it == elem) cnt++;

    return cnt;
  }
};
HashMap mp;

int v[1 + N];
int l, u, n;

long long Solve(int x) {
  //debug(x);
  int p1, p2, dif;
  long long ans;

  mp.Clear();

  p2 = 0;
  ans = 0;
  dif = 0;

  for(p1 = 1; p1 <= n; p1++) {
    //debug(p1);
    while(p2 <= n && dif <= x) {
      ++p2;
      if(!mp.Find(v[p2])) dif++;
      mp.Insert(v[p2]);
    }
    //debug(p2);

    ans += 1LL * (p2 - p1);

    if(mp[v[p1]] == 1) dif--;
    mp.Erase(v[p1]);
  }

  return ans;
}

int main() {
  freopen("secv5.in", "r", stdin);
  freopen("secv5.out", "w", stdout);
  scanf("%d%d%d", &n, &l, &u);

  for(int i = 1; i <= n; i++) 
    scanf("%d", &v[i]);

  printf("%lld\n", Solve(u) - Solve(l - 1));
  return 0;
}