HDOJ 题目3874 Necklace(线段树+离线求区间去重和)

Necklace

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


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   |   We have carefully selected several similar problems for you:   3878  3875  3870  3877  3876 
HDOJ3333也是求区间去重之后的和,而且比这个题数据还小,这个代码水过
 先把查询的左右边界记下来,然后开始从左开始往扫,以前出现过,就消去,知道这个查询的右边界,然后查询,
ac代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
#define N 100100
#define LL __int64
LL node[N<<2];
LL ans[N<<2],a[N];
struct s
{
	int l,r,id;
}q[N<<1];
int cmp(s a,s b)
{
	return a.r<b.r;
}
void pushup(int tr)
{
	node[tr]=node[tr<<1]+node[tr<<1|1];
}
void update(int pos,int l,int r,int tr,int val)
{
	if(l==r)
	{
		node[tr]+=val;
		return;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)
	{
		update(pos,l,mid,tr<<1,val);
	}
	else
		update(pos,mid+1,r,tr<<1|1,val);
	pushup(tr);
}
LL query(int L,int R,int l,int r,int tr)
{
	if(L<=l&&r<=R)
	{
		return node[tr];
	}
	int mid=(l+r)>>1;
	LL ans1,ans2;
	ans1=ans2=0;
	if(L<=mid)
		ans1=query(L,R,l,mid,tr<<1);
	if(R>mid)
		ans2=query(L,R,mid+1,r,tr<<1|1);
	return ans1+ans2;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		memset(node,0,sizeof(node));
		int i;
		for(i=1;i<=n;i++)
		{
			scanf("%I64d",&a[i]);
		}
		int m;
		scanf("%d",&m);
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&q[i].l,&q[i].r);
			q[i].id=i;
		}
		sort(q+1,q+m+1,cmp);
		int r=1;
		map<LL ,int>pre;
		for(i=1;i<=m;i++)
		{
			for(;r<=q[i].r;r++)
			{
				if(pre[a[r]])
					update(pre[a[r]],1,n,1,-a[r]);
				update(r,1,n,1,a[r]);
				pre[a[r]]=r;
			}
			ans[q[i].id]=query(q[i].l,q[i].r,1,n,1);
		}
		for(i=1;i<=m;i++)
		{
			printf("%I64d\n",ans[i]);
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值