【线段树】 SPOJ 2713 Can you answer these queries IV

更新操作:一段区间内的全部数开根号

查询操作:一段区间内的sum值


在一个值更新操作十几次之后会变成 1 之后就不需要再更新这段了

所以在update 某个区间 r-l+1==sum[rt] j就return

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define FOR(i,a,b)  for(int i=a;i<=b;i++)
#define IN   freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 120000;
const int MAXM = 200000;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const double eps= 1e-8;
#define lson l,m, rt<<1
#define rson m+1,r,rt<<1|1
LL sum[MAXN<<2];
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(lson);
    build(rson);
    pushup(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return sum[rt];
    int m=(l+r)>>1;
    LL res=0;
    if(L<=m) res+=query(L,R,lson);
    if(R>m) res+=query(L,R,rson);
    return res;
}
void update(int L,int R,int l,int r,int rt)
{
    if(r-l+1==sum[rt]&&L<=l && r<=R)
        return ;
    if(l==r)
    {
        sum[rt]=(LL)sqrt(sum[rt]*1.0);
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m) update(L,R,lson);
    if(R>m) update(L,R,rson);
    pushup(rt);
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    int m,n,cas=0,flag=0;
    while(scanf("%lld",&n)!=EOF)
    {
        build(1,n,1);
        scanf("%d",&m);
        printf("Case #%d:\n",++cas);
        for(int i=0;i<m;i++)
        {
            int ls,rs,op;
            scanf("%d%d%d",&op,&ls,&rs);
            if(rs<ls)swap(ls,rs);
            if(op)
                printf("%lld\n",query(ls,rs,1,n,1));
            else update(ls,rs,1,n,1);
        }
        puts("");
    }
    return 0;
}


转载于:https://www.cnblogs.com/kewowlo/p/4088319.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值