zoukankan      html  css  js  c++  java
  • spoj GSS4

    GSS4 - Can you answer these queries IV

    http://www.spoj.com/problems/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
    
    
     题意:
    区间开根
    区间求和
     
    线段树
    x<=10^18,最多开7、8次就变成1,所以直接单点该
    当一个区间全是1的时候直接return
    总复杂度:8nlogn
     
    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    #define N 100001
    using namespace std;
    int n,m,opl,opr,type;
    long long sum[N*4];
    long long ans;
    bool v[N];
    void read(long long &x)
    {
        x=0; int f=1; char c=getchar();
        while(c<'0'|| c>'9')  { if(c=='-') f=-1; c=getchar(); }
        while(c>='0' && c<='9') { x=x*10+c-'0'; c=getchar(); }
        x*=f;
    }
    void build(int k,int l,int r)
    {
        sum[k]=0;
        if(l==r)    {read(sum[k]); return;    }
        int mid=l+r>>1;
        build(k<<1,l,mid);
        build(k<<1|1,mid+1,r);
        sum[k]=sum[k<<1]+sum[k<<1|1];
    }
    void change(int k,int l,int r)
    {
        if(sum[k]==r-l+1) return;
        if(l==r)  { sum[k]=sqrt(sum[k]); return; }
        int mid=l+r>>1;
        if(opl<=mid) change(k<<1,l,mid);
        if(opr>mid) change(k<<1|1,mid+1,r);
        sum[k]=sum[k<<1]+sum[k<<1|1];
    }
    void query_sum(int k,int l,int r)
    {
        if(l>=opl && r<=opr)
        {
            ans+=sum[k];
            return;
        }
        int mid=l+r>>1;
        if(opl<=mid) query_sum(k<<1,l,mid);
        if(opr>mid) query_sum(k<<1|1,mid+1,r);
    }
    void out(long long x)
    {
        if(x/10) out(x/10);
        putchar(x%10+'0');
    }
    int main()
    {
        int cas=0;
        while(scanf("%d",&n)!=EOF)
        {
            printf("Case #%d:
    ",++cas);
            build(1,1,n);
            scanf("%d",&m);
            while(m--)
            {
                scanf("%d%d%d",&type,&opl,&opr);
                if(opl>opr) swap(opl,opr);
                if(!type) change(1,1,n);
                else { ans=0; query_sum(1,1,n); out(ans); puts(""); }
            }
        }
    }
     
     
  • 相关阅读:
    K8S--架构及基本概念
    虚拟化及云原生的一些概念
    JPA事务中的异常最后不也抛出了,为什么没被catch到而导致回滚?
    OpenFaaS实战之六:of-watchdog(为性能而生)
    OpenFaaS实战之五:大话watchdog
    OpenFaaS实战之四:模板操作(template)
    OpenFaaS实战之三:Java函数
    OpenFaaS实战之二:函数入门
    OpenFaaS实战之一:部署
    Python
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7191265.html
Copyright © 2011-2022 走看看