Pagini recente » Cod sursa (job #749429) | Cod sursa (job #1755104) | Cod sursa (job #1638120) | Cod sursa (job #2276255) | Cod sursa (job #1767984)
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int NMAX = 100005;
struct QUERY {
int st, dr, index;
i64 ant;
};
int n, k, m;
queue<int> *nxt;
vector<i64> aib;
vector<QUERY> q;
int v[NMAX];
bool cmp_a(const QUERY &a, const QUERY &b) {
return a.st < b.dr;
}
bool cmp_b(const QUERY &a, const QUERY &b) {
return a.index < b.index;
}
inline int lsb(int arg) {
return arg&-arg;
}
inline i64 aib_query(int pos) {
i64 ant = 0;
while(pos > 0) {
ant+= aib[pos];
pos-= lsb(pos);
}
return ant;
}
inline void aib_update(int pos, i64 val) {
while(pos <= n) {
aib[pos]+= val;
pos+= lsb(pos);
}
}
int main(void) {
ifstream fi("distincte.in");
ofstream fo("distincte.out");
int lst;
QUERY tmp;
fi >> n >> k >> m;
nxt = new queue<int>[k+5];
aib.resize(n+5);
for(int i=1; i<=n; ++i) {
fi >> v[i];
if(nxt[v[i]].empty())
aib_update(i, v[i]);
nxt[v[i]].push(i);
}
tmp.ant = 0LL;
for(int i=0; i<m; ++i) {
fi >> tmp.st >> tmp.dr; //csf...
tmp.index = i;
q.push_back(tmp);
}
sort(q.begin(), q.end(), cmp_a);
lst = 1;
for(int i=0; i<m; ++i) {
for(; lst<q[i].st; ++lst) {
aib_update(nxt[v[lst]].front(), -v[lst]);
nxt[v[lst]].pop();
if(!nxt[v[lst]].empty())
aib_update(nxt[v[lst]].front(), v[lst]);
}
q[i].ant = aib_query(q[i].dr);
}
sort(q.begin(), q.end(), cmp_b);
for(int i=0; i<m; ++i)
fo << q[i].ant << '\n';
return 0;
}