zoukankan      html  css  js  c++  java
  • 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
     
     
  • 相关阅读:
    向架构师进军--->如何编写软件架构文档
    让创意更有黏性!
    eaby技术架构变迁
    应用系统之间数据传输的几种方式
    基于 CAS 无锁实现的 Disruptor.NET 居然慢于 BlockingCollection,是真的吗?
    调整数据库表结构,搞定 WordPress 数据库查询缓慢问题
    dynamic-css 动态 CSS 库,使得你可以借助 MVVM 模式动态生成和更新 css,从 js 事件和 css 选择器的苦海中脱离出来
    ASP.NET Framework 重写后的 .NET 异常报错界面(异常堆栈和溯源一目了然)
    Orchard Core 中运行带程序上下文的单元测试
    Angular 2 前端 http 传输 model 对象及其外键的问题
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/4761419.html
Copyright © 2011-2022 走看看