zoukankan      html  css  js  c++  java
  • Can you answer these queries? HDU

    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.

    InputThe input contains several test cases, terminated by EOF. 
      For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
      The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63
      The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000) 
      For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive. 
    OutputFor each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.Sample Input

    10
    1 2 3 4 5 6 7 8 9 10
    5
    0 1 10
    1 1 10
    1 1 5
    0 5 8
    1 4 8

    Sample Output

    Case #1:
    19
    7
    6

    题意:有一个长度不超过100000个数组,数组的数大小不超过2^63,且一直是整型,执行两个操作,0操作表示将指定区间内的数都开平方,1操作
    表示查询指定区间的总数和。注意操作的区间x,y,x可能大于y,输出格式按样例,输入以EOF结束。

    思路:首先一个数的被开方次数不会超过7次,因为超过7次后这个数会变成1,因此单曲奖和变成r-l+1时可以直接return ,方便节省时间,
    比较注意的是区间开方问题,由于是开方,依此标记就没用了,改用定区间,单点修改值的方法,具体实现见代码,最后再加一个区间求和就行。
    注意数要开longlong型。

    代码:
      1 #include <cstdio>
      2 #include <fstream>
      3 #include <algorithm>
      4 #include <cmath>
      5 #include <deque>
      6 #include <vector>
      7 #include <queue>
      8 #include <string>
      9 #include <cstring>
     10 #include <map>
     11 #include <stack>
     12 #include <set>
     13 #include <sstream>
     14 #include <iostream>
     15 #define mod 998244353
     16 #define eps 1e-6
     17 #define ll long long
     18 #define INF 0x3f3f3f3f
     19 using namespace std;
     20 
     21 struct node
     22 {
     23     //l表示左边,r表示右边
     24     int l,r;
     25     ll sum;
     26 
     27 };
     28 node no[500000];
     29 ll num[100000];
     30 int q,a,b,c;
     31 //初始化
     32 //k表示当前节点的编号,l表示当前区间的左边界,r表示当前区间的右边界
     33 void build(int k,int l,int r)
     34 {
     35     no[k].l=l;
     36     no[k].r=r;
     37     //如果递归到最低点
     38     if(l==r)
     39     {
     40         no[k].sum=num[l];
     41         return ;
     42     }
     43     //对半分
     44     int mid=(l+r)/2;
     45     //递归到左线段
     46     build(k*2,l,mid);
     47     //递归到右线段
     48     build(k*2+1,mid+1,r);
     49     //更新当前节点的值
     50     no[k].sum=no[k*2].sum+no[k*2+1].sum;
     51 }
     52 //区间单点修改
     53 //从区间1到n中搜索在定区间b,c中的元素,
     54 void change(int k,int l,int r)
     55 {
     56     if(no[k].sum==(no[k].r-no[k].l+1))
     57     {
     58         return ;
     59     }
     60     if(no[k].l==no[k].r)
     61     {
     62         no[k].sum=(ll)sqrt(no[k].sum);
     63         return ;
     64     }
     65     int mid = (no[k].l+no[k].r)/2;
     66     if(b<=mid)
     67     {
     68         change(k*2,l,mid);
     69     }
     70     if(c>mid)
     71     {
     72         change(k*2+1,mid+1,r);
     73     }
     74     //更新当前节点的值
     75     no[k].sum=no[k*2].sum+no[k*2+1].sum;
     76 }
     77 //求区间
     78 ll query(int k,int l,int r)
     79 {
     80     //到对应层时返回值
     81     if(no[k].l==l&&no[k].r==r)
     82     {
     83         return no[k].sum;
     84     }
     85     //取中值
     86     int mid=(no[k].l+no[k].r)/2;
     87     if(r<=mid)
     88     {
     89         return query(k*2,l,r);
     90     }
     91     else if(l>mid)
     92     {
     93         return query(k*2+1,l,r);
     94     }
     95     else
     96     {
     97         return query(k*2,l,mid)+query(k*2+1,mid+1,r);
     98     }
     99 }
    100 
    101 int main()
    102 {
    103     int n;
    104     int ans=1;
    105     while(scanf("%d",&n)!=EOF)
    106     {
    107         memset(num,0,sizeof(num));
    108         memset(no,0,sizeof(no));
    109         printf("Case #%d:
    ",ans++);
    110         for(int i=1;i<=n;i++)
    111         {
    112             scanf("%lld",&num[i]);
    113         }
    114         build(1,1,n);
    115         scanf("%d",&q);
    116         while(q--)
    117         {
    118             scanf("%d %d %d",&a,&b,&c);
    119             if(b>c)
    120             {
    121                 swap(b,c);
    122             }
    123             if(a==0)
    124             {
    125                 change(1,1,n);
    126             }
    127             else if(a==1)
    128             {
    129                 printf("%lld
    ",query(1,b,c));
    130             }
    131         }
    132         printf("
    ");
    133     }
    134 }
  • 相关阅读:
    mysql 数据类型总结
    #微信小程序子传父 #小程序子组件向父组件传值 小程序子组件触发父组件中的事件
    #最近看到了一个写的很棒的系列文章专栏
    《MySQL45讲》读书笔记(四):索引
    《MySQL45讲》读书笔记(六):数据库事务概述
    《MySQL45讲》读书笔记(一):三大日志概述
    Java基础篇(05):函数式编程概念和应用
    数据采集组件:Flume基础用法和Kafka集成
    架构设计:数据服务系统0到1落地实现方案
    Java基础篇(04):日期与时间API用法详解
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11808828.html
Copyright © 2011-2022 走看看