Cod sursa(job #2467065)

Utilizator Vlad.Vlad Cristian Vlad. Data 3 octombrie 2019 17:26:50
Problema Secventa 5 Scor 90
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <fstream>
#include <iostream>
#include <unordered_map>
#define MAX_N (1 << 20) + 1
using namespace std;
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};
InParser fin("secv5.in");
ofstream fout("secv5.out");
int n, u, l;
unordered_map<unsigned int, unsigned int> mapU, mapL;
unsigned int v[MAX_N];
long long con;
void citire() {
    fin >> n >> l >> u;
    int nr;
    for (int i = 0; i < n; ++i) {
        fin >> nr;
        v[i] = nr;
    }
}
int main()
{
    citire();
    // poz1 -> i l-1 nr distincte
    // poz2 -> i u nr distincte
    long long poz1 = 0, poz2 = 0;
    for (int i = 0; i < n; ++i) {
        mapL[v[i]]++;
        mapU[v[i]]++;
        while (mapL.size() >= l) {
            mapL[v[poz1]]--;
            if (mapL[v[poz1]] == 0) {
                mapL.erase(v[poz1]);
            }
            poz1++;
        }
        while (mapU.size() > u) {
            mapU[v[poz2]]--;
            if (mapU[v[poz2]] == 0) {
                mapU.erase(v[poz2]);
            }
            poz2++;
        }
        con += i - poz2;
        con -= i - poz1;
    }
    fout << con << '\n';
    return 0;
}