1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include<bits/stdc++.h> #define ll long long #define MAXNUM 1000000*4+10 using namespace std; struct node{ ll l,r,lef,rig; ll val,Max; }tree[210000]; ll a[110000],len,n,m; void build(ll l,ll r){ ll root=++len; tree[root].l=l;tree[root].r=r;tree[root].lef=tree[root].rig=-1; if(l==r)tree[root].val=tree[root].Max=a[l]; else{ ll mid=(l+r)/2; ll lef=tree[root].lef=len+1;build(l,mid); ll rig=tree[root].rig=len+1;build(mid+1,r); tree[root].val=tree[lef].val+tree[rig].val; tree[root].Max=max(tree[lef].Max,tree[rig].Max); } } void update(ll root,ll l,ll r){ if(tree[root].Max<2) return ; if(tree[root].l==tree[root].r){tree[root].val=sqrt(tree[root].val);tree[root].Max=tree[root].val;return ;} ll lef=tree[root].lef,rig=tree[root].rig,mid=(tree[root].l+tree[root].r)/2; if(r<=mid) update(lef,l,r); else if(mid<l) update(rig,l,r); else update(lef,l,mid),update(rig,mid+1,r); tree[root].val=tree[lef].val+tree[rig].val; tree[root].Max=max(tree[lef].Max,tree[rig].Max); } ll query(ll root,ll l,ll r){ if(tree[root].l>=l&&tree[root].r<=r) return tree[root].val; ll lef=tree[root].lef,rig=tree[root].rig,mid=(tree[root].l+tree[root].r)/2; if(r<=mid) return query(lef,l,r); else if(mid<l) return query(rig,l,r); else return query(lef,l,mid)+query(rig,mid+1,r); } int main(){ scanf("%lld",&n); for(ll i=1;i<=n;i++) scanf("%lld",&a[i]); build(1,n); scanf("%lld",&m); while(m--){ char s;cin>>s; if(s=='2'){ ll x,y;scanf("%lld%lld",&x,&y); update(1,x,y); }else{ ll x,y;scanf("%lld%lld",&x,&y); printf("%lld\n",query(1,x,y)); } } return 0; }
|