hdu 4027 Can you answer these queries?
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4027
Can you answer these queries?
Description
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?的更多相关文章
- HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】
Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & ...
- HDU 4027 Can you answer these queries?(线段树区间开方)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
- 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 ...
- HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)
题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...
- 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 ...
- hdu 4027 Can you answer these queries? (区间线段树,区间数开方与求和,经典题目)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- HDU - 4027 Can you answer these queries?(线段树区间修改)
https://cn.vjudge.net/problem/HDU-4027 题意 给一个有初始值的数组,存在两种操作,T=0时将[L,R]的值求平方根,T=1时查询[L,R]的和. 分析 显然不符合 ...
随机推荐
- EXT学习之——Ext下拉框绑定以及级联写法
/*******步骤有四个,缺一不可*********/ function () {xxxxxx = Ext.extend(construct, {InitControl: function () { ...
- 学习练习 java20160507作业
第一题 求水仙花的个数: //求水仙花数 int zongshu = 0; for(int i =100; i<=999;i++) { int bai = i/100; //求百位上面的数字 i ...
- awk中怎么比较字符串??
awk -vOFS="_" '{print $1,$2,$3} http://bbs.chinaunix.net/thread-1099655-1-1.html
- [前端 2]常用的JQuery和Dom页面取值与赋值
导读:书到用时方恨少,需要基础知识的时候,才悔恨自己没有总结学习好.前段时间调了好长时间的页面,突然发现自己之前不怎么在意的取值和赋值,真的是自己一个很薄弱的地方,有时候查半天都找不到一个对的,现在用 ...
- 解决Github访问超慢问题[自己留档]
解决Github访问超慢问题 Github is so slowly. 这段时间访问 github 都非常慢,google了一下发现是github某个CDN被伟大的墙屏蔽所致. 出问题的应该是这个CD ...
- jquery ajax 保存讲解
jquery ajax 参数传递与数据保存实例是一款适合于初学者用的,首先我们是讲一下关于如何利用ajax +php进行数据操作,然后再详细的介绍关于jquery ajax的帮助说明. jquery ...
- JS常用的设计模式(8)——访问者模式
GOF官方定义: 访问者模式是表示一个作用于某个对象结构中的各元素的操作.它使可以在不改变各元素的类的前提下定义作用于这些元素的新操作.我们在使用一些操作对不同的 对象进行处理时,往往会根据不同的对象 ...
- JS回车事件
<script type="text/javascript"> //当回车按下时,/=47,*=42,+=43 function keypress(form0){ if ...
- box2d 遍历世界中 body 的方法
大家都知道,循环遍历有很多方法,在box2d中大家普遍使用while和for循环来遍历世界,那什么时候使用for,什么时候使用while呢? 当循环中不设计body删除的时候,使用for循环,即: C ...
- Ajax实现步骤和原理
1.获取ajax异步对象 IE4~IE12 : 使用new ActiveXObject("microsoft.xmlhttp"); 非IE : 使用new XMLH ...