HDU3874 Necklace[树状数组]



E - Necklace
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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,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,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
 


题意:

给你n个数   然后给m个范围   求出范围内不重复数字的和


一开始一点思路都没有

如果直接把那数组直接建树的话  重复的要剔除就要花很长的时间

然后只能从m输入范围里面入手

把m的输入全部保存下来  然后把r从小到大排序

然后开始建树  每次update的时候 范围都比当前的r要小或者相等就可以了

然后注意 前面出现过的 要把他删去之后  更新当前位置 然后在update一次



#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
const int N=5e4+10;
const int M=1e6+10;
int num[N];
long long bit[N],ans[4*N];
int mark[M];
int n,m;
struct node
{
    int l,r,id;
}root[4*N];
bool cmp(const node &a,const node &b)
{
    return a.r<b.r;
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int val)
{
    while (x<=n)
    {
        bit[x]+=(long long)val;
        x+=lowbit(x);
    }
}
long long query(int x)
{
    long long sum=0;
    while (x>0)
    {
        sum+=bit[x];
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        memset(bit,0,sizeof(bit));
        memset(mark,-1,sizeof(mark));
        scanf("%d",&n);
        for (int i=1 ; i<=n ; i++)
            scanf("%d",&num[i]);
        scanf("%d",&m);
        for (int i=0 ; i<m ; i++)
        {
            scanf("%d%d",&root[i].l,&root[i].r);
            root[i].id=i;
        }
        sort(root,root+m,cmp);
        int s=1;
        for (int i=0 ; i<m ; i++)
        {
            while (s<=root[i].r)
            {
                if (mark[num[s]]==-1)//如果 num[s]没出现过 就标记
                {
                    mark[num[s]]=s;
                    update(s,num[s]);
                }
                else
                {
                    update(mark[num[s]],-num[s]);// 把前面出现过的删去
                    mark[num[s]]=s;
                    update(s,num[s]);//替换成新的位置
                }
                s++;
            }
            ans[root[i].id]=query(root[i].r)-query(root[i].l-1);
        }
        for (int i=0 ; i<m ; i++)
            printf("%lld\n",ans[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值