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
 

Source

题意:
给定n个数。
接下来m个操作
0,x,y 把区间x-y内的所有数开根号
1,x,y输出区间x-y内所有数的和

分析:
数的大小不超过2^63,1开根号大小不变,所以开根号的次数不会超过7次,所以可以用线段树来储存区间值,更新时如果区间内所有数都是1那么就不需要继续更新了,否则则需要更新每个叶子结点。
ps:有个小坑是操作时给定的区间不一定是从小到大

代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#define INF 0x3f3f3f3f
#define EXP 0.00000001
#define MOD 1e9+7
#define MAXN 100005
#define ltree 2*id,ll,mid
#define rtree 2*id+1,mid+1,rr
#define FO(i,n,m) for(int i=n;i<=m;++i)
#define mem(a) memset(a,0,sizeof(a))
typedef long long LL;

using namespace std;

int n;
LL a[MAXN];
LL tree[4*MAXN];

void Pushup(int id)
{
    tree[id]=tree[2*id]+tree[2*id+1];
}

void Build(int id,int ll,int rr)
{
    if(ll==rr)
    {
        tree[id]=a[ll];
        return;
    }
    int mid=(ll+rr)/2;
    Build(ltree);
    Build(rtree);
    Pushup(id);
}

void Update(int id,int ll,int rr,int x,int y)
{
    if(tree[id]==rr-ll+1) //如果区间内所有数都是1就不用向下更新了
        return;
    if(ll==rr)
    {
        tree[id]=sqrt(tree[id]);
        return;
    }
    int mid=(ll+rr)/2;
    if(x<=mid)
        Update(ltree,x,y);
    if(y>mid)
        Update(rtree,x,y);
    Pushup(id);
}

LL Query(int id,int ll,int rr,int x,int y)
{
    if(x<=ll&&rr<=y)
        return tree[id];
    int mid=(ll+rr)/2;
    LL sum=0;
    if(x<=mid)
        sum+=Query(ltree,x,y);
    if(y>mid)
        sum+=Query(rtree,x,y);
    return sum;
}

int main()
{
    //freopen("00.txt","r",stdin);
    int cnt=1;
    while(~scanf("%d",&n))
    {
        FO(i,1,n)
            scanf("%lld",&a[i]);
        Build(1,1,n);
        int m;
        scanf("%d",&m);
        printf("Case #%d:\n",cnt++);
        while(m--)
        {
            int t,x,y;
            scanf("%d%d%d",&t,&x,&y);
            if(x>y)     //可能x比y大
                swap(x,y);
            if(t==0)
                Update(1,1,n,x,y);
            else
                printf("%lld\n",Query(1,1,n,x,y));
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值