• hdu 3874 树状数组


    Necklace

    Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2447    Accepted Submission(s): 865


    Problem Description
    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
     
    题目大意:有N个魔球,每个魔球都有自己的价值,M条查询(L,R)的价值和(同一价值的只算一次)。
     
    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<algorithm>
    using namespace std;
    
    int dit[50010];
    __int64 ans[200010];
    __int64 f[50010];
    
    inline int lowbit(int x){ return x&(-x);}
    
    struct query
    {
        int lp,rp,id;
    }q[200010];
    
    bool mycomp(const query &a,const query &b)
    {
        if(a.rp==b.rp) return a.lp<b.lp;
        return a.rp<b.rp;
    }
    
    void swap(int &a,int &b){ int t=a;a=b;b=t;}
    
    void add(int x,int d,int n)
    {
        while(x<=n){
            f[x]+=d;x+=lowbit(x);
        }
    }
    
    __int64 sum(int x)
    {
        __int64 ret=0;
        while(x>=1){
            ret+=f[x];x-=lowbit(x);
        }
        return ret;
    }
    
    int main()
    {
        int n,m,i,rp,t,x;
        map<int,int> mp;
        scanf("%d",&t);
        while(t--)
        {
            mp.clear();
            memset(f,0,sizeof(f));
            scanf("%d",&n);
            for(i=1;i<=n;i++) scanf("%d",&dit[i]);
            scanf("%d",&m);
            for(i=0;i<m;i++)
            {
                scanf("%d %d",&q[i].lp,&q[i].rp);
                if(q[i].lp>q[i].rp) swap(q[i].lp,q[i].rp);
                q[i].id=i;
            }
            sort(q,q+m,mycomp);
            rp=1;
            for(i=0;i<m;i++)
            {
                while(rp<=q[i].rp)
                {
                    x=dit[rp];
                    if(mp[x]!=0) add(mp[x],-x,n);
                    add(rp,x,n);
                    mp[x]=rp;
                    rp++;
                }
                ans[q[i].id]=sum(q[i].rp)-sum(q[i].lp-1);
            }
            for(i=0;i<m;i++) printf("%I64d
    ",ans[i]);
        }
        return 0;
    }
  • 相关阅读:
    C#面向对象设计模式纵横谈 笔记10 Decorator 装饰(结构型模式)
    [译]WPF 应用程序和MVVM设计模式 ——Josh Smith
    熙熙如何对待JavaScript(绝好的一套针对初学者的JavaScript教程)推荐教程下载PDFCHM电子书经熙熙筛选整理
    熙熙如何在Vmware里安装Ubantu9.10Alpha6(虚拟机安装Linux)
    熙熙如何设置WinCE仿真模拟器(网络、串口、耳机)
    熙熙我的博客开通啦
    熙熙SQLCE类熙熙
    熙熙WebBrowser判断登录成功WebBrowser404错误500错误屏蔽消息窗口Webbrowser判断是否加载成功
    熙熙如何用USB将WinCE与PC同步(天嵌开发板,ARM)
    推荐我一个很喜欢的音乐软件 多米音乐盒
  • 原文地址:https://www.cnblogs.com/xiong-/p/3585206.html
Copyright © 2020-2023  润新知