Necklace HDU - 3874

题目链接:点我


    Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful value of some interval x,yx,y as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6. 

    Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals L,RL,R (1<=L<=R<=N) and you must tell her F(L,R) of them.

Input

    The first line is T(T<=10), representing the number of test cases. 
      For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.

Output

    For each query, output a line contains an integer number, representing the result of the query.

Sample Input

2
6
1 2 3 4 3 5
3
1 2
3 5
2 6
6
1 1 1 2 3 5
3
1 1
2 4
3 5

Sample Output

3
7
14
1
3
6

题意:

求一段区间内所有的数的总和,但是相同的数只能计算一次.

思路:

树状数组 + 离线查询.因为我们已经知道了将要进行的查询,那么我们将我们要查询的操作按照右端点的值进行升序排序,同时开一个last数组记录,每个值出现的位置,每次查询时,如果当前值不在初始位置,那么我们将值去掉,并且更新值为当前位置.

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;

const int maxn = 1e6+10;
typedef long long LL;
int a[50000+10];
LL ans[200000+10];
LL s[maxn];
int last[maxn];//记录值的位置
int n,m;
struct ss{
    int x,y,z;
    bool operator < (const ss &p){
    return y<p.y;
}
}q[200000+10];

int lowbit(int x){
    return x&-x;
}

void add(int x, int v){
    while(x<=n){
        s[x] += v;
        x += lowbit(x);
    }
}

LL sum(int x){
    LL ans = 0;
    while(x>0){
        ans += s[x];
        x -= lowbit(x);
    }
    return ans;
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        memset(s,0,sizeof(s));
        memset(last,0,sizeof(last));
        scanf("%d", &n);
        for(int  i = 1; i <= n; ++i){
            scanf("%d",&a[i]);
            add(i,a[i]);
            if(!last[a[i]])
                last[a[i]] = i;//初始化每个值第一次出现的位置
        }scanf("%d", &m);
        for(int i = 1; i <= m; ++i){
            scanf("%d %d", &q[i].x, &q[i].y);
            q[i].z = i;
        }sort(q + 1, q + 1 + m);
        int l = 1;
        for(int i = 1; i <= m; ++i){
            for(int j = l; j <= q[i].y ;++j)
            if(last[a[j]] != j){//如果值不在当前位置,那么就删去
                add(last[a[j]], -a[j]);
                last[a[j]] = j;//更新值为当前位置
            }
            l = q[i].y;
             ans[q[i].z] = sum(q[i].y) - sum(q[i].x - 1);
        }
        for(int i = 1; i <= m; ++i)
            printf("%lld\n",ans[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值