• hdu 3874 Necklace(bit树+事先对查询区间右端点排序)


    Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful value of some interval [x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6. 
    Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.
     

    Input

    The first line is T(T<=10), representing the number of test cases.    For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
     

    Output

    For each query, output a line contains an integer number, representing the result of the query.
     

    Sample Input

    2
    6
    1 2 3 4 3 5
    3
    1 2
    3 5
    2 6
    6
    1 1 1 2 3 5
    3
    1 1
    2 4
    3 5
     

    Sample Output

    3
    7
    14
    1
    3
    6
     
    题意:查询一段连续区间内不同整数的和。
    解析:刚开始拿到这题没有想法,后来看了别人的题解才知道可以先对查询区间的右端点从小到大排序然后再处理。从上次更新停止的位置(上一个的右端点)一直更新到这次的右端点。如果某个数之前出现过,则add(pre,-val)(减掉这个数),再在当前位置插入这个数,则可以起到去重的作用。
    代码如下:
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<vector>
    #include<iterator>
    #include<utility>
    #include<sstream>
    #include<iostream>
    #include<cmath>
    #include<stack>
    using namespace std;
    const int INF=1000000007;
    const double eps=0.00000001;
    typedef __int64 LL;
    int N;
    LL A[50005],elem[50005],ans[200005];
    int lowbit(int x){  return x&-x; }
    LL sum(int id)              bit树
    {
        LL ret=0;
        while(id>0){ ret+=elem[id]; id-=lowbit(id); }
        return ret;
    }
    void add(int id,LL val)
    {
        while(id<=N){ elem[id]+=val; id+=lowbit(id); }
    }
    int sign[1000005];
    struct node
    {
        int le,ri,id;
        bool operator < (const node& t) const { return ri<t.ri; }   //对右端点排序
    }qes[200005];
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&N);
            memset(elem,0,sizeof(elem));
            for(int i=1;i<=N;i++)
            {
                scanf("%I64d",&A[i]);
                sign[A[i]]=0;
            }
            int M;
            cin>>M;
            for(int i=1;i<=M;i++)  scanf("%d%d",&qes[i].le,&qes[i].ri),qes[i].id=i;   //输入
            sort(qes+1,qes+1+M);
            int pos=1;
            for(int i=1;i<=M;i++)
            {
                while(pos<=qes[i].ri)     //更新
                {
                    if(sign[A[pos]])  add(sign[A[pos]],-A[pos]);   //之前出现过
                    sign[A[pos]]=pos;   //更新到当前位置
                    add(pos,A[pos]);    //插入
                    pos++;
                }
                ans[qes[i].id]=sum(qes[i].ri)-sum(qes[i].le-1);   //得到答案
            }
            for(int i=1;i<=M;i++)  printf("%I64d
    ",ans[i]);
        }
        return 0;
    }
    View Code
     
     
  • 相关阅读:
    。。。
    __new__ 单例
    bokeh
    空间数据可视化
    关系网络图
    Pandas 50题练习
    seaborn
    数据输出及内容美化 简单介绍
    数据分析---项目总结
    数学建模
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/4761419.html
Copyright © 2020-2023  润新知