Cod sursa(job #2450535)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 23 august 2019 16:48:41
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 2.27 kb
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007

using namespace std;

typedef long long ll;


int add(int a, int b)
{
    ll x = a+b;
    if(x >= mod)
        x -= mod;
    if(x < 0)
        x += mod;
    return x;
}
ll mul(ll a, ll b)
{
    return (a*b) % mod;
}

ll pw(ll a, ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b & 1)
            ans = (ans * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans;
}


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;
	}
}f("secv5.in");
ofstream g("secv5.out");

unsigned n, L, R, v[(1<<20) + 2];
unordered_map<unsigned, int>mp;
ll seq(int mx)
{
    mp.clear();
    int st = 1;
    int dr = 1;
    int countt = 0;
    ll ans = 0;
    while(st <= n)
    {
        while(dr <= n && countt <= mx)
        {
            if(mp[v[dr]] == 0 && countt == mx)
                break;
            if(mp[v[dr]] == 0)
                ++countt;
            ++mp[v[dr]];
            ++dr;
        }
        ans += dr - st;
        --mp[v[st]];
        if(mp[v[st]] == 0)
            --countt;
        ++st;
    }
    return ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    f >> n >> L >> R;
    for(int i = 1; i <= n; ++i)
        f >> v[i];
    g << seq(R) - seq(L-1) << '\n';
    return 0;
}