Pagini recente » Cod sursa (job #3278556) | Cod sursa (job #2466068) | Borderou de evaluare (job #3280147) | Cod sursa (job #2469903) | Cod sursa (job #2568710)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
//struct Diapazon {
// int a, b;
//}dio[N];
int last[N], v[N], urmval[N], ans[N];
vector < pair < int, int > > queries[N];
namespace AIB {
int t[N];
void Update(int poz, int val) {
for (; poz < N; poz += poz&(-poz))
t[poz] += val;
}
int Query(int poz) {
int ans(0);
for (; poz > 0; poz -= poz&(-poz))
ans += t[poz];
return ans;
}
}
int main()
{
ifstream fin("distincte.in");
ofstream fout("distincte.out");
ios_base::sync_with_stdio(NULL);
fin.tie(0);
fout.tie(0);
int m, n, k, a, b;
fin >> n >> k >> m;
for (int i = 1; i <= n; ++i) {
fin >> v[i];
if (last[v[i]])
urmval[last[v[i]]] = i;
else
AIB::Update(i, v[i]);
last[v[i]] = i;
}
// for (int i = 1; i <= n; ++i)
// fout << urmval[i] << ' ';
// fout << '\n';
for (int i = 1; i <= m; ++i) {
fin >> a >> b;
queries[a].push_back({b, i});
}
for (int i = 1; i <= n; ++i) {
if (i - 1) {
AIB::Update(i - 1, -v[i - 1]);
if (urmval[i - 1])
AIB::Update(urmval[i - 1], v[i - 1]);
}
for (auto j : queries[i])
ans[j.second] = AIB::Query(j.first);
}
for (int i = 1; i <= m; ++i)
fout << ans[i] << '\n';
return 0;
}