bzoj-3211 花神游历各国

141 篇文章 0 订阅
88 篇文章 0 订阅

题意:

给出长度为n的非负序列与m个操作;

操作1 查询区间和;

操作2 将区间内所有数字开方;


题解:

本以为是到神题推了一会,结果是个傻题= =;

总而言之就是10^9开五次平方就变成了1,就可以不再修改他;

那么记录一个标记,表示这段区间是否还可以被开平方;

线段树暴力搞就可以了,要开long long;


代码:


#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 100100
#define lson l,mid,no<<1
#define rson mid+1,r,no<<1|1
using namespace std;
typedef long long ll;
ll sum[N<<2],a[N];
bool is[N<<2];
void Pushup(ll no)
{
	sum[no]=sum[no<<1]+sum[no<<1|1];
	is[no]=is[no<<1]&is[no<<1|1];
}
void build(ll l,ll r,ll no)
{
	if(l==r)
	{
		scanf("%lld",a+l);
		sum[no]=a[l];
		if(a[l]==1||a[l]==0)
			is[no]=1;
		else
			is[no]=0;
	}
	else
	{
		ll mid=(l+r)>>1;
		build(lson);
		build(rson);
		Pushup(no);
	}
}
void update(ll l,ll r,ll no,ll st,ll en)
{
	if(st<=l&&r<=en)
	{
		if(!is[no])
		{
			if(l!=r)
			{
				ll mid=(l+r)>>1;
				if(st<=mid)		update(lson,st,en);
				if(mid+1<=en)	update(rson,st,en);
				Pushup(no);
			}
			else
			{
				a[l]=sqrt(a[l]);
				sum[no]=a[l];
				if(a[l]==1)
					is[no]=1;
			}
		}
	}
	else
	{
		ll mid=(l+r)>>1;
		if(st<=mid)		update(lson,st,en);
		if(mid+1<=en)	update(rson,st,en);
		Pushup(no);
	}
}
ll query(ll l,ll r,ll no,ll st,ll en)
{
	if(st<=l&&r<=en)
	{
		return sum[no];
	}
	else
	{
		ll mid=(l+r)>>1;
		if(en<=mid)			return query(lson,st,en);
		else if(st>=mid+1)	return query(rson,st,en);
		else
			return query(lson,st,en)+query(rson,st,en);
	}
}
int main()
{
	ll n,m,i,j,k,l,r,op;
	scanf("%lld",&n);
	build(1,n,1);
	scanf("%lld",&m);
	for(i=1;i<=m;i++)
	{
		scanf("%lld%lld%lld",&op,&l,&r);
		if(op==1)
		{
			printf("%lld\n",query(1,n,1,l,r));
		}
		else
		{
			update(1,n,1,l,r);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值