HDU - 4027 Can you answer these queries?
题 意:给你n个数字,q个操作。每次操作输入x,l,r。x= 0 ,代表把al到ar的值全部都开根号。x=1,代表求al到ar的和。
数据范围:
1<=M,N<=1e5
输入样例:
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
输出样例:
Case #1:
19
7
6
思 路:因为1个1e9的数字连续开6,7次根号就变成1了,而如果一段区间都为1的话,我们就可以直接跳过。所以区间更新的复杂度并不大。
收 获:学习了这种看起来update复杂度很大,其实通过分析,更行操作的复杂度并不大
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+4;
ll sum[maxn<<2];
ll a[maxn];
int n,m;
void PushDown(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l,int r,int rt){
if(l == r){
sum[rt] = a[l];
return;
}
int m = (l+r)/2;
build(lson);
build(rson);
PushDown(rt);
}
void update(int L,int R,int l,int r,int rt){
if(sum[rt] == (r-l+1))return;
if(l == r){
sum[rt] = (ll)sqrt(1.0*sum[rt]);
return;
}
int m = (l+r)>>1;
if(L<=m) update(L,R,lson);
if(R>m) update(L,R,rson);
PushDown(rt);
}
ll query(int L,int R,int l,int r,int rt){
if(L<=l && r<=R){
return sum[rt];
}
int m = (l+r)>>1;
ll ans = 0;
if(L<=m) ans+=query(L,R,lson);
if(R>m) ans+=query(L,R,rson);
return ans;
}
int main(){
int Kase = 1;
while(~scanf("%d",&n)){
printf("Case #%d:\n",Kase++);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1,n,1);
int flag,l,r;
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d %d %d",&flag,&l,&r);
if(l>r) swap(l,r);
if(flag == 0){
update(l,r,1,n,1);
}else{
ll ans = query(l,r,1,n,1);
printf("%lld\n",ans);
}
}
printf("\n");
}
return 0;
}
/*
5
1 1
2 1
3 2
1 1
*/