HDU 3333 树状数组离线

 G
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again... 

Now given a sequence of N numbers A1, A2, ..., AN and a number of Queries(i, j) (1≤i≤j≤N). For each Query(i, j), you are to caculate the sum of distinct values in the subsequence Ai, Ai+1, ..., Aj.
 

Input

The first line is an integer T (1 ≤ T ≤ 10), indecating the number of testcases below. 
For each case, the input format will be like this: 
* Line 1: N (1 ≤ N ≤ 30,000). 
* Line 2: N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1,000,000,000). 
* Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries. 
* Next Q lines: each line contains 2 integers i, j representing a Query (1 ≤ i ≤ j ≤ N).
 

Output

For each Query, print the sum of distinct values of the specified subsequence in one line.
 

Sample Input

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

Sample Output

       
       
1 5 6 3 6
 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<map>
#define maxn1 30005
#define maxn2 100005
typedef long long LL;
using namespace std;
int l[maxn2],r[maxn2],loc[maxn1],A[maxn1],id[maxn2],n,vis[30007];
LL C[maxn1],ans[maxn2];
int Hash(int x)
{
    return x%30007;
}
inline int lowbit(int x){return x&-x;}
void update(int x,int d)
{
    while(x<=n)
    {
        C[x]+=d;
        x+=lowbit(x);
    }
}
LL Query(int x)
{
    LL sum=0;
    while(x>0)
    {
        sum+=C[x];
        x-=lowbit(x);
    }
    return sum;
}
bool cmp(int i,int j)
{
    return r[i]<r[j];
}
int  main()
{
    int T;
    ios_base::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        cin>>n;
        memset(loc,0,sizeof(loc));
        memset(C,0,sizeof(C));
        //memset(vis,0,sizeof(vis));
        map<int,int>q;
        q.clear();
        for(int i=1;i<=n;i++)
        {
            cin>>A[i];
            update(i,A[i]);
            if(!q[A[i]])
            {
                q[A[i]]=i;
            }
        }
        int m;
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>l[i]>>r[i];
            id[i]=i;
        }
        sort(id+1,id+1+m,cmp);
        int Right=1;
        for (int i = 1; i <= m; ++i)
        {
            int x=id[i];
            while(Right<=r[x])
            {
                if(q[A[Right]]!=Right){
                    update(q[A[Right]],-A[Right]);
                    q[A[Right]]=Right;
                }
                ++Right;
            }
            Right=r[x];
            ans[x]=Query(r[x])-Query(l[x]-1);
        }
        for(int i=1;i<=m;i++)printf("%I64d\n",ans[i]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值