码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3333 Turing Tree(离线线段树)

时间:2014-11-14 06:55:49      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   

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
 


题意:求给定区间内不同值的和,重复的计算一次。。询问Q<=100000,考虑离线
做法。将所有的询问保存下来,按照右端点由小到大排序。跟新小于p[i].r的值,对
值进行hash,保存值的位置,相同时,原位置上值更新为0,此值的位置更新的当前位置。。
    1. #include<iostream>  
    2. #include<cstdio>  
    3. #include<cstring>  
    4. #include<algorithm>  
    5. #include<map>  
    6. typedef long long LL;  
    7. using namespace std;  
    8. #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )  
    9. #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )  
    10. #define CLEAR( a , x ) memset ( a , x , sizeof a )  
    11. const int maxn=30005;  
    12. const int maxm=100005;  
    13. LL sum[maxn<<2],a[maxn],ans[maxm];  
    14. struct node{  
    15.     int l,r,index;  
    16.     bool operator<(const node a)const  
    17.     {  
    18.         return r<a.r;  
    19.     }  
    20. }p[maxm];//保存每次询问  
    21. map<LL,int>hash;  
    22. int t,n,m;  
    23. void pushup(int rs)  
    24. {  
    25.     sum[rs]=sum[rs<<1]+sum[rs<<1|1];  
    26. }  
    27. void build(int rs,int l,int r)  
    28. {  
    29.     sum[rs]=0;  
    30.     if(l==r)   return ;  
    31.     int mid=(l+r)>>1;  
    32.     build(rs<<1,l,mid);  
    33.     build(rs<<1|1,mid+1,r);  
    34. }  
    35. void update(int rs,LL c,int x,int l,int r)  
    36. {  
    37.     if(l==r)  
    38.     {  
    39.         sum[rs]=c;  
    40.         return ;  
    41.     }  
    42.     int mid=(l+r)>>1;  
    43.     if(x<=mid)   update(rs<<1,c,x,l,mid);  
    44.     else   update(rs<<1|1,c,x,mid+1,r);  
    45.     pushup(rs);  
    46. }  
    47. LL query(int rs,int x,int y,int l,int r)  
    48. {  
    49.     if(l>=x&&r<=y)  
    50.         return sum[rs];  
    51.     int mid=(l+r)>>1;  
    52.     LL res=0;  
    53.     if(x<=mid)  res+=query(rs<<1,x,y,l,mid);  
    54.     if(y>mid)   res+=query(rs<<1|1,x,y,mid+1,r);  
    55.     return res;  
    56. }  
    57. void solve()  
    58. {  
    59.     int pos=1;  
    60.     REPF(i,1,m)  
    61.     {  
    62.         while(p[i].r>=pos)  
    63.         {  
    64.             if(hash[a[pos]])//若此值出现过,原位置上值更新为0.  
    65.                 update(1,0,hash[a[pos]],1,n);  
    66.             hash[a[pos]]=pos;//更新当前值的位置  
    67.             update(1,a[pos],pos,1,n);pos++;  
    68.         }  
    69. //        cout<<"666   "<<query(1,1,2,1,n)<<endl;  
    70.         ans[p[i].index]=query(1,p[i].l,p[i].r,1,n);  
    71.     }  
    72. }  
    73. int main()  
    74. {  
    75.      scanf("%d",&t);  
    76.      int x,y;  
    77.      while(t--)  
    78.      {  
    79.          hash.clear();  
    80.          scanf("%d",&n);  
    81.          build(1,1,n);  
    82.          REPF(i,1,n)  scanf("%I64d",&a[i]);  
    83.          scanf("%d",&m);  
    84.          REPF(i,1,m)  
    85.          {  
    86.              scanf("%d%d",&p[i].l,&p[i].r);  
    87.              p[i].index=i;  
    88.          }  
    89.          sort(p+1,p+1+m);  
    90.          solve();  
    91.          REPF(i,1,m)  
    92.             printf("%I64d\n",ans[i]);  
    93.      }  
    94.      return 0;  

HDU 3333 Turing Tree(离线线段树)

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/yuyanbian/p/4096328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!