HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

Necklace

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


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
 

 

Source
 

 

Recommend
lcy
 

 

  1. /* 
  2. 题意为查找区间去重后的和 
  3. 用树状数组离线处理 
  4. 将所有查询以右端点从小到大排序 
  5. 按此顺序边去重边查询 
  6. 前面的去重就不会影响到后面的结果了 
  7. */  
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int N=51000;
const int M=210000;

struct node{
    int l,r;
    int id;
}q[M];

int n,m,val[N],pre[N],loc[1100000];
long long arr[N],res[M];

int lowbit(int x){
    return x&(-x);
}

void update(int i,int x){
    while(i<=n){
        arr[i]+=x;
        i+=lowbit(i);
    }
}

long long Sum(int i){
    long long ans=0;
    while(i>0){
        ans+=arr[i];
        i-=lowbit(i);
    }
    return ans;
}

bool cmp(node a,node b){
    return a.r<b.r;
}

int main(){

    //freopen("input.txt","r",stdin);

    int t;
    scanf("%d",&t);
    while(t--){
        memset(arr,0,sizeof(arr));
        memset(loc,-1,sizeof(loc));
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&val[i]);
            pre[i]=loc[val[i]];
            loc[val[i]]=i;
            update(i,val[i]);
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort(q+1,q+1+m,cmp);
        int r=0;
        for(int i=1;i<=m;i++){
            for(int j=r+1;j<=q[i].r;j++)
                if(pre[j]!=-1)
                    update(pre[j],-val[j]);
            r=q[i].r;
            res[q[i].id]=Sum(q[i].r)-Sum(q[i].l-1);
        }
        for(int i=1;i<=m;i++)
            printf("%I64d\n",res[i]);
    }
    return 0;
}

 

线段树:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>

using namespace std;

const int N=51000;

//#define L(rt) (rt<<1)
//#define R(rt) (rt<<1|1)

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1

struct Tree{
    int l,r;
    int id;
}q[N<<2];

map<int,int> mp;
int n,m,a[N];
long long sum[N<<2],res[N<<2];

void PushUp(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void update(int id,int val,int l,int r,int rt){
    if(l==r){
        sum[rt]+=val;
        return ;
    }
    int mid=(l+r)>>1;
    if(id<=mid)
        update(id,val,lson);
    else
        update(id,val,rson);
    PushUp(rt);
}

long long query(int L,int R,int l,int r,int rt){
    if(L<=l && R>=r)
        return sum[rt];
    int mid=(l+r)>>1;
    long long ans=0;
    if(L<=mid)
        ans+=query(L,R,lson);
    if(R>mid)
        ans+=query(L,R,rson);
    return ans;
}

int cmp(Tree a,Tree b){
    return a.r<b.r;
}

int main(){

    //freopen("input.txt","r",stdin);

    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort(q+1,q+1+m,cmp);
        mp.clear();
        memset(sum,0,sizeof(sum));
        int r=0;
        for(int i=1;i<=m;i++){
            for(int j=r+1;j<=q[i].r;j++){
                if(mp[a[j]])
                    update(mp[a[j]],-a[j],1,n,1);
                update(j,a[j],1,n,1);
                mp[a[j]]=j;
                r=q[i].r;
            }
            res[q[i].id]=query(q[i].l,q[i].r,1,n,1);
        }
        for(int i=1;i<=m;i++)
            printf("%I64d\n",res[i]);
    }
    return 0;
}

 

posted @ 2013-09-04 09:01  Jack Ge  阅读(732)  评论(0编辑  收藏  举报