You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.5 KiB

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <unordered_map>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
typedef pair<int, int> PII;
int n, m;
LL a[N];
LL ans[N];
// 树状数组模板
LL c[N];
#define lowbit(x) (x & -x)
void add(int x, LL d) {
for (int i = x; i <= n; i += lowbit(i)) c[i] += d;
}
LL query(int x) {
LL res = 0;
for (int i = x; i; i -= lowbit(i)) res += c[i];
return res;
}
int main() {
// 加快读入
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
unordered_map<int, int> b;
vector<PII> q[N]; // 这是一个二维数组,单元格中是PII
memset(ans, 0, sizeof ans);
memset(c, 0, sizeof c);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> m;
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
q[r].push_back({l, i}); // 以r结尾的有一个查询:{id=i,l}
}
for (int i = 1; i <= n; i++) {
if (b.count(a[i])) add(b[a[i]], -a[i]);
b[a[i]] = i;
add(i, a[i]);
// 枚举以i为结尾的所有查询
for (auto c : q[i]) ans[c.second] = query(i) - query(c.first - 1);
}
// 输出
for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]);
}
return 0;
}