Pagini recente » Arhiva de probleme | Cod sursa (job #1344612) | Cod sursa (job #1577025) | Cod sursa (job #2471143) | Cod sursa (job #2694448)
#include <iostream>
#include <fstream>
#include <unordered_map>
const int NMAX = (1 << 20);
int n, qLeft, qRight;
int a[1 + NMAX];
std::unordered_map<int, int> hashmap;
void read() {
std::ifstream in("secv5.in");
in >> n >> qLeft >> qRight;
for (int i = 1; i <= n; ++i)
in >> a[i];
}
int countSeq(int target) {
if (target == 0)
return 0;
hashmap.clear();
int left = 1;
int cnt = 1, ans = 0;
hashmap[a[1]] = 1;
for (int i = 2; i <= n; ++i) {
if (hashmap.count(a[i]) == 0) {
if (cnt < target) {
++cnt;
hashmap[a[i]] = 1;
} else {
while (hashmap[a[left]] != 1) {
--hashmap[a[left]];
ans += i - left;
++left;
}
--hashmap[a[left]];
ans += i - left;
++left;
hashmap[a[i]] = 1;
}
} else {
++hashmap[a[i]];
}
}
while (left <= n) {
ans += n - left + 1;
++left;
}
return ans;
}
int main() {
read();
int left = countSeq(qLeft - 1);
int right = countSeq(qRight);
std::ofstream out("secv5.out");
out << right - left;
return 0;
}