hdu3874 Necklace 线段树/树状数组

Necklace

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


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 

唉,树状数组和线段树的应用都是基于他们是“活”的,他们的更新是有顺序的,巧妙地运用顺序方能解题。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

const int  maxn=5000;
int n;
int a[maxn+20];
int sum[4*maxn+20];
int tot;

void update(int num,int le,int ri,int x)
{
	if(le==ri)
	{
		sum[num]++;
		return;
	}
	int mid=(le+ri)>>1;
	if(x<=mid)   update(num*2,le,mid,x);
	else   update(num*2+1,mid+1,ri,x);
	sum[num]=sum[num<<1]+sum[(num<<1)+1];

}

int query(int num,int le,int ri,int x,int y)
{
	if(x<=le&&ri<=y)
	{
		return sum[num];
	}
   int ret=0;
   int mid=(le+ri)>>1;
   if(x<=mid)   ret+=query(num*2  ,le   ,mid,x,y);
   if(y> mid)   ret+=query(num*2+1,mid+1,ri ,x,y);
   return ret;

}

int main()
{
    while(~scanf("%d",&n))
	{
		memset(sum,0,sizeof sum);
		tot=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			tot+=query (1,0,maxn,a[i]+1,maxn);
			     update(1,0,maxn,a[i]);
		}
		int mini=tot;
		for(int i=1;i<=n-1;i++)
		{
			int sm=query(1,0,maxn,0,a[i])-1;
			int la=n-1-sm;
			tot=tot-sm+la;
			mini=min(mini,tot);
		}
		printf("%d\n",mini);
	}
    return 0;

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值