Can you answer these queries?

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5195

Description

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

Input

The 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 263.
  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.

Output

For 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

HINT

题意

线段树 维护区间开根号,查询区间和

题解:

直接搞就好了,直接区间维护线段和,然后开根号,要注意的一点就是,int范围内的数,最多开7次根号

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
//**************************************************************************************
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
} struct node
{
int l,r;
ll cnt,sum;
};
node a[maxn*];
ll d[maxn];
void build(int x,int l,int r)
{
a[x].l=l,a[x].r=r;
a[x].cnt=;
if(l==r)
a[x].sum=d[l];
else
{
int mid=(l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
a[x].sum=a[x<<].sum+a[x<<|].sum;
}
}
void pushup(int x)
{
a[x].sum=a[x<<].sum+a[x<<|].sum;
if(a[x<<].cnt>=&&a[x<<|].cnt>=)
a[x].cnt=;
}
void update(int x,int st,int ed)
{
int l=a[x].l,r=a[x].r;
if(a[x].cnt>=)
{
a[x].sum=r-l+;
return;
}
if(st<=l&&r<=ed)
{
a[x].cnt+=;
if(l==r)
a[x].sum=sqrt(a[x].sum);
else
{
update(x<<,st,ed);
update(x<<|,st,ed);
pushup(x);
}
} else
{
int mid=(l+r)>>;
if(st<=mid) update(x<<,st,ed);
if(ed> mid) update(x<<|,st,ed);
pushup(x);
}
}
ll query(int x,int st,int ed)
{
int l=a[x].l,r=a[x].r;
if(st<=l&&r<=ed)
return a[x].sum;
else
{
int mid=(l+r)>>;
ll sum1=,sum2=;
if(st<=mid)
sum1=query(x<<,st,ed);
if(ed>mid)
sum2=query(x<<|,st,ed);
return sum1+sum2;
}
}
int n,m,f,b,c;
int main()
{ int cas=;
while(scanf("%d",&n)!=EOF)
{
memset(d,,sizeof(d));
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
d[i]=read();
printf("Case #%d:\n",cas++);
scanf("%d",&m);
build(,,n);
for(int i=;i<m;i++)
{
f=read(),b=read(),c=read();
if(b>c)swap(b,c);
if(f==)update(,b,c);
else printf("%lld\n",query(,b,c));
}
puts("");
}
}

hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和的更多相关文章

  1. HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

    题目 线段树 简单题意: 区间(单点?)更新,区间求和  更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...

  2. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...

  3. hdu 4027 Can you answer these queries? 线段树

    线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...

  4. HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)

    题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...

  5. hdu 4027 Can you answer these queries?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...

  6. HDU 4027 Can you answer these queries?(线段树区间开方)

    Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K ...

  7. HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】

    Can you answer these queries? Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & ...

  8. hdu 4027 Can you answer these queries? (区间线段树,区间数开方与求和,经典题目)

    Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K ...

  9. HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)

    题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...

随机推荐

  1. struts集合类型封装

    1.list类型封装

  2. linux device tree源代码解析--转

    //Based on Linux v3.14 source code Linux设备树机制(Device Tree) 一.描述 ARM Device Tree起源于OpenFirmware (OF), ...

  3. Python3中字符串的编码与解码以及编码之间转换(decode、encode)

    一.编码 二.编码与解码 Python3中对py文件的默认编码是urf-8.但是字符串的编码是Unicode. 由于Unicode采用32位4个字节来表示一个字符,存储和传输太浪费资源,所以传输和存储 ...

  4. 设置NGINX进程分配至多核CPU提升性能

    Nginx 配置文件 nginx.conf 首先需要找到 Nginx 的配置文件 nginx.conf 才能进行下面的操作,在LNMP一键安装包默认配置下,nginx.conf 存放在/usr/loc ...

  5. linux下rz,sz安装

    1.sz rz yum安装 yum install lrzsz

  6. log优化

    isLoggable(Level level) 包含计算的日志记录用isLoggable判断下. debug  info warn   error   ,一般记录error,  但是其他里面的计算还是 ...

  7. PHP下利用PHPMailer

    PHPMailer有什么优点? 可运行在任何平台之上 支持SMTP验证 发送邮时指定多个收件人,抄送地址,暗送地址和回复地址:注:添加抄送.暗送仅win平台下smtp方式支持 支持多种邮件编码包括:8 ...

  8. CSU 1102 多连块拼图

    多连块拼图 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述     多连块是指由多个等大正方形边与边连接而成的平面连通图形.         ———— 维基百科      ...

  9. webStorage,离线缓存

    一.webStorage 1.目标 1.了解cookie的不足之处,引入webstorage的概念    2.学习并且掌握webstorage有哪两种    3.学习并且掌握sessionStorag ...

  10. 基于vue2.0的后管系统(配置篇)

    一些项目依赖package.json { "name": "frontend", "description": "tssp bas ...