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

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 14057    Accepted Submission(s): 3264

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
 
Source
 

虽然这个过了而且也写过题解。但是还是再写一次。

因为:

它输入的区间L和R大小可能不是固定的L < R,要手动判定一下。

那么我以后做题,也要这样。因为我遇过太多这些坑了。

无论数据有没有,我都判定一下

 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#define lson L, mid, cur << 1
#define rson mid + 1, R, cur << 1 | 1
const int maxn = + ;
LL sum[maxn << ];
bool book[maxn << ];
void pushUp(int cur) {
sum[cur] = sum[cur << ] + sum[cur << | ];
book[cur] = book[cur << ] && book[cur << | ];
}
void build(int L, int R, int cur) {
book[cur] = ;
if (L == R) {
cin >> sum[cur];
return ;
}
int mid = (L + R) >> ;
build(lson);
build(rson);
pushUp(cur);
}
void upDate(int begin, int end, int L, int R, int cur) {
if (book[cur]) return;
if (L == R) {
sum[cur] = sqrt(sum[cur]);
if (sum[cur] == ) book[cur] = ;
return;
}
int mid = (L + R) >> ;
if (begin <= mid) upDate(begin, end, lson);
if (end > mid) upDate(begin, end, rson);
pushUp(cur);
}
LL query(int begin, int end, int L, int R, int cur) {
if (L >= begin && R <= end) {
return sum[cur];
}
int mid = (L + R) >> ;
LL ans = ;
if (begin <= mid) ans += query(begin, end, lson);
if (end > mid) ans += query(begin, end, rson);
return ans;
}
int f;
int n;
void work() {
printf("Case #%d:\n", ++f);
build(, n, );
int q;
cin >> q;
for (int i = ; i <= q; ++i) {
int flag, L, R;
scanf("%d%d%d", &flag, &L, &R);
if (L > R) swap(L, R);
if (flag == ) upDate(L, R, , n, );
else {
cout << query(L, R, , n, ) << endl;
}
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
while (scanf("%d", &n) != EOF) {
work();
printf("\n");
}
return ;
}

HDU 1027 G - Can you answer these queries?的更多相关文章

  1. G - Can you answer these queries? & N - 花神游历各国

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

  2. hdu 4027 Can you answer these queries?

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

  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?——————【线段树区间开方,区间求和】

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

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

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

  6. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

  7. V - Can you answer these queries? HDU - 4027 线段树 暴力

    V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...

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

  9. Can you answer these queries? HDU - 4027 (线段树,区间开平方,区间求和)

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

随机推荐

  1. Workerman安装流程

    第一步检测安装环境 curl -Ss http://www.workerman.net/check.php | php 操作结果显示 报错了  需要找到php.ini文件 解决办法如下: 打开 php ...

  2. 1095 Cars on Campus (30)(30 分)

    Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...

  3. MySQL条件判断处理函数_20160925

    MySQL条件判断处理 一.假如我想把salesperson 分成 5组,计算每个销售分组的业绩 首先先将销售分组 SELECT *, CASE WHEN salesperson IN (" ...

  4. 「NOIP2017」「LuoguP3959」 宝藏(爆搜

    题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 nn 个深埋在地下的宝藏屋, 也给出了这 nn 个宝藏屋之间可供开发的mm 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋中的宝藏. ...

  5. Idea无法加载主类

    今天重装了下电脑,运行idea发现各种问题. 直接进主题哈, 遇到三种情况 第一种: 首先查看这里是否有多个,只保留当前需要用工程路径.点击P右边的删除即可 删除后 然后运行是否能运行. 如果没有的话 ...

  6. eclipse安装cppcheck

     简介: cppcheck 是一个 c 和 c++ 的静态的代码检查分析工具,不用运行程序就可以进行代码的检测. 可以检测一般的内存泄漏和程序编码错误 0.安装 cppcheck 1.57版本,这个版 ...

  7. go 时间戳和时间格式的相互转换

    package main import( "fmt" "time" ) func main() { datetime := "2015-01-01 0 ...

  8. HDU 5547 Sudoku (暴力)

    题意:数独. 析:由于只是4*4,完全可以暴力,要注意一下一些条件,比如2*2的小方格也得是1234 代码如下: #pragma comment(linker, "/STACK:102400 ...

  9. mysql由浅入深探究(二)----mysql用户操作

    上一节我们完成了mysql的安装,现在我们将开启实战模式,完成一些基本的mysql操作.这节我们分为一些几个内容: mysql中用户新建 mysql中用户删除 mysql中用户修改 mysql中用户查 ...

  10. CF1119F Niyaz and Small Degrees【treedp+堆】

    如果枚举d来dp,那么就是设f[u][0/1]为u点不断/断掉和父亲的边,然后优先选取f[v][1]+w(u,v)<=f[v][0]的,如果断掉这些度数还是多就用一个堆维护剩下的按f[v][1] ...