码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4027 Can you answer these queries?

时间:2015-05-29 23:17:22      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:线段树

Problem Description
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.
 

Input
The input contains several test cases, terminated by EOF.
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
 

Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
 

Sample Input
10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
 

Sample Output
Case #1: 19 7 6
这道题看了别人的题解做出来了,感觉2^63次是来吓唬人的,其实开7次根号就是1了,然后就不变了。这里一个一个更新肯定会超时的,所以要利用成段更新,但这里又有一个问题,每个数字变化后他们变化的值都不同,不是普通的减或乘,所以比较特殊。这里我先判断是不是区间里的数都是1,如果都是1就都不用更新,因为1的平方根还是1,那么怎么判断呢,这里用b[i].sum==b[i].r-b[i].l+1来判断。如果说区间里有大于1的数,那么都要更新到叶子节点(这里有个剪枝,就是每次更新完后记录a[i]的值,如果下次更新,a[i]已经是1了就可以直接跳过去不更新)。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 1000006
__int64 a[maxn+100],sum;
struct node{
	int l,r;
	__int64 sum;
}b[16*maxn];

void build(int l,int r,int i)
{
	int mid;
	b[i].l=l;b[i].r=r;
	if(l==r){
		b[i].sum=a[l];return;
	}
	mid=(l+r)/2;
	build(l,mid,i*2);
	build(mid+1,r,i*2+1);
	b[i].sum=b[i*2].sum+b[i*2+1].sum;
}

void update1(int id,int i)
{
	int mid;
	if(b[i].l==b[i].r){
		b[i].sum=a[b[i].l]=(int)(sqrt(1.0*a[b[i].l]));
		return;
	}
	mid=(b[i].l+b[i].r)/2;
	if(id>mid)update1(id,i*2+1);
	else update1(id,i*2);
	b[i].sum=b[i*2].sum+b[i*2+1].sum;
}

void update(int l,int r,int i)
{
	int mid,j;
	if(b[i].l==l && b[i].r==r){
		if(b[i].sum==b[i].r-b[i].l+1)return;
		for(j=b[i].l;j<=b[i].r;j++){
			if(a[j]==1)continue;
			update1(j,1);
		}
		return;
	}
	mid=(b[i].l+b[i].r)/2;
	if(r<=mid)update(l,r,i*2);
	else if(l>mid)update(l,r,i*2+1);
	else {
		update(l,mid,i*2);
		update(mid+1,r,i*2+1);
	}
}

void question(int l,int r,int i)
{
	int mid;
	if(b[i].l==l && b[i].r==r){
		sum+=b[i].sum;return;
	}
	mid=(b[i].l+b[i].r)/2;
	if(r<=mid)question(l,r,i*2);
	else if(l>mid)question(l,r,i*2+1);
	else {
		question(l,mid,i*2);
		question(mid+1,r,i*2+1);
	}
}

int main()
{
	int n,m,i,j,d,c,e,num1=0,temp;
	while(scanf("%d",&n)!=EOF)
	{
		num1++;
		printf("Case #%d:\n",num1);
		for(i=1;i<=n;i++){
			scanf("%I64d",&a[i]);
		}
		build(1,n,1);
		scanf("%d",&m);
		for(i=1;i<=m;i++){
			scanf("%d%d%d",&c,&d,&e);
			if(d>e){
			    temp=d;d=e;e=temp;
			}
			if(c==0)update(d,e,1);
			else{
				sum=0;
				question(d,e,1);
				printf("%I64d\n",sum);
			}
		}
		printf("\n");
	}
	return 0;
}


hdu4027 Can you answer these queries?

标签:线段树

原文地址:http://blog.csdn.net/kirito_acmer/article/details/46239925

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!