天天看點

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