HDU 4027 Can you answer these queries(线段树 成段更新)

本文介绍了一个涉及区间更新和查询的问题,特别之处在于区间内的数值通过取平方根的方式进行更新。文章提供了一种利用线段树的数据结构解决方案,并通过具体实例展示了如何高效地处理此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Problem Description
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
 

题意:给一个区间跟新,不过不同的是把这一个区间的数都开根号,


思路:因为一个数开一定的根号后是1 ,那么就不会再变了,一段区间这样都是一就没必要改变了,所以这样可以节约时间,还有这份代码用G++交超时,c++交过





#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 1000005

struct stud{
int le,ri;
ll len,sum;
}f[N*4];

ll a[N];

inline void pushup(int pos)
{
	f[pos].sum=f[L(pos)].sum+f[R(pos)].sum;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].len=ri-le+1;
	if(le==ri)
	{
		f[pos].sum=a[le];
		return ;
	}

    int mid=MID(le,ri);
    build(L(pos),le,mid);
    build(R(pos),mid+1,ri);

    pushup(pos);
}

void hello(int pos)       //把这一个区间的数都开根号
{
	if(f[pos].len==f[pos].sum)
		return;

	if(f[pos].le==f[pos].ri)
	{
		f[pos].sum=(ll)(sqrt(f[pos].sum+0.0));
		return ;
	}
    hello(L(pos));
    hello(R(pos));

    pushup(pos);
}

void update(int pos,int le,int ri)
{
    if(f[pos].len==f[pos].sum)
		return ;

	if(f[pos].le==le&&f[pos].ri==ri)
	{
		hello(pos);
		return ;
	}

	int mid=MID(f[pos].le,f[pos].ri);

	if(mid>=ri)
		update(L(pos),le,ri);
	else
		if(mid<le)
		 update(R(pos),le,ri);
	else
		{
			update(L(pos),le,mid);
			update(R(pos),mid+1,ri);
		}
   pushup(pos);
}

ll query(int pos,int le,int ri)
{
	if(f[pos].le==le&&f[pos].ri==ri)
		return f[pos].sum;

	int mid=MID(f[pos].le,f[pos].ri);

	if(mid>=ri)
		return query(L(pos),le,ri);
	else
		if(mid<le)
		return query(R(pos),le,ri);
	else
		return (ll)(query(L(pos),le,mid)+query(R(pos),mid+1,ri));
}

int main()
{
   int i,j,n,m,ca=0;
   while(~sf(n))
   {
   	   fre(i,1,n+1)
   	     scanf("%I64d",&a[i]);

		build(1,1,n);
		sf(m);
		pf("Case #%d:\n",++ca);

		int op,le,ri;

		while(m--)
		{
			sfff(op,le,ri);
			if(le>ri) swap(le,ri);
			if(op)
			   pf("%I64d\n",query(1,le,ri));
			else
			   update(1,le,ri);
		}
		pf("\n");
   }
   return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值