http://www.spoj.com/problems/GSS4/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 #define lson l,m,rt<<1 6 #define rson m+1,r,rt<<1|1 7 #define maxn 100010 8 typedef long long LL; 9 struct node 10 { 11 int c; 12 LL sum; 13 }setree[maxn<<2]; 14 void pushup(int rt) 15 { 16 setree[rt].sum=setree[rt<<1].sum+setree[rt<<1|1].sum; 17 setree[rt].c=setree[rt<<1].c&&setree[rt<<1|1].c; 18 } 19 void build(int l,int r,int rt) 20 { 21 if(l==r){ 22 scanf("%lld",&setree[rt].sum); 23 if(setree[rt].sum==1) 24 setree[rt].c=1; 25 else 26 setree[rt].c=0; 27 return; 28 } 29 int m=(l+r)>>1; 30 build(lson); 31 build(rson); 32 pushup(rt); 33 } 34 void update(int l,int r,int rt,int L,int R) 35 { 36 if(L<=l&&r<=R&&setree[rt].c) 37 return; 38 if(l==r){ 39 setree[rt].sum=(LL)sqrt(setree[rt].sum); 40 if(setree[rt].sum==1) 41 setree[rt].c=1; 42 else 43 setree[rt].c=0; 44 return; 45 } 46 int m=(l+r)>>1; 47 if(L<=m&&setree[rt<<1].c==0) 48 update(lson,L,R); 49 if(R>m&&setree[rt<<1|1].c==0) 50 update(rson,L,R); 51 pushup(rt); 52 } 53 LL query(int l,int r,int rt,int L,int R) 54 { 55 if(L<=l&&r<=R) 56 return setree[rt].sum; 57 int m=(l+r)>>1; 58 LL ans=0; 59 if(L<=m) 60 ans+=query(lson,L,R); 61 if(R>m) 62 ans+=query(rson,L,R); 63 return ans; 64 } 65 int main() 66 { 67 int n,cas=1; 68 while(~scanf("%d",&n)){ 69 build(1,n,1); 70 int m; 71 scanf("%d",&m); 72 printf("Case #%d:\n",cas++); 73 while(m--){ 74 int a,b,c; 75 scanf("%d%d%d",&a,&b,&c); 76 if(b>c)swap(b,c); 77 if(a==0) 78 update(1,n,1,b,c); 79 else 80 printf("%lld\n",query(1,n,1,b,c)); 81 } 82 printf("\n"); 83 } 84 return 0; 85 }