HDU 3874 Necklace (树状数组+离线操作)

93 篇文章 1 订阅
13 篇文章 0 订阅

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 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 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

题解:

一开始毫无头绪,百度后发现有种东西叫离线操作,感觉说明白就是在插入数据之前先按照特殊情况处理一下数据,这题就是这样,首先将得到的询问区间按照右端点从小到大排序,如果相同按左区间从小到大排序,这样就避免了询问后面段的区间的时候由于前面段的数据被处理了而出错,然后就按照排好序的询问进行处理,插入到哪处理到哪,整个有点动态同时进行的意思,遇到之前出现过的数就删掉然后重新插入该位置就好了

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
int a[50005];//存数值
int vis[1000005];//存每个数出现的最后位置
long long sum[50005];//树状数组
struct node
{
    int l,r;
    int id;
};
int cmp(node x,node y)//对询问区间右端点从小到大排序,如果相同对左端点从小到大排
{
    if(x.r!=y.r)
        return x.r<y.r;
    else
        return x.l<y.l;
}
node ask[200005];//存询问区间
long long ans[200005];//存id对应的答案
int n,m;
long long lowbit(long long x)//树状数组神器
{
    return x&(-x);
}
long long query(long long x)//long long防爆
{
    long long s=0;
    while(x>0)//向下连通的子区间询问和
    {
        s+=sum[x];
        x-=lowbit(x);
    }
    return s;
}
void insert(long long pos,long long v)//向上父区间累加和
{
    while(pos<=n)
    {
        sum[pos]+=v;
        pos+=lowbit(pos);
    }
}
long long Getsum(long long x,long long y)//两以开头为左端点的线段和相减得出该线段和
{
    return query(y)-query(x-1);
}
int main()
{
    int i,j,test;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=0;
        }
        sum[0]=0;
        scanf("%d",&m);
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&ask[i].l,&ask[i].r);
            ask[i].id=i;
        }
        sort(ask,ask+m,cmp);//排序
        memset(vis,-1,sizeof(vis));
        j=1;//为当前插入位置,即更新好的区段
        for(i=0;i<m;)//离线操作
        {
            if(ask[i].r>=j)//访问区间还没更新好,要插入后才可以访问
            {
                if(vis[a[j]]==-1)//前面没出现过
                {
                    vis[a[j]]=j;//记录下插入位置
                    insert(j,a[j]);
                }
                else
                {
                    insert(vis[a[j]],-a[j]);//删去之前的位置
                    vis[a[j]]=j;//记录当前位置
                    insert(j,a[j]);//插入当前位置
                }
                j++;
            }
            else//访问区间更新好了,访问
            {
                ans[ask[i].id]=Getsum(ask[i].l,ask[i].r);
                i++;
            }
        }
        for(i=0;i<m;i++)
            printf("%lld\n",ans[i]);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值