我是靠谱客的博主 缥缈裙子,最近开发中收集的这篇文章主要介绍HDU-3874 Necklace(线段树)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
2011 Multi-University Training Contest 4 - Host by SDU
 

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("%I64dn",p[i].ans);
}
return 0;
}


最后

以上就是缥缈裙子为你收集整理的HDU-3874 Necklace(线段树)Necklace的全部内容,希望文章能够帮你解决HDU-3874 Necklace(线段树)Necklace所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部