HDU-4027Can you answer these queries?(线段树)

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
题意分析:给定n个值,m次操作,每次操作有三个数值如果第一个数值为零则表示将后面两个数组成的区间内的所有数值变成它对应的开根号的值(两个端点值的大小并不知道),如果第一个数值为1的话就是计算后面这个区间内所有数值的和。
解题思路:区间端点值修改,在单点修改的基础上加上一些标记,防止超时。
AC代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define lson rt<<1
#define rson (rt<<1|1)
long long sum[290005];
int add[290005];
void pushup(int rt){
    
    
	sum[rt]=sum[lson]+sum[rson];
	add[rt]=add[lson] && add[rson];
}
void build(int l,int r,int rt){
    
    
	add[rt]=0;
	int mid=(l+r)>>1;
	if(l==r){
    
    
		scanf("%lld",&sum[rt]);
		return;
	}
	build(l,mid,lson);
	build(mid+1,r,rson);
	pushup(rt); 
}
void update(int rt,int L,int R,int l,int r){
    
    
	if(add[rt])
		return ;
	if(l==r){
    
    
		int a=sqrt(sum[rt]);
		sum[rt]=a;
		if(!(sum[rt]!=1))//因为1的平方根还是1所以可以直接将其进行标记,节省时间
			add[rt]=1;
		return;
	}
	int mid=(l+r)>>1;
	if(L<=mid) update(lson,L,R,l,mid);
	if(R>mid) update(rson,L,R,mid+1,r);
	pushup(rt);
} 
long long query(int rt,int L,int R,int l,int r){
    
    
	if(L<=l && R>=r) return sum[rt];
	int mid=(l+r)>>1;
	long long sum1=0;
	if(L<=mid) sum1+=query(lson,L,R,l,mid);
	if(R>mid) sum1+=query(rson,L,R,mid+1,r);
	return sum1;
}
int main(){
    
    
	int N,M,x,y,z,T,k=0,op;
	while(~scanf("%d",&N)){
    
    
		build(1,N,1);
		scanf("%d",&M);
		printf("Case #%d:\n",++k);
		for(int i=0;i<M;i++){
    
    
			scanf("%d%d%d",&op,&x,&y);
			if(x>y) swap(x,y);
			if(op==0) 
				update(1,x,y,1,N);
			if(op==1)
				printf("%lld\n",query(1,x,y,1,N));
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44313771/article/details/107409079