Cod sursa(job #2546517)

Utilizator stefantagaTaga Stefan stefantaga Data 14 februarie 2020 11:24:26
Problema Secventa 5 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int DIM = (1 << 17);
const int MOD = 214723;

unsigned n, l, u;
long long ans;
int nrst, nrdr;

unsigned v[1048580];
vector <pair <unsigned, int>> st[MOD + 1], dr[MOD + 1];

char nxt() {
  static char buff[DIM];
  static int bp = DIM;
  if(bp == DIM) {
    fread(buff, 1, DIM, stdin);
    bp = 0;
  }
  return buff[bp++];
}

void read(unsigned &x) {
  x = 0;
  static char ch;
  do {
    ch = nxt();
  } while(ch < '0' || '9' < ch);
  do {
    x = x * 10 + ch - '0';
    ch = nxt();
  } while('0' <= ch && ch <= '9');
}

int main() {
  freopen("secv5.in", "r", stdin);
  freopen("secv5.out", "w", stdout);
  read(n), read(l), read(u);
  for(unsigned i = 1; i <= n; i++)
    read(v[i]);
  unsigned j1 = 1, j2 = 1;
  for(unsigned i = 1; i <= n; i++) {
    int ind = v[i] % MOD, ok = 0;
    for(int j = 0; j < st[ind].size(); j++) {
      if(st[ind][j].first == v[i])
        st[ind][j].second++, ok = 1;
    }
    if(ok == 0)
      nrst++, st[ind].push_back({v[i], 1});
    ok = 0;
    for(int j = 0; j < dr[ind].size(); j++) {
      if(dr[ind][j].first == v[i])
        dr[ind][j].second++, ok = 1;
    }
    if(ok == 0)
      nrdr++, dr[ind].push_back({v[i], 1});
    while(nrst >= l) {
      ind = v[j1] % MOD;
      for(int j = 0; j < st[ind].size(); j++) {
        if(st[ind][j].first == v[j1]) {
          st[ind][j].second--;
          if(st[ind][j].second == 0) {
            swap(st[ind][j], st[ind].back());
            st[ind].pop_back();
            nrst--;
          }
          j = st[ind].size();
        }
      }
      j1++;
    }
    while(nrdr > u) {
      ind = v[j2] % MOD;
      for(int j = 0; j < dr[ind].size(); j++) {
        if(dr[ind][j].first == v[j2]) {
          dr[ind][j].second--;
          if(dr[ind][j].second == 0) {
            swap(dr[ind][j], dr[ind].back());
            dr[ind].pop_back();
            nrdr--;
          }
          j = dr[ind].size();
        }
      }
      j2++;
    }
    ans += j1 - j2;
  }
  printf("%lld", ans);
  return 0;
}