HDU 1027 G - Can you answer these queries?
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
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.
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.
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
19
7
6
虽然这个过了而且也写过题解。但是还是再写一次。
因为:
它输入的区间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?的更多相关文章
- 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 ...
- hdu 4027 Can you answer these queries?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...
- 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?——————【线段树区间开方,区间求和】
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 ...
- Can you answer these queries? HDU 4027 线段树
Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...
- V - Can you answer these queries? HDU - 4027 线段树 暴力
V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...
- 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 ...
- 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 ...
随机推荐
- 【HDU 6126】Give out candies 最小割
题意 有$n$个小朋友,给每个人分$1~m$个糖果,有k个限制 限制形如$(x,y,z)$ 表示第$x$个人分到的糖数减去第$y$个人分到的糖数不大于$z$,给第$i$个人$j$颗糖获 ...
- hdu 6121 Build a tree
/** * 题意:一棵 n 个点的完全 k 叉树,结点标号从 0 到 n - 1,求以每一棵子树的大小的异或和. * 解法:k叉树,当k=1时,特判,用xorn函数,具体解释:http://blog. ...
- python下setuptools安装
python下的setuptools带有一个easy_install的工具,在安装python的每三方模块.工具时很有用,也很方便.安装setuptools前先安装pip,请参见<pytho ...
- Codeforces617E XOR and Favorite Number(分块 异或)
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...
- 「LOJ#10072」「一本通 3.2 例 1」Sightseeing Trip(无向图最小环问题)(Floyd
题目描述 原题来自:CEOI 1999 给定一张无向图,求图中一个至少包含 333 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方 ...
- bzoj3456城市规划 多项式取模
題目大意 求出有n个点的有标号简单连通无向图的数目. 题解 什么破玩意,直接输出\(2^{C_n^2}\)走人 我们发现这张图要求连通,而上式肯定不能保证连通. 其实上式表示的是不保证连通的有标号简单 ...
- JSOI2008星球大战——联通块数量
题目:https://www.luogu.org/problemnew/show/1197 此题不能按时间顺序进行删点.求连通块数量,而应打破时间的思维,先形成一张没有要删去的点的图,再从后往前逐个加 ...
- tyvj1061移动服务——DP
题目:http://www.joyoi.cn/problem/tyvj-1061 DP记录状态为当前任务时不在此任务位置上的两个人的位置(因为一定有一个人在此任务位置上): 不妨设初始位置p[0]=3 ...
- 面向对象(封装)get set的由来
封装:是指隐藏对象的属性和实现细节,进对外提供公共访问方式 好处:将变化隔离.便于使用,提高重用性,提高安全性 封装原则:将不需要对外提供的额内容隐藏起来,把属性都隐藏,提供公共方法对其访问 pack ...
- CentOS下安装配置Samba服务器
0 环境介绍 VMWARE12下安装的CENTOS7虚拟机.宿主机为WIN7. 1 离线安装 费了九牛二虎之力,下载各种依赖,还是有问题,转向在线安装. 2 在线安装 虚拟机采用默认的配置: 其次,网 ...