Can you answer these queries? (HDU-4027)

Can you answer these queries? (HDU-4027)

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

题目翻译:

许多邪恶的战舰在战斗前排成一行。我们的指挥官决定用我们的秘密武器消灭战舰。每一艘战舰都可以被标记为耐久力值。对于我们的秘密武器的每一次攻击,它都能使战列舰的一个连续部分的耐久力降低,使它们的耐久力等于它原来的耐久力值的平方根。在我们秘密武器的一系列攻击中,指挥官想要评估武器的效果,所以他找你帮忙。

你被要求回答的问题是,一艘战舰连续部分耐力的总和。

注意,平方根操作应该四舍五入为integer。

输入

输入包含几个测试用例,由EOF终止。

对于每个测试用例,第一行包含一个整数N,表示一行中有N艘邪恶战舰。(1 <= N <= 100000)

第二行包含N个整数Ei,表示每艘战舰从一行开始到结束的耐久值。你可以假设所有耐力值的和小于2 63。

下一行包含一个整数M,表示操作和查询的数量。(1 <= M <= 100000)

对于以下M行,每一行包含T、X、y三个整数,其中T=0表示秘密武器的作用,这将降低战列舰在X-th和Y-th之间的耐久值,包括。T=1表示指挥官的查询,该查询要求战舰在X-th和Y-th之间的耐力值之和,包括。

输出

对于每个测试用例,在第一行打印用例号。然后为每个查询打印一行。记住,在每个测试用例之后,都要跟随一个空行。

思路:线段树区间查询和更改,注意1的平方根还是1,所以可以特判一下,可以省不少时间。

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define PI 3.1415926
#define inf 0x3f3f3f3f
const int maxn=1e5+10;
using namespace std;
long long Sum[maxn<<2];
long long A[maxn];
void PushUp(int rt)
{
    Sum[rt]=Sum[rt<<1]+Sum[rt<<1|1];
}

void Build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%lld",&Sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    Build(l,m,rt<<1);
    Build(m+1,r,rt<<1|1);
    PushUp(rt);
}
void Update(int x,int y,int l,int r,int rt)
{
    if(l==r)
    {
        Sum[rt]=sqrt(Sum[rt]);
        return ;
    }
    if(x<=l && r<=y && Sum[rt]==r-l+1)
        return;
    int m=(l+r)>>1;
    if(x<=m)
        Update(x,y,l,m,rt<<1);
    if(m<y)
        Update(x,y,m+1,r,rt<<1|1);
    PushUp(rt);
}
long long Query(int x,int y,int l,int r,int rt)
{
    if(x<=l && r<=y)
    {
        return Sum[rt];
    }
    int m=(l+r)>>1;
    long long num=0;
    if(x<=m)
        num+=Query(x,y,l,m,rt<<1);
    if(y>m)
        num+=Query(x,y,m+1,r,rt<<1|1);
    return num;
}
int main()
{
    int n,m,k=0;
    while(scanf("%d",&n)!=EOF)
    {
        int a,b,c;
        Build(1,n,1);
        scanf("%d",&m);
        printf("Case #%d:\n",++k);
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(b>c)
                swap(b,c);
            if(a)
                printf("%lld\n",Query(b,c,1,n,1));
            else
                Update(b,c,1,n,1);
        }
        printf("\n");
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值