题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027
题目大意:n个数,两种操作,0.将[l,r]内的所有数开平方(四舍五入取ll),1.求[l,r]内的sum
思路:线段树板子,首先需要更新到最下面,1开平方还是1,所以一个数最多更新64次,然后记录一下是否需要开平发即可。
ACCode:
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
// srand(unsigned)time(NULL));rand();
#include<map>
#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
#define ll long long
#define Pii pair<int,int>
#define clean(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXN=1e5+10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll MOD=1e9+7;
const double PI=acos(-1.0);
const double EPS=1.0e-8;
//unsigned register
// ios::sync_with_stdio(false)
struct SegTree{
struct Node{
int l,r,len;
ll Sum;
int lazy;
};
Node Tree[MAXN<<2];
void PushUp(int rt){
Tree[rt].Sum=Tree[rt<<1].Sum+Tree[rt<<1|1].Sum;
}
void Build(int l,int r,int rt,ll A[]){
Tree[rt].l=l;Tree[rt].r=r;Tree[rt].len=r-l+1;
Tree[rt].lazy=0;
if(l==r){
Tree[rt].Sum=A[l];
return ;
}int mid=(l+r)>>1;
Build(l,mid,rt<<1,A);Build(mid+1,r,rt<<1|1,A);
PushUp(rt);
}
void Update(int ql,int qr,int rt){
if(Tree[rt].l==Tree[rt].r){
Tree[rt].Sum=sqrt(Tree[rt].Sum);
return ;
}
if(ql<=Tree[rt].l&&Tree[rt].r<=qr&&Tree[rt].Sum==Tree[rt].len) return ;
if(qr<=Tree[rt<<1].r) Update(ql,qr,rt<<1);
else if(ql>=Tree[rt<<1|1].l) Update(ql,qr,rt<<1|1);
else{
Update(ql,qr,rt<<1);
Update(ql,qr,rt<<1|1);
}PushUp(rt);
}
ll Query(int ql,int qr,int rt){
if(ql<=Tree[rt].l&&Tree[rt].r<=qr) return Tree[rt].Sum;
if(qr<=Tree[rt<<1].r) return Query(ql,qr,rt<<1);
else if(ql>=Tree[rt<<1|1].l) return Query(ql,qr,rt<<1|1);
else return Query(ql,qr,rt<<1)+Query(ql,qr,rt<<1|1);
}
};
SegTree Seg;
ll A[MAXN];
int n,m;
int main(){
int Case=1;
while(~scanf("%d",&n)){
printf("Case #%d:\n",Case++);
for(int i=1;i<=n;++i) scanf("%lld",&A[i]);
Seg.Build(1,n,1,A);
scanf("%d",&m);
while(m--){
int opt,l,r;scanf("%d%d%d",&opt,&l,&r);
if(r<l) swap(l,r);
if(opt==0){
Seg.Update(l,r,1);
}
else{
printf("%lld\n",Seg.Query(l,r,1));
}
}printf("\n");
}
}