Cod sursa(job #2909646)

Utilizator vlad2009Vlad Tutunaru vlad2009 Data 14 iunie 2022 14:25:56
Problema Secventa 5 Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <fstream>
#include <map>

using namespace std;

const int MAX_N = 1 << 20;
long long a[MAX_N + 1];
map<long long, long long> mp;
long long n, l, u;

long long Count(long long k) {
    mp.clear();
    long long j = 1, cnt = 0;
    long long answer = 0;
    for (int i = 1; i <= n; i++) {
        mp[a[i]]++;
        if (mp[a[i]] == 1) {
            cnt++;
        }
        while (j <= i && cnt > k) {
            if (mp[a[j]] == 1) {
                cnt--;
            }
            mp[a[j]]--;
            j++;
        }
        answer += i - j + 1;
    }
    return answer;
}

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;
	}
};

signed main() {
    InParser fin("secv5.in");
    ofstream fout("secv5.out");
    fin >> n >> l >> u;
    for (int i = 1; i <= n; i++) {
        fin >> a[i];
    }
    fout << Count(u) - Count(l - 1);
    return 0;
}