zoukankan      html  css  js  c++  java
  • hdu4027 Can you answer these queries? 线段树

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
    You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

    Notice that the square root operation should be rounded down to integer.

    题意:有一列数,共两个操作,将区间每个数开成平方根(取四舍五入),和求区间和。

    线段树,由于四舍五入开根,最后一定会开成1,所以记录区间和以及区间内是否全是1,若全是1则不需要再进行开根操作了。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 using namespace std;
     5 typedef long long ll;
     6 const int maxm=100005;
     7 
     8 ll st[maxm<<2],a[maxm];
     9 bool ch[maxm<<2];
    10 int ql,qr;
    11 
    12 void build(int o,int l,int r){
    13     if(l==r){
    14         st[o]=a[l];
    15         if(st[o]==1)ch[o]=1;
    16         else ch[o]=0;
    17         return;
    18     }
    19     int m=l+((r-l)>>1);
    20     build(o<<1,l,m);
    21     build(o<<1|1,m+1,r);
    22     st[o]=st[o<<1]+st[o<<1|1];
    23     ch[o]=ch[o<<1]&ch[o<<1|1];
    24 }
    25 
    26 void update(int o,int l,int r){
    27     if(ch[o]==1)return;
    28     if(l==r){
    29         st[o]=sqrt(st[o]*1.0);
    30         if(st[o]==1)ch[o]=1;
    31         else ch[o]=0;
    32         return;
    33     }
    34     int m=l+((r-l)>>1);
    35     if(ql<=m)update(o<<1,l,m);
    36     if(qr>=m+1)update(o<<1|1,m+1,r);
    37     st[o]=st[o<<1]+st[o<<1|1];
    38     ch[o]=ch[o<<1]&ch[o<<1|1];
    39 }
    40 
    41 ll query(int o,int l,int r){
    42     if(ql<=l&&qr>=r)return st[o];
    43     int m=l+((r-l)>>1);
    44     ll ans=0;
    45     if(ql<=m)ans+=query(o<<1,l,m);
    46     if(qr>=m+1)ans+=query(o<<1|1,m+1,r);
    47     return ans;
    48 }
    49 
    50 int main(){
    51     int n;
    52     int cnt=0;
    53     while(scanf("%d",&n)!=EOF){
    54         int i;
    55         for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
    56         build(1,1,n);
    57         int m;
    58         scanf("%d",&m);
    59         printf("Case #%d:
    ",++cnt);
    60         for(i=1;i<=m;++i){
    61             int f;
    62             scanf("%d%d%d",&f,&ql,&qr);
    63             if(ql>qr){
    64                 int t=ql;ql=qr;qr=t;
    65             }
    66             if(f==1)printf("%I64d
    ",query(1,1,n));
    67             else update(1,1,n);
    68         }
    69         printf("
    ");
    70     }
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    分享一组很赞的 jQuery 特效【附源码下载】
    HTML5 Canvas 实现的9个 Loading 效果
    Charted – 自动化的可视化数据生成工具
    jQuery Countdown Timer 倒计时效果
    Droidicon – 1600+ 漂亮的 Android 图标
    tiltShift.js
    推荐12个最好的 JavaScript 图形绘制库
    Elastic Image Slider 带缩略图功能的幻灯片
    12款高质量的响应式 HTML5/CSS3 网站模板
    7种鼠标悬停效果,多样的图片说明展示
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6597958.html
Copyright © 2011-2022 走看看