您的位置:首页 > 其它

bzoj-3211 花神游历各国

2015-04-30 19:52 253 查看
题意:

给出长度为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
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: