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

题目大意

如果前面是0就区间开根号,如果前面是1就区间求和

解题思路

先把线段树模板搬出来,节点保存的是区间和
由于区间开根号的特性,lazy数组不能帮助我们减少操作,每一次区间开根号都要遍历到每个叶子进行更新
如果不做任何优化,这样的复杂度是非常高的
但是由于1开根号还是1,换言之,只要当前子树的每个节点上都是1,那么这颗树上的节点都可以不更新。
也就是tree[node]=R-L+1时可以直接返回
ps。还有一个坑点是hdu常用的,输入中的范围不一定是从小到大,需要判断一下

#include<iostream>
#include<string>
#include"cmath"
#include"cstdio"
#define LL long long 
#define MAX 200010
using namespace std;
LL tree[MAX<<2]; // 线段树
LL lazy[MAX<<2]; // 延迟标记。都要4倍空间 
//lazy数组设置的目的:如果我要求的区间覆盖了此子树代表的区间,只需要改子树根节点的lazy值
//就相当于操作这整一个子树,不再需要深入更新 
//如果要进行深入某一个节点操作,就把lazy值分配下去(push_down函数),然后递归单独访问要操作的节点 

void push_up(int node){//每次操作后,要进行线段树的状态更新 
	//tree[node] = max(tree[node<<1] , tree[node<<1|1]);//根节点保存极大值
	//tree[node] = min(tree[node<<1] , tree[node<<1|1]);根节点保存极小值
	tree[node] = tree[node<<1] + tree[node<<1|1];//根节点保存和
}
// 创建线段树
void build(int node,int L,int R){
	if(L == R){
		scanf("%lld",&tree[node]);
		return;
	}
	int mid = (L+R)>>1;
	build(node<<1,L,mid);
	build(node<<1|1,mid+1,R);
	push_up(node);
}
// 更新,newdata为更新值,indexl/r为要求范围(单点更新indexl==indexr),l,r为更新范围
void update(int indexl,int indexr,int L,int R,int node){
	if(indexl<=L&&indexr>=R){
		if(tree[node]==R-L+1)
		return;
	}
	if(L==R){
		tree[node]=sqrt(tree[node]);
        return ;
	}
	int mid = (L+R) >> 1;
	if(indexl<=mid) update(indexl,indexr,L,mid,node<<1);
	if(indexr>mid) update(indexl,indexr,mid+1,R,node<<1|1);
	push_up(node);
}
// 区间查找
LL query_range(int node,int L,int R,int indexl,int indexr){
	if(indexl <= L && indexr >= R) return tree[node];//满足lazy要求,不用深入操作 
	int mid = (L+R) >>1;
	LL ans= 0;
	if(mid >= indexl) ans+=query_range(node<<1,L,mid,indexl,indexr);//区间求和
	if(mid < indexr) ans+=query_range(node<<1|1,mid+1,R,indexl,indexr);
	return ans;
}
int main() {
	int n,m;
	int case1=0;
	while(~scanf("%d",&n)){
		build(1,1,n);
		scanf("%d",&m);
		printf("Case #%d:\n",++case1);
		for(int i=1;i<=m;i++){
			char ch[2];
			scanf("%2s",ch);
			if(ch[0]=='1'){
				int a,b;
				scanf("%d%d",&a,&b);
				if(a>b)swap(a,b);
				printf("%lld\n",query_range(1,1,n,a,b));
			}else {
				int a,b;
				scanf("%d%d",&a,&b);
				if(a>b)swap(a,b);
				update(a,b,1,n,1);
			}
		}
		cout<<endl;
	}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值