Cod sursa(job #1838112)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 31 decembrie 2016 00:29:29
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
#include <cstdio>
#include <unordered_map>
#define MAXN 1050000
#define DIM 1000000

using namespace std;

class InputReader
{
private:
	char buf[DIM];
	int cursor;
	FILE *f;
	void adv()
	{
        if (++cursor == DIM) {
			cursor = 0;
			fread(buf, DIM, 1, f);
        }
	}
public:
	InputReader(char *fname)
	{
		f = fopen(fname, "r");
		cursor = 0;
		fread(buf, DIM, 1, f);
	}
    InputReader &operator>>(unsigned int &x)
    {
		for (; buf[cursor] < '0' || buf[cursor] > '9'; adv());
        for (x = 0; buf[cursor] >= '0' && buf[cursor] <= '9'; adv())
			x = x*10 + buf[cursor] - '0';
		return *this;
    }
};

unsigned int n, L, U;
unsigned int a[MAXN];
long long sol;

long long tot(unsigned int ma)
{
	unordered_map<unsigned int, int> m;
    int ind = 1;
    long long part = 0;
    for (unsigned int i = 1; i <= n; ++i) {
        ++m[a[i]];
        while (m.size() > ma) {
            --m[a[ind]];
            if (!m[a[ind]])
                m.erase(a[ind]);
            ++ind;
        }
        part += i - ind + 1;
    }
    return part;
}

void solve()
{
	sol = tot(U) - tot(L-1);
}

InputReader fin("secv5.in");

int main()
{
	ofstream fout("secv5.out");
    fin >> n >> L >> U;
    for (int i = 1; i <= n; i++)
		fin >> a[i];
	solve();
	fout << sol;

    return 0;
}