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

来源:互联网 发布:图解汉诺塔递归算法c 编辑:程序博客网 时间:2024/06/03 07:56

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
231 1 421 22 351 1 2 1 331 52 43 5
 

Sample Output
15636
 

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]);    }}


原创粉丝点击