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
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest

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

因为:

它输入的区间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 = 100000 + 20;
LL sum[maxn << 2];
bool book[maxn << 2];
void pushUp(int cur) {sum[cur] = sum[cur << 1] + sum[cur << 1 | 1];book[cur] = book[cur << 1] && book[cur << 1 | 1];
}
void build(int L, int R, int cur) {book[cur] = 0;if (L == R) {cin >> sum[cur];return ;}int mid = (L + R) >> 1;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] == 1) book[cur] = 1;return;}int mid = (L + R) >> 1;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) >> 1;LL ans = 0;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(1, n, 1);int q;cin >> q;for (int i = 1; i <= q; ++i) {int flag, L, R;scanf("%d%d%d", &flag, &L, &R);if (L > R) swap(L, R);if (flag == 0) upDate(L, R, 1, n, 1);else {cout << query(L, R, 1, n, 1) << endl;}}
}int main() {
#ifdef localfreopen("data.txt","r",stdin);
#endifwhile (scanf("%d", &n) != EOF) {work();printf("\n");}return 0;
}

View Code

转载于:https://www.cnblogs.com/liuweimingcprogram/p/5966608.html

HDU 1027 G - Can you answer these queries?相关推荐

  1. HDU 4027 Can you answer these queries?(线段树/区间不等更新)

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

  2. 线性代数四之动态DP(广义矩阵加速)——Can you answer these queries III,保卫王国

    动态DP--广义矩阵加速 SP1716 GSS3 - Can you answer these queries III description solution code [NOIP2018 提高组] ...

  3. SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并

    Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...

  4. SPOJ GSS2 Can you answer these queries II (线段树离线) - xgtao -

    Can you answer these queries II 这是一道线段树的题目,维护历史版本,给出N(<=100000)个数字(-100000<=x<=100000),要求求出 ...

  5. HDU 1027 全排列

    这是一道排列的题目,北大的程序设计书里面有这道题,讲的挺细的,可以去看看,两种方法 /*  * Author:lonelycatcher  * Problem:HDU 1027  * Type:全排列 ...

  6. GSS2 - Can you answer these queries II

    GSS2 - Can you answer these queries II 题意: 给你1e51e51e5 的序列,每次询问区间l到rl到rl到r,每个相同的数只算一次的最大子段和. 思路: 乍一眼 ...

  7. hdu - 4027 Can you answer these queries?

    http://acm.hdu.edu.cn/showproblem.php?pid=4027 /** * 题意:给你n个数,对这些数进行操作,有m组操作 * q == 0 [x,y]区间内的每个数开方 ...

  8. hdu 4027 Can you answer these queries?

    http://acm.hdu.edu.cn/showproblem.php?pid=4027 [更新区间,查询区间]的线段树,必须抓住修改6次以后每个数必然会变成1,然后以后的修改都将不起作用,在此之 ...

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

    题目链接:点击查看 题目大意:给定n艘敌军的舰队,每艘舰队都有一定的耐力值,随后进行m次操作,共包括两种操作,分别是输出区间[l,r]中的耐力 值之和,以及将区间[l,r]中的每个的耐力值都开平方 题 ...

最新文章

  1. WordCount程序
  2. Delphi - 我的代码之窗体移动
  3. MVP模式在Android实际项目中的应用和优化
  4. vs android 打电话,iOS vs. Android:二者真的是在竞争吗?
  5. (问题)c语言现代方法2th,自己编写的reminder.c程序 找错/修改/拓展延伸
  6. 学习Spring-Cloud –编写微服务
  7. 源码安装mysql_CentOS 7中源码安装MySQL 5.7.16 (亲测成功)
  8. Rtworld防洪系统全解开源完整源码
  9. 【Away3D代码解读】(一):主要类及说明
  10. python学习模型_python学习笔记(IO模型)
  11. 微信小程序之----audio音频播放
  12. 校园edu无网络访问解决方案
  13. HTML+JS调用摄像头
  14. 电机系列(1) - foc最基本原理、clark变换 、park变换、附代码
  15. 践行快乐音乐教育 郎朗The ONE全球公益钢琴盛典传递音乐力量!
  16. QT Designer中编辑菜单栏技巧
  17. 笔记本计算机没有没有显示无线网络连接,笔记本没有无线网络连接,教您笔记本没有无线网络连接...
  18. 博客园Training Team加入申请帖
  19. 科普:什么是CPU?CPU和芯片关系?CPU怎么做的?CPU有什么用?不同CPU有什么区别?我们怎么选CPU?
  20. 装系统时无法创建新的分区

热门文章

  1. 人生一知己,足以慰风尘吗?
  2. 盼望的意思是什么,怎么用盼望造句?
  3. 现在很多人都在网上找富业
  4. IDEA开发中常用快捷键
  5. SQL Server中的查询优化技术:基础
  6. aws cli_学习AWS CLI:AWS CLI概述(AWS命令行界面)
  7. 使用ssms异机还原数据库_使用SSMS 18中的查询优化助手向导进行数据库升级
  8. 201671010430 司昕劼 实验十四 课程学习总结
  9. 前端工作学习相关网站收集整理
  10. 通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明