boj 81

Description

Creating the stars is a boring job, the God also felt boring to do that. To make the creating more interesting, when created a star which had a mass value, the God put it at a position of a line. After created some stars, the God noticed that the he forgot where the mass central of the stars was. It was very important to know the mass central because the God needed to balance the mass. The God was very nervous. So, can you help the God with his problem? We assume there were no stars on the line at the beginning, and the universe was full of dust, whose mass is much lighter than a star and we can ignore it when compared with stars.

Input

There are several test cases and the input end with the end of file. In each case, the first line contains the length of the line L (1<=L<=100000) and the God’s action number Q (1<=Q<=100000); following Q lines, the first integer in each line is 0 or 1, if the integer is 0, and then follows 2 integers x, y, (1<=x<=y<=L) that indicate a query between x and y, and you should output the mass central between the x and y of that time; if the integer is 1, and then follow 2 integers p, v, (1<=p<=L, 1<=v<=100) that indicate the God puts a star whose mass is v in the position p.

Output

For every query, output a line contains only one number indicates where the mass central is. The output should round to 2 digits after decimal.

Sample Input

3 4

1 2 3

0 1 3

1 3 3

0 1 3

Sample Output

2.00

2.50

 

记得是以前阿里巴巴精英赛网络预赛的题,当时不会线段树一直超时,昨天学了下,今天重新做了下,要注意mass central的计算方法,为star的质量乘以它所在位置坐标的和除以star质量的值。

代码:

#include<iostream>
using namespace std;
#define LEN 100005
struct Treenode{
	int l;
	int r;
	int w;
	long long wd;
};

Treenode arr[3*(LEN+1)];

void bulidTree(int l,int r,int k)
{
	arr[k].l = l;
	arr[k].r = r;
	arr[k].w = 0;
	arr[k].wd = 0;
	if(l==r)
		return;
	int mid = (l+r)>>1;
	bulidTree(l,mid,2*k);
	bulidTree(mid+1,r,2*k+1);
}
void insert(int p,int w,int k)
{
	if(arr[k].l==arr[k].r)
	{
		arr[k].w+=w;
		arr[k].wd+=w*arr[k].l;
		return;
	}
	int mid = (arr[k].l+arr[k].r)>>1;
	if(p>mid)
		insert(p,w,2*k+1);
	else
		insert(p,w,2*k);
	arr[k].w = arr[2*k].w+arr[2*k+1].w;
	arr[k].wd = arr[2*k].wd+arr[2*k+1].wd;
}
void search(int l,int r,int k,int &w,long long &wd)
{
	if(arr[k].l==l&&arr[k].r==r)
	{
		w += arr[k].w;
		wd += arr[k].wd;
		return;
	}
	int mid = (arr[k].l+arr[k].r)>>1;
	if(l>mid)
		search(l,r,2*k+1,w,wd);
	else if(r<=mid)
		search(l,r,2*k,w,wd);
	else
	{
		search(l,mid,2*k,w,wd);
		search(mid+1,r,2*k+1,w,wd);
	}
}
int main()
{
	int l,q;
	while(~scanf("%d %d",&l,&q))
	{
		bulidTree(1,l,1);
		for(int i=0;i<q;i++)
		{
			int oper;
			scanf("%d",&oper);
			if(oper==1)
			{
				int p,v;
				scanf("%d %d",&p,&v);
				insert(p,v,1);
			}
			else
			{
				int x,y,w = 0;
				long long wd = 0;
				scanf("%d %d",&x,&y);
				search(x,y,1,w,wd);
				printf("%.2f\n",(double)wd/w);
			}
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值