题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4027
Can you answer these queries?
Description
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
线段树,区间开方求和。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define lc root<<1
#define rc root<<1|1
#define mid ((l+r)>>1)
typedef unsigned long long ull;
const int Max_N = ;
struct Node { ull val; bool flag; };
struct SegTree {
Node seg[Max_N << ];
inline void push_up(int root) {
seg[root].val = seg[lc].val + seg[rc].val;
}
inline void built(int root, int l, int r) {
seg[root].flag = false;
if (l == r) {
scanf("%lld", &seg[root].val);
return;
}
built(lc, l, mid);
built(rc, mid + , r);
push_up(root);
}
inline void update(int root, int l, int r, int x, int y) {
if (x > r || y < l || seg[root].flag) return;
if (l == r) {
seg[root].val = (ull)sqrt((double)seg[root].val);
if ( == seg[root].val) seg[root].flag = true;
return;
}
update(lc, l, mid, x, y);
update(rc, mid + , r, x, y);
push_up(root);
seg[root].flag = seg[lc].flag && seg[rc].flag;
}
inline ull query(int root, int l, int r, int x, int y) {
if (x > r || y < l) return ;
if (x <= l && y >= r) return seg[root].val;
ull v1 = query(lc, l, mid, x, y);
ull v2 = query(rc, mid + , r, x, y);
return v1 + v2;
}
}seg;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, q, a, b, c, k = ;
while (~scanf("%d", &n)) {
seg.built(, , n);
scanf("%d", &q);
printf("Case #%d:\n", k++);
while (q--) {
scanf("%d %d %d", &a, &b, &c);
if (b > c) b ^= c ^= b ^= c;
if (!a) seg.update(, , n, b, c);
else printf("%lld\n", seg.query(, , n, b, c));
}
printf("\n");
}
return ;
}