HDU #3333

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)



Problem 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
 

 

Author
3xian@GDUT
 

 

Source
 

 

Recommend
lcy
 
题目大意:
查询区间[L, R]内所有不同元素的和。
 
解法:
在线做法我还不清楚,有一个利用树状数组的巧妙的离线做法。
将所有查询[L, R]按 右端点R从小到大排序后,依次处理。
要点是记录一个数 上一次出现的位置,这样就可以将当前的数在上个位置上的计数消除。
这样做不会影响当前的查询,因为当这个数的当前位置离所查询的区间的右端点更近。
 
Implementation:
总体是个two-pointer
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int N(3e4+5), M(1e5+5);

LL bit[N], ans[M];
int pos[N], a[N], b[N], n, q;

void add(int x, int v)
{
    for(; x<=n; bit[x]+=v, x+=x&-x);
}

LL sum(int x)
{
    LL res=0;
    for(; x; res+=bit[x], x-=x&-x);
    return res;
}

struct P
{
    int l, r, id;
    P(int l, int r, int id):l(l),r(r),id(id){}
    P(){}
    bool operator<(const P &b)const{return r<b.r;}
}p[M];


int main()
{
    int T;
    for(cin>>T; T--; )
    {
        cin>>n;
        for(int i=1; i<=n; i++) cin>>a[i], b[i-1]=a[i];
        sort(b, b+n);   //error-prone
        int *e=unique(b, b+n);
        memset(bit, 0, sizeof(bit));
        memset(pos, 0, sizeof(pos));
        cin>>q;
        for(int l, r, i=0; i<q; i++)
        {
            cin>>l>>r;
            p[i]={l, r, i};
        }
        sort(p, p+q);
        for(int i=0, j=1, k; i<q && j<=n; )
        {
            for(; j<=p[i].r; j++)
            {
                int id=lower_bound(b, e, a[j])-b;
                if(pos[id]) add(pos[id], -a[j]);
                pos[id]=j, add(j, a[j]);
            }
            for(k=i; p[k].r==p[i].r; k++)
            {
                ans[p[k].id]=sum(p[k].r)-sum(p[k].l-1);
            }
            i=k;
        }
        for(int i=0; i<q; i++) cout<<ans[i]<<endl;
    }
    return 0;
}

 

 
 

转载于:https://www.cnblogs.com/Patt/p/5410647.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值