zoukankan      html  css  js  c++  java
  • SPOJ GSS4 Can you answer these queries IV

    Can you answer these queries IV

    Time Limit: 5000ms
    Memory Limit: 262144KB
    This problem will be judged on SPOJ. Original ID: GSS4
    64-bit integer IO format: %lld      Java class name: Main

    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
    
    
     

    Source

     
    解题:线段树
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 100010;
     5 struct node{
     6     int lt,rt;
     7     LL sum;
     8 }tree[maxn<<2];
     9 inline void pushup(int v){
    10     if(tree[v<<1].sum == tree[v<<1|1].sum) tree[v].sum = tree[v<<1].sum;
    11     else tree[v].sum = -1;
    12 }
    13 void build(int lt,int rt,int v){
    14     tree[v].lt = lt;
    15     tree[v].rt = rt;
    16     if(lt == rt){
    17         scanf("%lld",&tree[v].sum);
    18         return;
    19     }
    20     int mid = (lt + rt)>>1;
    21     build(lt,mid,v<<1);
    22     build(mid + 1,rt,v<<1|1);
    23     pushup(v);
    24 }
    25 inline void pushdown(int v){
    26     if(tree[v].sum != -1){
    27         tree[v<<1].sum = tree[v].sum;
    28         tree[v<<1|1].sum = tree[v].sum;
    29     }
    30 }
    31 void update(int lt,int rt,int v){
    32     if(tree[v].sum == 1) return;
    33     if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].sum != -1){
    34         tree[v].sum = sqrt(tree[v].sum);
    35         return;
    36     }
    37     pushdown(v);
    38     if(lt <= tree[v<<1].rt) update(lt,rt,v<<1);
    39     if(rt >= tree[v<<1|1].lt) update(lt,rt,v<<1|1);
    40     pushup(v);
    41 }
    42 LL query(int lt,int rt,int v){
    43     if(tree[v].sum != -1) return tree[v].sum*(rt - lt + 1);
    44     if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].sum != -1)
    45         return tree[v].sum*(tree[v].rt - tree[v].lt + 1);
    46     pushdown(v);
    47     if(rt <= tree[v<<1].rt) return query(lt,rt,v<<1);
    48     if(lt >= tree[v<<1|1].lt) return query(lt,rt,v<<1|1);
    49     return query(lt,tree[v<<1].rt,v<<1) + query(tree[v<<1|1].lt,rt,v<<1|1);
    50     pushup(v);
    51 }
    52 int main(){
    53     int n,m,op,x,y,cs = 1;
    54     while(~scanf("%d",&n)){
    55         build(1,n,1);
    56         scanf("%d",&m);
    57         printf("Case #%d:
    ",cs++);
    58         while(m--){
    59             scanf("%d%d%d",&op,&x,&y);
    60             if(x > y) swap(x,y);
    61             if(op) printf("%lld
    ",query(x,y,1));
    62             else update(x,y,1);
    63         }
    64         putchar('
    ');
    65     }
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    Linux权限对文件或者目录的影响
    Linux基础命令chmod(什么是权限?如何设置权限)
    Shell编程——素数的判断
    Shell编程——阶乘
    Shell编程——回文数
    汇编(8253计数器)
    汇编(8255A的工作方式1)
    汇编(8255A的方式0)
    汇编(程序查询方式控制输入输出)
    Shell函数、接受用户输入
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4888023.html
Copyright © 2011-2022 走看看