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(""); }
            }
        }
    }
     
     
  • 相关阅读:
    用DOS把一个文件夹下的所有文件名打印到txt文件里
    多线程时的CreateDispatch问题
    [转载]惟楚有才
    [转] 数学专业参考书整理推荐
    大整数处理类(cpp文件)
    [转]英文文献检索
    大整数处理类(头文件)
    国外遥感GIS期刊整理(转)
    [转载]Hibernate中No row with the given identifier exis
    在武汉,总有那么一个词~~~
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7191265.html
Copyright © 2011-2022 走看看