HDU 3874 Necklace(樹狀數組),hdu3874

來源:互聯網
上載者:User

HDU 3874 Necklace(樹狀數組),hdu3874
Problem DescriptionMery 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. 
InputThe 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. 
OutputFor each query, output a line contains an integer number, representing the result of the query. 
Sample Input

261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
 
Sample Output
3714136

題意:給你一串數字,讓你求區間[l,r]內的和,要求重複數字只求一次。

樹狀數組做法:大家肯定會想到離線,但是離線後排序如何排,這裡有一個思路:

由於要去重,我們考慮將詢問按右區間從小到大排序,對於詢問,我們逐個去掉前面

重複的值,只保留當前的。

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;const int maxn=50000+100;const int maxm=200000+100;LL c[maxn], ans[maxm];int pre[maxn],hash[1000000+10];struct node{    int l,r;    int id;}q[maxm];int num[maxn];int t,n,m;int lowbit(int x){    return x&(-x);}void update(int x,LL w){    while(x<maxn)    {        c[x]+=w;        x+=lowbit(x);    }}LL query(int x){    LL s=0;    while(x>0)    {        s+=c[x];        x-=lowbit(x);    }    return s;}int cmp(node l1,node l2){    return l1.r<l2.r;}int main(){    int x,y;    scanf("%d",&t);    while(t--)    {        CLEAR(hash,-1);        CLEAR(c,0);        scanf("%d",&n);        REPF(i,1,n)        {            scanf("%I64d",&num[i]);            pre[i]=hash[num[i]];            hash[num[i]]=i;            update(i,num[i]);        }        scanf("%d",&m);        REP(i,m)        {            scanf("%d%d",&x,&y);            q[i].l=x;q[i].r=y;            q[i].id=i;        }        sort(q,q+m,cmp);        int rr=0;        REP(i,m)        {            for(int j=rr+1;j<=q[i].r;j++)            {                if(pre[j]!=-1)//臥槽,只保留當前位置的num[j],精髓啊                    update(pre[j],-num[j]);            }            rr=q[i].r;            ans[q[i].id]=query(q[i].r)-query(q[i].l-1);        }        REP(i,m)           printf("%I64d\n",ans[i]);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.