Cod sursa(job #2730373)

Utilizator FrostfireMagirescu Tudor Frostfire Data 26 martie 2021 10:44:38
Problema Distincte Scor 35
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#define NMAX 100000
#define ll long long
#define f first
#define s second

using namespace std;

ifstream fin("distincte.in");
ofstream fout("distincte.out");

int n, k, m, nxt[NMAX+10], pr[NMAX+10], v[NMAX+10];
ll ans[NMAX+10], aib[NMAX+10];
vector <pair <int, int> > q[NMAX+10];

void update(int poz, int val)
{	while(poz <= n)
		{	aib[poz] += (ll)val;
			int lastBit = poz & (-poz);
			poz += lastBit;
		}
}

ll query(int poz)
{	ll ans = 0;
	while(poz)
		{	ans += aib[poz];
			int lastBit = poz & (-poz);
			poz -= lastBit;
		}
	return ans;
}

int main()
{
	fin >> n >> k >> m;
	for(int i=1; i<=n; i++)
		{	fin >> v[i];
			if(pr[v[i]])
				nxt[pr[v[i]]] = i;
			else
				update(i, v[i]);
			pr[v[i]] = i;
		}
	for(int i=1; i<=m; i++)
		{	int x, y;
			fin >> x >> y;
			q[x].push_back({y, i});
		}
	for(int i=1; i<=n; i++)
		{	for(auto u : q[i])
				ans[u.s] = query(u.f);
			update(i, -v[i]);
			if(nxt[i])
				update(nxt[i], v[i]);
		}
	for(int i=1; i<=m; i++)
		fout << ans[i] << '\n';
	return 0;
}