#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000 + 7;
const double esp = 1e-13;
const double pi = 3.141592653589;
const int MAX = 500000;
long long ans, a[MAX + 10], f[MAX + 10], n, m, d, l, r, x, ql, qx;
void build(int k, int l, int r) {
  if (l == r) {
    cin >> a[k];
    f[k] = a[k];
    return;
  }
  int m = (l + r) / 2;
  build(k * 2, l, m);
  build(k * 2 + 1, m + 1, r);
  f[k] = f[k * 2] + f[k * 2 + 1];
  a[k] = max(a[k * 2], a[k * 2 + 1]);
}
void tinh(int k, int l, int r, int i, int j) {
  if (i > r || l > j) return;
  if (i <= l && r <= j) {
    ans += f[k];
    return;
  }
  int m = (l + r) / 2;
  tinh(k * 2, l, m, i, j);
  tinh(k * 2 + 1, m + 1, r, i, j);
}
void update(int k, int l, int r, int i, int j) {
  if (l == r) {
    a[k] %= x;
    f[k] = a[k];
    return;
  }
  if (a[k] < x) return;
  int m = (l + r) / 2;
  if (i <= m) update(k * 2, l, m, i, j);
  if (m < j) update(k * 2 + 1, m + 1, r, i, j);
  f[k] = f[k * 2] + f[k * 2 + 1];
  a[k] = max(a[k * 2], a[k * 2 + 1]);
}
void change(int k, int l, int r) {
  if (l == r) {
    f[k] = a[k] = qx;
    return;
  }
  int m = (l + r) / 2;
  if (ql <= m)
    change(k * 2, l, m);
  else
    change(k * 2 + 1, m + 1, r);
  f[k] = f[k * 2] + f[k * 2 + 1];
  a[k] = max(a[k * 2], a[k * 2 + 1]);
}
int main() {
  ios ::sync_with_stdio(false);
  cin >> n >> m;
  build(1, 1, n);
  while (m--) {
    cin >> d;
    if (d == 1) {
      cin >> l >> r;
      ans = 0;
      tinh(1, 1, n, l, r);
      cout << ans << "\n";
    } else if (d == 2) {
      cin >> l >> r >> x;
      update(1, 1, n, l, r);
    } else {
      cin >> ql >> qx;
      change(1, 1, n);
    }
  }
}
