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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
ll n,m; ll a[N];
struct segtree { struct node { ll sum,mx; } tr[N*4];
void pushup(ll p) { tr[p].sum=tr[p<<1].sum+tr[p<<1|1].sum; tr[p].mx=max(tr[p<<1].mx,tr[p<<1|1].mx); }
void build(ll p,ll l,ll r) { if(l==r) { tr[p]={a[l],a[l]}; return ; }
ll mid=l+r>>1; build(p<<1,l,mid); build(p<<1|1,mid+1,r); pushup(p); }
void update(ll p,ll l,ll r,ll x,ll y) { if(l==r) { tr[p]={y,y}; return ; }
ll mid=l+r>>1; if(x<=mid) { update(p<<1,l,mid,x,y); } else { update(p<<1|1,mid+1,r,x,y); }
pushup(p); }
void update_mod(ll p,ll l,ll r,ll x,ll y,ll v) { if(l==r) { tr[p].sum%=v; tr[p].mx%=v; return ; }
ll mid=l+r>>1; if(x<=mid&&tr[p<<1].mx>=v) { update_mod(p<<1,l,mid,x,y,v); } if(y>mid&&tr[p<<1|1].mx>=v) { update_mod(p<<1|1,mid+1,r,x,y,v); }
pushup(p); }
ll query(ll p,ll l,ll r,ll x,ll y) { if(x<=l&&y>=r) { return tr[p].sum; }
ll mid=l+r>>1; ll ret=0; if(x<=mid) { ret+=query(p<<1,l,mid,x,y); } if(y>mid) { ret+=query(p<<1|1,mid+1,r,x,y); }
return ret; } } T;
int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0);
cin>>n>>m; for(ll i=1;i<=n;i++) { cin>>a[i]; }
T.build(1,1,n);
while(m--) { ll typ,l,r; cin>>typ>>l>>r; if(typ==1) { cout<<T.query(1,1,n,l,r)<<"\n"; } else if(typ==2) { ll x; cin>>x; T.update_mod(1,1,n,l,r,x); } else { T.update(1,1,n,l,r); } }
return 0; }
|