HDU3333-Turing Tree

Turing Tree

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

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

题意:求一个区间内不重复数字的和
解题思路:离散化,离线处理
首先把所有查询区间记录下来,然后按照区间的右值排序,接着从左到右把每一个数更新到线段树中,并记录它出现的位置。如果一个数已经出现过,那么我们就把他上次出现的位置的值置为0,并更新它出现的位置。因为查询区间是按右值排序的,所以当前区间的左值要么和之前一样要么比之前的要大,因此把过去重复出现的数字置为0不会影响结果。当更新到某个区间的右值时,我们就查询一次该区间的答案,并把答案记录到对应的地方。最后把区间查询的答案按照输入顺序输出即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;

int n;
const int MAX=30005;
long long int a[MAX],sum[MAX<<2],ans[100009];

struct node
{
    int l,r,number;
}x[MAX<<2];

int cmp(node a,node b)
{
    if(a.r!=b.r) return a.r<b.r;
    else return a.l<b.l;
}

void updata(int pos,int a,int l,int r,int k)
{
    if(l==r)
    {
        sum[k]=a;return ;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) updata(pos,a,l,mid,k<<1);
    else updata(pos,a,mid+1,r,k<<1|1);
    sum[k]=sum[k<<1]+sum[k<<1|1];
}

long long int query(int l,int r,int ll,int rr,int k)
{
    if(ll>=l&&rr<=r) return sum[k];
    long long int cnt=0;
    int mid=(ll+rr)>>1;
    if(l<=mid) cnt+=query(l,r,ll,mid,k<<1);
    if(mid<r) cnt+=query(l,r,mid+1,rr,k<<1|1);
    return cnt;
}

int main()
{
    int t,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d",&x[i].l,&x[i].r);
            x[i].number=i;
        }
        sort(x+1,x+m+1,cmp);
        memset(sum,0,sizeof sum);
        map<long long int,int>visit;
        int j=1;
        for(int i=1;i<=m;i++)
        {
            while(j<=x[i].r)
            {
                if(!visit[a[j]])
                {
                    updata(j,a[j],1,n,1);
                    visit[a[j]]=j;
                }
                else
                {
                    updata(j,a[j],1,n,1);
                    updata(visit[a[j]],0,1,n,1);
                    visit[a[j]]=j;
                }
                j++;
            }
            ans[x[i].number]=query(x[i].l,x[i].r,1,n,1);
        }
        for(int i=1;i<=m;i++)
            printf("%I64d\n",ans[i]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值