[线段树]*对区间里的单点开根号 区间和查询* hdu 4027

题目

在这里插入图片描述
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027

这道题和线段树板子有点不一样,算是做到的一第一个不能全靠板子的题。这道题的update应该看点不是区间。
x,y的大小,是一个坑。

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
#include<fstream>
#include<complex>
#include<algorithm>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
struct sut{
	int l,r;bool lazy;
	ll sum;
}tree[400010];
ll a[100010];
void build(int l1,int r1,int x){
	tree[x].l=l1,tree[x].r=r1;
	if(l1==r1) {
		tree[x].sum=a[l1],tree[x].lazy=1;
		if(tree[x].sum<=1) tree[x].lazy=0;
		return; 
	} 
	int mid=(l1+r1)>>1;
	build(l1,mid,x<<1);
	build(mid+1,r1,x<<1|1);
	tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
	tree[x].lazy=tree[x<<1|1].lazy|tree[x<<1].lazy;
}
void update(int l1,int r1,int x){
	if(tree[x].l==tree[x].r){
		tree[x].sum=(ll)sqrt(1.0*tree[x].sum);
		if(tree[x].sum<=1) tree[x].lazy=0;
		return;
	}
	int mid=(tree[x].l+tree[x].r)>>1;
	if(l1<=mid&&tree[x<<1].lazy) update(l1,r1,x<<1);
	if(mid<r1&&tree[x<<1|1].lazy) update(l1,r1,x<<1|1);
	tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
	tree[x].lazy=tree[x<<1|1].lazy|tree[x<<1].lazy;
}
ll query(int l1,int r1,int x){
	ll res=0;
	if(l1<=tree[x].l&&tree[x].r<=r1){
		return tree[x].sum;
	}
	int mid=(tree[x].l+tree[x].r)>>1;
	if(l1<=mid) res+=query(l1,r1,x<<1);
	if(mid<r1) res+=query(l1,r1,x<<1|1);
	return res;
}
int main(){
	//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int n;
	int t=0;
	while(~scanf("%d",&n)){
		t++;
		printf("Case #%d:\n",t); 
		memset(a,0,sizeof a);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	build(1,n,1);
	int m;
	scanf("%d",&m);
	while(m--){
		int b,x,y;
		scanf("%d%d%d",&b,&x,&y);
		if(b==0){
			if(x>y) swap(x,y);//弱智错误 
			update(x,y,1);
		}
		if(b==1){
			if(x>y) swap(x,y);//弱智错误 
			printf("%lld\n",query(x,y,1));}
		}
		printf("\n");
	} 
    return 0;
}
发布了78 篇原创文章 · 获赞 1 · 访问量 4379

猜你喜欢

转载自blog.csdn.net/kosf_/article/details/104433941