`
zsybupt
  • 浏览: 41572 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

boj 81

    博客分类:
  • oj
阅读更多

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);
			}
		}
	}
}

 

分享到:
评论

相关推荐

    BOJ题目1023. Ancient Keyboard 源代码

    BOJ的题目1023. Ancient Keyboard解法 源代码

    boj 0809复试模拟题答案

    boj 上08 09 年复试模拟题的答案

    boj:算法

    boj:算法

    JAVA_BOJ

    JAVA_BOJ

    Algorithm-BOJ.zip

    Algorithm-BOJ.zip,BekJon在线法官(Java,Kotlin,SWIFT)和PS路线图,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    BOJ

    BOJ

    Algorithm-BOJ-PSJ.zip

    Algorithm-BOJ-PSJ.zip,Baykon在线判断JAVA问题解决方法(第二章),算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-BOJ-AutoCommit.zip

    Algorithm-BOJ-AutoCommit.zip,当您解决baekjoon online judge的问题时,它会自动提交并推送到远程存储库。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-boj-auto-submit.zip

    Algorithm-boj-auto-submit.zip,日本央行cli提交脚本,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Python库 | boj-0.0.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:boj-0.0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Boj Coloring Book-crx插件

    通过这本图画书展示您的创造力,其中包括Boj和朋友。 一本有趣的,全数字化且可重复使用的着色书,可用于 通过这本图画书展示您的创造力,其中包括Boj和朋友。 一本有趣的全数字可重复使用的图画书,专为孩子,父母...

    boj.kr:解决boj.kr的问题

    解决问题 Boj.kr

    BOJ:日本央行

    BOJ:日本央行

    boj:算法求解

    boj:算法求解

Global site tag (gtag.js) - Google Analytics