Cod sursa(job #1573636)

Utilizator AlexandraaaaMereu Alexandra Alexandraaaa Data 19 ianuarie 2016 20:27:31
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>

using namespace  std;

vector <long long> V;

int NrSec(int k){
	unordered_map <long long, int> H;
	int i, j;
	i = j = 0;

	while (i < V.size() && j < k){
		H[V[i]]++;
		if (H[V[i]] == 1)
			j++;
		i++;
	}

	int s = i*(i + 1) / 2;

	j = 0;
	while (i < V.size() && j < V.size()){
		H[V[i]]++;
		if (H[V[i]] == 1){
			while (j < V.size() && H[V[j]] > 1){
				H[V[j]]--;
				j++;
			}
			H[V[j]]--;
			j++;
		}
		s += i - j + 1;
		i++;
	}

	return s;
}

int main(){
	ifstream f("secv5.in"); 
	ofstream g("secv5.out");

	int n, l, u, s;

	f >> n >> l >> u;
	for (int i = 0; i < n; ++i){
		int x;
		f >> x;
		V.push_back(x);
	}

	int x, y;
	x = NrSec(u);
	y = NrSec(l - 1);
	s = x - y;

	g << s;

	f.close();
	g.close();

	return 0;
}