题目连接

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

Can you answer these queries?

Description

Problem 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 integer.

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

线段树,区间开方求和。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define lc root<<1
#define rc root<<1|1
#define mid ((l+r)>>1)
typedef unsigned long long ull;
const int Max_N = ;
struct Node { ull val; bool flag; };
struct SegTree {
Node seg[Max_N << ];
inline void push_up(int root) {
seg[root].val = seg[lc].val + seg[rc].val;
}
inline void built(int root, int l, int r) {
seg[root].flag = false;
if (l == r) {
scanf("%lld", &seg[root].val);
return;
}
built(lc, l, mid);
built(rc, mid + , r);
push_up(root);
}
inline void update(int root, int l, int r, int x, int y) {
if (x > r || y < l || seg[root].flag) return;
if (l == r) {
seg[root].val = (ull)sqrt((double)seg[root].val);
if ( == seg[root].val) seg[root].flag = true;
return;
}
update(lc, l, mid, x, y);
update(rc, mid + , r, x, y);
push_up(root);
seg[root].flag = seg[lc].flag && seg[rc].flag;
}
inline ull query(int root, int l, int r, int x, int y) {
if (x > r || y < l) return ;
if (x <= l && y >= r) return seg[root].val;
ull v1 = query(lc, l, mid, x, y);
ull v2 = query(rc, mid + , r, x, y);
return v1 + v2;
}
}seg;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, q, a, b, c, k = ;
while (~scanf("%d", &n)) {
seg.built(, , n);
scanf("%d", &q);
printf("Case #%d:\n", k++);
while (q--) {
scanf("%d %d %d", &a, &b, &c);
if (b > c) b ^= c ^= b ^= c;
if (!a) seg.update(, , n, b, c);
else printf("%lld\n", seg.query(, , n, b, c));
}
printf("\n");
}
return ;
}

hdu 4027 Can you answer these queries?的更多相关文章

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

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

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

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

  3. hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和

    Can you answer these queries? Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

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

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

  5. 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 ...

  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?(线段树,区间更新,区间查询)

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

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

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

  9. HDU - 4027 Can you answer these queries?(线段树区间修改)

    https://cn.vjudge.net/problem/HDU-4027 题意 给一个有初始值的数组,存在两种操作,T=0时将[L,R]的值求平方根,T=1时查询[L,R]的和. 分析 显然不符合 ...

随机推荐

  1. C# ref和out的区别

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...

  2. java多态例子

    多态存在的三个必要条件一.要有继承:二.要有重写:三.父类引用指向子类对象. 代码部分: class A { public String show(D obj) { return ("A a ...

  3. python中若干错误

    今天在运行的django的时候一直提示”系统错误“,如下 except Exception, ex: logger.error(printException()) return render_stri ...

  4. Java基础——异常体系

    在Java中,异常对象都是派生于Throwable类的一个实例,Java的异常体系如下图所示: 所有的异常都是由Throwable继承而来,在下一层立即分解为两个分支,Error和Exception. ...

  5. 华为OJ平台——字符串分隔

    题目描述: 连续输入字符串,请按长度为8拆分每个字符创 后输出到新的字符串数组: 长度不是8整数倍的字符串请在后面补数字0,空字符串不处理 输入 连续输入字符串(输入两次,每个字符长长度小于100)输 ...

  6. 复习下 AJAX

    什么是AJAx:Asynchronous Javascript and XML中文意思:异步JavaScript 和XML批一种创建交互式网页应用的网页开发技术.AJAX优点1.Ajax 在本质上是一 ...

  7. [drp 8]get和post的区别,以及乱码问题的解决

    导读:不管是之前做.NET还是现在做Java的项目,都有涉及到get和post请求,第一次遇到的时候,应该是在人事系统的时候,那时候说要总结,结果一直没有总结.现在,做一个初步的总结,连着总结一下提交 ...

  8. udp打洞( NAT traversal )的方法介绍

    http://www.cnblogs.com/whyandinside/archive/2010/12/08/1900492.html http://www.gzsec.com/oldversion/ ...

  9. (笔记)angular 的根据后台StateCode本地显示指定文案

  10. leetcode 100

    100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...