HDU4027——Can you answer these queries? (线段树区间更新求和+开根减枝+坑)

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 2 63.
  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

题解:

这个题没法打lazy标记(多半是我菜想不出来),但如果直接每次都更新到叶子结点绝对会超时。所以肯定有什么方法,然后我们可以发(bai)现(du)一个longlong数最多开根7次就变成了1 。所以我们可以通过判断区间是否都为1来减少时间 。

另外还有两个坑点见代码。

代码:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

long long board[MAXN];
long long Tree[MAXN*4];
bool One[MAXN*4]; 

void Up(int temp){
	Tree[temp] = Tree[temp<<1] + Tree[temp<<1|1];
	if(One[temp<<1] && One[temp<<1|1])One[temp] = true;
}

void Build(int temp,int left,int right){
	One[temp] = false;
	if(left == right){
		Tree[temp] = board[left];
		if(Tree[temp] == 1)One[temp] = true;
		return ;
	}
	int mid = left + (right-left)/2;
	Build(temp<<1,left,mid);
	Build(temp<<1|1,mid+1,right);
	Up(temp);
}

void Updata(int temp,int left,int right,int ql,int qr){
	if(ql>right || qr<left)return ;
	if(One[temp])return;
	if(left == right){
		Tree[temp] = (int)sqrt(Tree[temp]);
		/*这里要注意,sqrt(1) = 0 ,而(int)sqrt(1) = 1 */ 
		if(Tree[temp] == 1)One[temp] = true;
		return;
	}
	int mid = left + (right-left)/2;
	if(ql<=mid)Updata(temp<<1,left,mid,ql,qr);
	if(qr>mid)Updata(temp<<1|1,mid+1,right,ql,qr);
	Up(temp);
}

long long Query(int temp,int left,int right,int ql,int qr){
	if(ql>right || qr<left)return 0;
	if(ql<=left && qr>=right)return Tree[temp];
	int mid = left + (right-left)/2;
	long long sum = 0;
	if(ql<=mid)sum += Query(temp<<1,left,mid,ql,qr);
	if(qr>mid)sum += Query(temp<<1|1,mid+1,right,ql,qr);
	return sum;
}

int main(){
	int N,M;
	int a,b,c;
	int Num = 0;
	while(scanf("%d",&N)!=EOF){
		++Num;
		for(int _=1 ; _<=N ; _++){
			scanf("%lld",&board[_]);
		}
		Build(1,1,N);
		scanf("%d",&M);
		printf("Case #%d:
",Num);
		while(M--){
			scanf("%d %d %d",&a,&b,&c);
			if(b>c)swap(b,c);//注意b可能比c大,这又是一个坑点。 
			if(a){
				printf("%lld
",Query(1,1,N,b,c));
			}
			else {
				Updata(1,1,N,b,c);
			}
		}
		printf("
");
	}
	return 0;
}


原文地址:https://www.cnblogs.com/vocaloid01/p/9514105.html