HDU-3874 Necklace(线段树)

Necklace

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


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

题意:给出一个长度为n的序列,然后进行m次询问,每次询问输入一个区间[a,b],要求输出[a,b]内数的和,并且重复的数只能计算一次
题解:因为询问次数多,因此我们想到用线段树维护来做.①对于询问,先用离线将询问按区间右边界从小到大排序,这样更新的时候,
就可以计算出要询问的答案;②对于更新,因为有了"重复的数只能计算一次"这个条件,这使得我们很难去用普通的单点更新的方法去做,
我们可以想到:当前要更新的位置为i,数值为a[i]的时候,只要判断上一次a[i]出现的位置,假设这个位置为index,那么我们只需要更新
[index+1,i]这段区间,因为[1,index]这个区间已经加过一次a[i]了不能重复.最后我们可以发现,当更新到第i个位置时,
线段树最底部的sum[rt]对应的数值等于区间[p,i]内数的和,因此这其实是一个成段更新的线段树.

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
const int maxn = 5e4 + 5;
int pre[maxn*20];
LL sum[maxn<<2],a[maxn];
struct Ans{
    int l,r,id;
    LL ans;
}p[maxn*4];
bool cmp1(Ans p1,Ans p2){
    if(p1.r!=p2.r)return p1.r<p2.r;
    return p1.l<p2.l;
}
bool cmp2(Ans p1,Ans p2){
    return p1.id<p2.id;
}
void PushDown(int rt){
    sum[rt<<1]+=sum[rt];
    sum[rt<<1|1]+=sum[rt];
    sum[rt]=0;
}
void build(int l,int r,int rt){
    sum[rt]=0;
    if(l==r) return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
//sum最下面的叶储存的是[l,R]的和,上面的根节点属于懒惰标记
void update(int L,int R,int l,int r,int rt){
    if(L<=l&&R>=r){
        if(pre[a[R]]>=r) return;   //如果这个数在r之后出现过了,则不重复计算
        if(pre[a[R]]<l){           //如果这个数还没有出现在[l,R],则加上这个数
            sum[rt]+=a[R];  //一开始脑残写成pre[a[R]]了
            return;
        }
    }
    int m=(l+r)>>1;
    PushDown(rt);
    if(L<=m) update(L,R,lson);
    if(R>m) update(L,R,rson);
}
//直接输出区间[p,i]的值,i表示询问时候的区间的右边界
LL query(int p,int l,int r,int rt){
    if(l==r) return sum[rt];
    int m=(l+r)>>1;
    PushDown(rt);
    LL ret=0;
    if(p<=m) ret=query(p,lson);
    else ret=query(p,rson);
    return ret;
}
int main(){
    int T,n,m;
   // freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        build(1,n,1);
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d%d",&p[i].l,&p[i].r);
            p[i].id=i;
            p[i].ans=0;
        }
        sort(p,p+m,cmp1);
        int k=0;
        for(int i=1;i<=n;i++){
            update(1,i,1,n,1);   //更新从1到i的后缀
            while(p[k].r<=i&&k<m) {p[k].ans=query(p[k].l,1,n,1);k++;}
            pre[a[i]]=i;      //更新a[i]上一次出现的位置
        }
        sort(p,p+m,cmp2);
        for(int i=0;i<m;i++) printf("%I64d\n",p[i].ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值