#include <bits/stdc++.h>
template <class T>
inline void read(T &x) {
  x = 0;
  T flag = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') flag *= -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - 48;
    c = getchar();
  }
  x *= flag;
}
struct Node {
  long long max, sum;
} tr[100007 << 2];
int n, m, a[100007], p;
inline long long max(long long x, long long y) { return x > y ? x : y; }
inline void update(int id) {
  tr[id].sum = tr[id << 1].sum + tr[id << 1 | 1].sum;
  tr[id].max = max(tr[id << 1].max, tr[id << 1 | 1].max);
}
void build(int id, int lf, int rf) {
  if (lf == rf)
    tr[id].max = tr[id].sum = a[lf];
  else {
    int mid = (lf + rf) >> 1;
    build(id << 1, lf, mid);
    build(id << 1 | 1, mid + 1, rf);
    update(id);
  }
}
int l, r, val;
void modify(int id, int lf, int rf) {
  if (lf == rf)
    tr[id].max = tr[id].sum = val;
  else {
    int mid = (lf + rf) >> 1;
    if (l <= mid)
      modify(id << 1, lf, mid);
    else
      modify(id << 1 | 1, mid + 1, rf);
    update(id);
  }
}
void Mod(int id, int lf, int rf) {
  if (tr[id].max < p) return;
  if (lf == rf) {
    tr[id].max %= p;
    tr[id].sum %= p;
  } else {
    int mid = (lf + rf) >> 1;
    if (l <= mid) Mod(id << 1, lf, mid);
    if (r > mid) Mod(id << 1 | 1, mid + 1, rf);
    update(id);
  }
}
long long query(int id, int lf, int rf) {
  if (l <= lf && rf <= r)
    return tr[id].sum;
  else {
    int mid = (lf + rf) >> 1;
    long long ret = 0;
    if (l <= mid) ret += query(id << 1, lf, mid);
    if (r > mid) ret += query(id << 1 | 1, mid + 1, rf);
    return ret;
  }
}
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; i++) read(a[i]);
  build(1, 1, n);
  int opt;
  while (m--) {
    read(opt);
    if (opt == 1) {
      read(l), read(r);
      printf("%lld\n", query(1, 1, n));
    } else if (opt == 2) {
      read(l), read(r), read(p);
      Mod(1, 1, n);
    } else {
      read(l), read(val);
      modify(1, 1, n);
    }
  }
}
