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 2 63.
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
题意:
给你一个区间,0 x y表示区间[x, y]内的所有数开根号,1 x y表示统计区间[x, y]和;
分析:
解题关键数值范围在64位以内,每个数开根号操作最多进行7次,就会归为1,所以updata过程你可以对区间进行单点操作。dp统计区间和,剪枝操作很重要,降低很多复杂度,查询区间和=区间长度,直接停止query和updata;
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
LL dp[MAXN << 2];
void pushup(int root) {
dp[root] = dp[root << 1] + dp[root << 1 | 1];
}
void build(int root, int L, int R) {
if(L == R) {
scanf("%lld", &dp[root]);
return ;
}
int mid = (L + R) >> 1;
build(root << 1, L, mid);
build(root << 1 | 1, mid + 1, R);
pushup(root);
}
void updata(int root, int L, int R, int l, int r) {
if(R - L + 1 == dp[root]) { //dp[i] = 1,不需要在进行更新,剪枝操作
return;
}
if(L == R) { //区间单点更新
dp[root] = sqrt((double)dp[root]);
return;
}
int mid = (L + R) >> 1;
if(l <= mid) updata(root << 1, L, mid, l, r);
if(r > mid) updata(root << 1 | 1, mid + 1, R, l, r);
pushup(root);
}
LL query(int root, int L, int R, int l, int r) {
if(R - L + 1 == dp[root]) { //不需要向下query,直接返回你要查询的区间范围值
return (min(R, r) - max(l, L) + 1);
}
if(L >= l && R <= r) {
return dp[root];
}
LL ans = 0;
int mid = (L + R) >> 1;
if(l <= mid) ans += query(root << 1, L, mid, l, r);
if(r > mid) ans += query(root << 1 | 1, mid + 1, R, l, r);
return ans;
}
int main() {
int n, m;
while(scanf("%d", &n) != EOF) {
build(1, 1, n);
scanf("%d", &m);
static int p = 1;
printf("Case #%d:\n", p++);
while(m--) {
int x, y, c;
scanf("%d", &c);
if(c) {
scanf("%d %d", &x, &y);
printf("%lld\n", query(1, 1, n, min(x, y), max(x, y))); //x,y大小未知,有点坑
}
else {
scanf("%d %d", &x, &y);
updata(1, 1, n, min(x, y), max(x, y));
}
}
puts("");
}
return 0;
}