线段树--区间更新区间查询--hdu4027

题意:

给定一串数字,再给定两个操作:

0.查询  x 到 y的和

1.更新 x到 y的每个值 ,使其变为根号倍

Notice that the square root operation should be rounded down to integer.

将有可能变成的小树变为整数


#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
#define lson i<<1,l,m
#define rson i<<1|1,m+1,r
long long a[100010];
long long ans;
struct TreeNode
{
    long long  num;
    int left;
    int right;
    int ac;
}Tree[100010*4];

void Build(int i, int l , int r)
{
    Tree[i].left=l;
    Tree[i].right=r;
    if(l==r)
    {
       Tree[i].num=a[l];
       if(Tree[i].num==1)
        Tree[i].ac=1;
        else
        Tree[i].ac=0;
       return ;
    }
    int m=(l+r)/2;
    Build(lson);
    Build(rson);
    Tree[i].num=Tree[i<<1].num+Tree[i<<1|1].num;
    if(Tree[i*2].ac&&Tree[i*2+1].ac)
       Tree[i].ac=1;
   else Tree[i].ac=0;
}
void U(int i,int l,int r)
{
    if(Tree[i].ac)return;
    if(Tree[i].left==Tree[i].right)
    {
        Tree[i].num=floor(sqrt(Tree[i].num));
        if(Tree[i].num==1)
            Tree[i].ac=1;
        return ;
    }
    int m=(Tree[i].left+Tree[i].right)>>1;
    if(l>m)
    {
        U(i<<1|1,l,r);
    }
    else if(r<=m)
    {
        U(i<<1,l,r);
    }
    else
    {
        U(lson);
        U(rson);
    }
    Tree[i].num=Tree[i<<1].num+Tree[i<<1|1].num;

 if(Tree[i*2].ac&&Tree[i*2+1].ac)
       Tree[i].ac=1;
}

void  Q(int i, int l ,int r)
{
    if(l==Tree[i].left&&r==Tree[i].right)
    {
       ans+=Tree[i].num;
        return ;
    }
    int m=(Tree[i].left+Tree[i].right)>>1;


    if(r<=m)
    {
        Q(i<<1,l,r);
    }
    else if(l>m)
    {
        Q(i<<1|1,l,r);
    }
    else
    {
        Q(lson);
        Q(rson);
    }

}

int main()
{
    int n;
    int flag=0;
    while(scanf("%d",&n)!=EOF)
    {   flag++;
        printf("Case #%d:\n",flag);
        int i;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        Build(1,1,n);
        int t;
        scanf("%d",&t);
        while(t--)
        {
        ans=0;
        int d,x,y;
        scanf("%d%d%d",&d,&x,&y);
        if(x>y)
            swap(x,y);
        if(d==0)
        {
            U(1,x,y);
        }
        else
        {Q(1,x,y);
            printf("%lld\n",ans);
        }
        }
        printf("\n");
    }
   return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值