spoj2713(线段树)

http://www.spoj.pl/problems/GSS4/

SPOJ Problem Set (classical)

2713. Can you answer these queries IV

Problem code: GSS4

 

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.

Example

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

题意:
操作:
1,把[x,y]每个数k变成sqrt(k),向下取整。
2,查询区间的和。
 
这题的操作为开方操作,可以Lazy,可是基本没有什么效果,操作不能进行合并是一个大问题~
但是也正因为这是开方操作,也是突破点
一个64位整数,最多开方7次就变成1了,之后的开方操作也就没有效果了,这就是突破口
如果一个区间全是1了,那就没必要执行开方操作了,而最多只有7次,那就对区间的每一个暴力
加一点优化就是区间的和等于区间的个数,即区间全为1,就不更新,或者记录一个最值,最值为1不更新
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define maxn 100005
struct Node
{
    int left,right;
    LL sum;
}L[maxn*5];
int n,q;
LL a[maxn];
//void Swap(int a,int b)
//{
  //  int t;
    //t=a;
    //a=b;
    //b=t;
//}
void Push_Up(int step)
{
    L[step].sum=L[step*2].sum+L[step*2+1].sum;
}
void build(int step,int l,int r)
{
    L[step].left=l;
    L[step].right=r;
    if(l==r)
    {
        L[step].sum=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(step*2,l,mid);
    build(step*2+1,mid+1,r);
    Push_Up(step);
}
void Update(int step,int l,int r)
{
    if(L[step].right-L[step].left+1==L[step].sum)
    {
        return;
    }
    if(L[step].left==L[step].right)
    {
        L[step].sum=(LL)sqrt(L[step].sum+0.0);
        return;
    }
    int mid=(L[step].left+L[step].right)>>1;
    if(r<=mid)
    {
        Update(step*2,l,r);
    }
    else if(l>mid)
    {
        Update(step*2+1,l,r);
    }
    else
    {
        Update(step*2,l,mid);
        Update(step*2+1,mid+1,r);
    }
    Push_Up(step);
}
LL Query(int step,int l,int r)
{
    if(L[step].left==l&&L[step].right==r)
    {
        return L[step].sum;
    }
    int mid=(L[step].left+L[step].right)>>1;
    if(r<=mid)
    {
        return Query(step*2,l,r);
    }
    else if(l>mid)
    {
        return Query(step*2+1,l,r);
    }
    else
    {
        return Query(step*2,l,mid)+Query(step*2+1,mid+1,r);
    }
}
int main()
{
    int cas=0;
    while(scanf("%d",&n)!=EOF)
    {
        int i;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        build(1,1,n);
        scanf("%d",&q);
        printf("Case #%d:\n",++cas);
        //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,l,r);
            }
            else
            {
                printf("%lld\n",Query(1,l,r));
            }
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值