1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> using namespace std; const int maxn = 100005; struct node{ int l,r,id,answer; }q[maxn]; long long cnt; long long a[maxn]; long long col[maxn]; bool cmp(node a,node b) { return col[a.l] == col[b.l]?(a.r < b.r):(a.l < b.l); } long long sum[maxn],b[maxn],num[maxn]; void update(int x, int add) { sum[a[x]] += add; if(sum[a[x]] == 1 && add == 1) { cnt += b[a[x]]; } if(sum[a[x]] == 0 && add == -1) { cnt -= b[a[x]]; } } int main() { int t,n,Q; scanf("%d",&t); while(t--) { cnt = 0; scanf("%d",&n); for(int i = 1; i <= n; ++i) { scanf("%lld",&a[i]); b[i] = a[i]; col[i] = i / (int)sqrt(i) + 1; } sort(b + 1, b + n + 1); int tot = unique(b + 1, b + n + 1) -(b + 1); for(int i = 1 ;i <= n; ++i) { a[i] = lower_bound(b + 1,b + tot + 1 ,a[i]) - b; } scanf("%d",&Q); for(int i = 1; i <= Q; ++i) { scanf("%d %d",&q[i].l ,&q[i].r); q[i].id = i; } sort(q + 1, q + Q + 1,cmp); int l = 1, r = 0; memset(sum,0,sizeof(sum)); for(int i = 1; i <= Q; ++i) { while(l < q[i].l){update(l, -1);l++;} while(l > q[i].l){update(l - 1, 1);l--;} while(r < q[i].r){update(r + 1, 1);r++;} while(r > q[i].r){update(r, -1); r--;} num[q[i].id] = cnt; } for(int i = 1; i <= Q; ++i) { printf("%lld\n",num[i]); } } }
|