题意:
给定100000个数,两种操作,0 i j表示将i j这段的数字都开根号(向下取整),1 i j表示查询i j之间的所有值的和。(所有的和都不超过64位)
解析:
此题的关键是要理解对任何64位以内的值,开根号最多不会超过7次,所以用线段树做,更新到叶子节点的次数最多7次,所以,每当我们要进行更新操作的时候,我们先判断一下这个区间是否有必要进行更新(若全都是1就没有更新的必要了),判断方法是开一个cover数组来表示当前区域时是否全为1。
注意:
此题输入时,可能发生L>R,所以遇到这种情况要两个数字互换。
AC代码:
#include <cstdio>
#include <algorithm>
#include <cmath>
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)
using namespace std;
typedef unsigned __int64 ll;
const int N = 100005;
ll sumv[N<<2];
bool cover[N<<2];
int n, q;
void maintain(int o) {
cover[o] = cover[lson(o)] && cover[rson(o)];
sumv[o] = sumv[lson(o)] + sumv[rson(o)];
}
void build(int o, int L, int R) {
cover[o] = false;
if(L == R) {
scanf("%I64u", &sumv[o]);
if(sumv[o] <= 1)
cover[o] = true;
return ;
}
int M = (L+R)/2;
build(lson(o), L, M);
build(rson(o), M+1, R);
maintain(o);
}
int ql, qr;
void modify(int o, int L, int R) {
if(cover[o]) return ;
if(L == R) {
sumv[o] = (ll)sqrt(sumv[o]*1.0);
if(sumv[o] <= 1) cover[o] = true;
return ;
}
int M = (L+R)/2;
if(ql <= M) modify(lson(o), L, M);
if(qr > M) modify(rson(o), M+1, R);
maintain(o);
}
ll query(int o, int L, int R) {
if(ql <= L && R <= qr) return sumv[o];
int M = (L+R)/2;
ll ret = 0;
if(ql <= M) ret += query(lson(o), L, M);
if(qr > M) ret += query(rson(o), M+1, R);
return ret;
}
int main() {
//freopen("in.txt", "r", stdin);
int cas = 1;
while (scanf("%d", &n) != EOF) {
build(1, 1, n);
printf("Case #%d:\n", cas++);
scanf("%d", &q);
int choice;
while(q--) {
scanf("%d%d%d", &choice, &ql, &qr);
if(ql > qr) swap(ql, qr);
if(choice == 0) {
modify(1, 1, n);
}else {
ll ans = query(1, 1, n);
printf("%I64u\n", ans);
}
}
puts("");
}
return 0;
}