HDU 1027 G - Can you answer these queries?
Posted stupid_one
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
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.
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.
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 = 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 local freopen("data.txt","r",stdin); #endif while (scanf("%d", &n) != EOF) { work(); printf("\\n"); } return 0; }
以上是关于HDU 1027 G - Can you answer these queries?的主要内容,如果未能解决你的问题,请参考以下文章
hdu 4027 Can you answer these queries?
HDU 2141 Can you find it? 二分查找