spoj GSS4 - Can you answer these queries IV 线段树

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.

Input:
5
1 2 3 4 5
5
1 2 4
0 2 4
1 2 4
0 4 5
1 1 5
4
10 10 10 10
3
1 1 4
0 2 3
1 1 4

Output:
Case #1:
9
4
6

Case #2:
40
26

由于longlong范围开根号最多开7次,所以最多操作nlogn*7次是可以接受的,所以暴力更新每个点就可以了 当一段都是1就不用更新了。
#include<iostream>
#include<cstdio>
#include<map>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define getmid int mid=(l+r)>>1
typedef long long sint;
#define maxn 4000010
sint sum[maxn];
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    getmid;
    if(l==r)
    {
        scanf("%lld",&sum[rt]);
        return;
    }
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int l,int r,int rt,int x,int y)
{
    if(r-l+1==sum[rt]) return;
    if(l==r)
    {
        sum[rt]=sint(sqrt(sum[rt]));
        return;
    }
    getmid;
    if(x<=mid) update(lson,x,y);
    if(y>mid) update(rson,x,y);
    pushup(rt);
}
sint query(int l,int r,int rt,int x,int y)
{
    if(x<=l&&y>=r)
    {
        return sum[rt];
    }
    getmid;
    sint tmp=0;
    if(x<=mid) tmp+=query(lson,x,y);
    if(y>mid) tmp+=query(rson,x,y);
    return tmp;
}
int main()
{
    int cas=0,n,m,q;
    while(scanf("%d",&n)!=EOF)
    {
        build(1,n,1);
        scanf("%d",&q);
        printf("Case #%d:\n",++cas);
        while(q--)
        {
            int k,l,r;
            scanf("%d%d%d",&k,&l,&r);
            if(l>r) swap(l,r);
            if(k==0) update(1,n,1,l,r);
            else printf("%lld\n",query(1,n,1,l,r));
        }
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值