Turing Tree hdu 3333(传说中的图灵树)

Turing Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5934    Accepted Submission(s): 2118


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
 

无直接在线线段树,我们只能写一个离线的.

区间内每一个点一个一个更新到线段树,同时删除之前出现过得点,这样的话对于每一个查询[l,r],当r等于当前更新点的时候你就去查询,这样就是正确答案.

为什么呢:对于每一个点更新到线段树,你紧紧是保留了距离当前点最近的重复点,这就是关键,如果l可以包含这些点,那么只包含最近点就可以了,如果最近的点都包含不了,那么就说明区间[l,r]根本就没有这个点,所以这样进行更新是可行的.

偷了个懒,直接map了

#include<bits/stdc++.h>
using namespace std;
const int M=3e4+10;
long long sv[M],tree[4*M],ans[100009];
struct aa
{
    int l,r,pos;
} qry[100008];
map<int,int>mapped;
bool cmp(const aa &a,const aa &b)
{
    return a.r<b.r;
}
void update(int pos,int l,int r,int val,int now)
{
    if(l>pos||r<pos)
        return ;
    if(l==r)
    {
        tree[now]=val;
        return ;
    }
    int mid=(l+r)/2;
    update(pos,l,mid,val,now*2+1);
    update(pos,mid+1,r,val,now*2+2);
    tree[now]=tree[now*2+1]+tree[now*2+2];
}
long long query(int al,int ar,int l,int r,int now)
{
    if(al>r||ar<l)
        return 0;
    if(al<=l&&ar>=r)
    {
        return tree[now];
    }
    int mid=(l+r)/2;
    return query(al,ar,l,mid,now*2+1)+query(al,ar,mid+1,r,now*2+2);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        mapped.clear();
        memset(tree,0,sizeof(tree));
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%lld",sv+i);
        int q;
        scanf("%d",&q);
        for(int i=0; i<q; i++)
        {
            scanf("%d%d",&qry[i].l,&qry[i].r);
            qry[i].pos=i;
        }

        sort(qry,qry+q,cmp);
        for(int i=1,j=0; i<=n; i++)
        {
            if(mapped[sv[i]]==0)
            {
                mapped[sv[i]]=i;
                update(i,1,n,sv[i],1);

            }
            else
            {
                int pos=mapped[sv[i]];
                update(pos,1,n,0,1);
                update(i,1,n,sv[i],1);
                mapped[sv[i]]=i;
            }
            while(j<q&&qry[j].r==i)
            {
                ans[qry[j].pos]=query(qry[j].l,qry[j].r,1,n,1);
                j++;
            }

        }
        for(int i=0;i<q;i++)
        printf("%lld\n",ans[i]);

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值