天天看点

HDU 3333 Turing Tree(树状数组/线段树+离线+Map)

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

花了很多时间才搞懂的一道题。

主要思路:若所有数字只出现一次,便是普通区间求和。

但是多次出现的话,就要离线后将右端点排序,

更新直到当前右端时,每次用map映射维护数字出现的地点,删去上次出现地点的所有值,更新树,加上这次出现地点更新树,

最后 ans=前r项-前l-1项。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

int num[30100];
struct ques
{
    int l,r;long long ans;int num;
}question[100100];

long long tree[40100];


int cmp(struct ques q,struct ques w)
{
    if(q.r==w.r)
    return q.l<w.l;
    else
    return q.r<w.r;
}

int cmp1(struct ques q,struct ques w)
{
    return q.num<w.num;
}


int main()
{
    int i,j,k,n,t,m;


    scanf("%d",&t);

    while(t--)
    {
        scanf("%d",&n);

        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);

        }

        scanf("%d",&m);

        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&question[i].l,&question[i].r);
            question[i].num=i;
        }

        sort(question+1,question+1+m,cmp);

        map<int,int>change;

        int st=0;long long ansss;

        for(i=1;i<=m;i++)
        {
            while(st<question[i].r)
            {
                st++;
                if(change[num[st]]==0)
                {
                    k=st;
                    while(k<=n)
                    {
                        tree[k]+=num[st];
                        k+=(k&(-k));
                    }
                    change[num[st]]=st;
                }
                else
                {
                    k=change[num[st]];

                    while(k<=n)
                    {
                        tree[k]-=num[st];
                        k+=(k&(-k));
                    }

                    change[num[st]]=st;
                    k=st;
                    while(k<=n)
                    {
                        tree[k]+=num[st];
                        k+=(k&(-k));
                    }
                }
            }
            ansss=0;

            k=question[i].r;
            while(k>0)
            {
                ansss+=tree[k];
                k-=(k&(-k));
            }


            k=question[i].l-1;
            while(k>0)
            {
                ansss-=tree[k];
                k-=(k&(-k));
            }


            question[i].ans=ansss;
        }


        sort(question+1,question+1+m,cmp1);


        for(i=1;i<=m;i++)
        {
            printf("%lld\n",question[i].ans);
        }

        memset(tree,0,sizeof tree);
        memset(question,0,sizeof question);
        memset(num,0,sizeof num);

    }


    return 0;
}