spoj2713(线段树)

来源:互联网 发布:柴犬为什么会笑 知乎 编辑:程序博客网 时间:2024/06/06 03:47

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. Onthis sequence you have to apply M (M <= 100,000)operations:

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

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

Input

Multiple test cases, please proceed them one by one. Inputterminates by EOF.

For each test case:

The first line contains an integer N. The following linecontains N integers, representing the sequenceA1..AN.
The third line contains an integer M. The next M lines contain theoperations 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 ofoutput. Then for each query, print an integer as the problemrequired.

Print an blank line after each test case.

See the sample output for more details.

Example

Input:51 2 3 4 551 2 40 2 41 2 40 4 51 1 5410 10 10 1031 1 40 2 31 1 4Output:Case #1:946Case #2:4026题意:
操作:
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;
}
原创粉丝点击