Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
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
题意:
就是让我们求一个区间的耐久度,耐久度每次变成本身的根号值的。
思路:
直接写个线段树就行了,线段树怎么写我就不说了,我来分析分析我错误的坑点:
1 这个给你的区间l 和 r有可能 l > r ,所以要进行判断,然后交换,否则会RE(我还来还以为是数组开小了)。
2 如果是不剪枝的话,会TLE,所以再更新的时候如果sum(区间总和) = len(区间长度)那么就说明区间里全部都是1,所以就不用再往下判断了。
3 最后的时候还要输出一个换行,否则会PE。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e+5 + 10;
struct NODE {
int l;
int r;
int len;
ll sum;
int mid () {
return (l + r) >> 1;
}
};
NODE node[maxn << 2];
void PushUp(int rt) {
node[rt].sum = node[rt << 1].sum + node[rt << 1 | 1].sum;
}
void BuildTree(int l, int r, int rt) {
node[rt].l = l;
node[rt].r = r;
node[rt].len = r - l + 1;
if (l == r) {
scanf("%lld", &node[rt].sum);
return ;
}
int mid = node[rt].mid();
BuildTree(l, mid, rt << 1);
BuildTree(mid + 1, r, rt << 1 | 1);
PushUp(rt);
}
void Updata(int l, int r, int rt) {
if (node[rt].len == node[rt].sum) return ;
if (node[rt].l == node[rt].r && node[rt].l >= l && node[rt].r <= r) {
node[rt].sum = sqrt(node[rt].sum * 1.0);
return ;
}
int mid = node[rt].mid();
if (r <= mid) Updata(l, r, rt << 1);
else if (l > mid) Updata(l, r, rt << 1 | 1);
else {
Updata(l, mid, rt << 1);
Updata(mid + 1, r, rt << 1 | 1);
}
PushUp(rt);
}
ll ans = 0;
void Query(int l, int r, int rt) {
if (node[rt].l == l && node[rt].r == r) {
ans += node[rt].sum;
return ;
}
int mid = node[rt].mid();
if (r <= mid) Query(l, r, rt << 1);
else if (l > mid) Query(l , r, rt << 1 | 1);
else {
Query(l, mid, rt << 1);
Query(mid + 1, r , rt << 1 | 1);
}
}
int main() {
ios::sync_with_stdio(false);
int n, m, k, Case = 0;
while (scanf("%d", &n) != EOF) {
BuildTree(1, n, 1);
scanf("%d", &m);
printf("Case #%d:\n", ++Case);
while (m--) {
int l, r;
scanf("%d %d %d", &k, &l, &r);
if (l > r) swap(l, r);
if (k == 0) {
Updata(l, r, 1);
} else {
ans = 0;
Query(l, r, 1);
printf("%lld\n", ans);
}
}
printf("\n");
}
return 0;
}