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

Time limit 500 ms
Memory limit 1572864 kB

You are given a sequence A of N(N <= 100,000) positive integers. There sum will be less than 1018. On this sequence you have to apply M (M <= 100,000) operations:

(A) For given x,y, for each elements between the x-th and the y-th ones (inclusively, counting from 1), modify it to its positive square root (rounded down to the nearest integer).

(B) For given x,y, query the sum of all the elements between the x-th and the y-th ones (inclusively, counting from 1) in the sequence.

Input

Multiple test cases, please proceed them one by one. Input terminates by EOF.

For each test case:

The first line contains an integer N. The following line contains N integers, representing the sequence A1…AN.
The third line contains an integer M. The next M lines contain the operations in the form “i x y”.i=0 denotes the modify operation, i=1 denotes the query operation.

Output

For each test case:

Output the case number (counting from 1) in the first line of output. Then for each query, print an integer as the problem required.

Print an blank line after each test case.

See the sample output for more details.


题目分析

区间和不支持直接开方,但是我们注意到 1 0 18 10^{18} 1018内的数据至多开方6次即可得1
所以我们在维护区间和的同时再维护一个区间最大值
对于区间最大值为1的区间可以不用更新,剩下的单点修改


#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;

lt read()
{
    lt f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=100010;
int n,m,cs;
lt a[maxn],mx[maxn<<2],sum[maxn<<2];

void build(int s,int t,int p)
{
    if(s==t){ mx[p]=sum[p]=a[s]; return;}
    int mid=s+t>>1;
    build(s,mid,p<<1); build(mid+1,t,p<<1|1);
    mx[p]=max(mx[p<<1],mx[p<<1|1]);
    sum[p]=sum[p<<1]+sum[p<<1|1];
}

lt qsum(int ll,int rr,int s,int t,int p)
{
    if(ll<=s&&t<=rr) return sum[p];
    lt mid=s+t>>1,ans=0;
    if(ll<=mid) ans+=qsum(ll,rr,s,mid,p<<1);
    if(rr>mid) ans+=qsum(ll,rr,mid+1,t,p<<1|1);
    return ans;
}

void update(int ll,int rr,int s,int t,int p)
{
    if(s==t){ a[s]=sqrt(a[s]); sum[p]=mx[p]=a[s]; return;}
    int mid=s+t>>1;
    if(ll<=mid&&mx[p<<1]>1) update(ll,rr,s,mid,p<<1);
    if(rr>mid&&mx[p<<1|1]>1) update(ll,rr,mid+1,t,p<<1|1);
    mx[p]=max(mx[p<<1],mx[p<<1|1]);
    sum[p]=sum[p<<1]+sum[p<<1|1];
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;++i) a[i]=read();
        build(1,n,1);
        
        printf("Case #%d:\n",++cs);
        m=read();
        while(m--)
        {
            int k=read(),ll=read(),rr=read();
            if(ll>rr) swap(ll,rr);
            if(k==0) update(ll,rr,1,n,1);
            else if(k==1) printf("%lld\n",qsum(ll,rr,1,n,1));
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值