hdu3333-Turing Tree-(线段树+离散化处理)

Turing Tree

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


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
 
翻译:t组测试,给n个数,q个查询,查询第i个数到第j个数之间的不重复的数的和是多少?
解题过程:
首先把所有查询区间记录下来,然后按照区间的右值排序,接着从左到右把每一个数更新到线段树中,并记录它出现的位置。如果一个数已经出现过,那么我们就把他上次出现的位置的值置为0,并更新它出现的位置。因为查询区间是按右值排序的,所以当前区间的左值要么和之前一样要么比之前的要大,因此把过去重复出现的数字置为0不会影响结果。当更新到某个区间的右值时,我们就查询一次该区间的答案,并把答案记录到对应的地方。
  1 #include <iostream>
  2 #include<stdio.h>
  3 #include <algorithm>
  4 #include<string.h>
  5 #include<cstring>
  6 #include<math.h>
  7 #include<map>
  8 #define inf 0x3f3f3f3f
  9 #define ll long long
 10 using namespace std;
 11 const int maxx=300010;
 12 int n,m;
 13 
 14 struct node
 15 {
 16     int l;
 17     int r;
 18     int idx;
 19     ll ans;
 20 };
 21 node q[100005];
 22 int a[maxx];
 23 ll sum[maxx*4];
 24 
 25 
 26 void update(int pos,int a,int l,int r,int rt)
 27 {
 28     if(l==r)///拼死只为找到pos的位置,然后把最底层的sum[rt]改为a
 29     {
 30         sum[rt]=a;return;
 31     }
 32     int mid = (l+r)/2;
 33     if(pos<=mid) update(pos,a,l,mid,rt*2);
 34     else update(pos,a,mid+1,r,rt*2+1);
 35     sum[rt]=sum[rt*2]+sum[rt*2+1];///每次只把一个a[i]累加到sum数组里
 36 }
 37 
 38 ll query(int L,int R,int l,int r,int rt)
 39 {
 40     if(L<=l && r<=R)
 41         return sum[rt];
 42     int mid=(r+l)/2;
 43     ll res=0;
 44     if(L<=mid)
 45         res+=query(L,R,l,mid,rt*2);
 46     if(R>=mid+1)
 47         res+=query(L,R,mid+1,r,rt*2+1);
 48     return res;
 49 }
 50 
 51 bool cmp1(node p1,node p2)
 52 {
 53     if(p1.r!=p2.r)
 54         return p1.r<p2.r;
 55     else
 56         return p1.l<p2.l;
 57 }
 58 
 59 bool cmp2(node p1,node p2)
 60 {
 61     return p1.idx<p2.idx;
 62 }
 63 
 64 int main()
 65 {
 66     int t;
 67     scanf("%d",&t);
 68     while(t--)
 69     {
 70         memset(q,0,sizeof(q));
 71         memset(a,0,sizeof(a));
 72         memset(sum,0,sizeof(sum));
 73         scanf("%d",&n);
 74         for(int i=1;i<=n;i++)
 75             scanf("%d",&a[i]);
 76 
 77         scanf("%d",&m);
 78         for(int i=1;i<=m;i++)
 79             scanf("%d %d",&q[i].l,&q[i].r),q[i].idx=i;
 80         sort(q+1,q+m+1,cmp1);
 81         map<int,int>vis;
 82         int j=1;
 83         for(int i=1;i<=m;i++)
 84         {
 85             while(j<=q[i].r) ///j是一个指针,指示原数组的下标,vis[ a[i] ]则是对应a[j]这个值上次出现的位置
 86             {
 87                 if( !vis[ a[j] ])///如果a[j]这个值没有出现过
 88                 {
 89                     update(j,a[j],1,n,1);
 90                     vis[ a[j] ]=j;
 91                 }
 92                 else
 93                 {
 94                     update(j,a[j],1,n,1);///先日常把a[j]累加到线段树里
 95                     update(vis[ a[j] ],0,1,n,1);///然后把上一个a[j]改成0,改动这个影响了哪些sum的值。
 96                     vis[ a[j] ]=j;///更新a[j]出现的位置。
 97                 }
 98                 j++;
 99             }
100             q[i].ans=query( q[i].l, q[i].r, 1,n,1 );
101         }
102         sort(q+1,q+m+1,cmp2);
103         for(int i=1;i<=m;i++)
104             printf("%lld\n",q[i].ans);
105     }
106     return 0;
107 }

 

 

posted @ 2019-02-14 15:30  守林鸟  阅读(340)  评论(0编辑  收藏  举报