#include //#include using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n) - 1; i >= 0; --i) #define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } #include #include #include #include #include struct Mo { int width; std::vectorleft, right, order; //std::vectorhorder; int n, q; Mo(int _n, int _q) :n(_n), q(_q) { width = n / min(n, std::sqrt(q)); order.resize(q); std::iota(order.begin(), order.end(), 0); } // add query[l,r) void query(const std::vector &l, const std::vector &r) { left = l; right = r; /* horder.resize(q); for (int i = 0; i < q; ++i) horder[i] = hilbertorder(left[i], right[i]); */ } // run void run(const std::function &upd, const std::function &set) { std::sort(order.begin(), order.end(), [&](int lh, int rh) { //return horder[lh] < horder[rh]; int lblock = left[lh] / width; int rblock = left[rh] / width; if (lblock != rblock)return lblock < rblock; if (lblock & 1) return right[lh] < right[rh]; return right[lh] > right[rh]; }); int nl = 0, nr = 0; for (auto idx : order) { while (nl > left[idx])upd(0, --nl, nr); while (nr < right[idx])upd(1, nl, ++nr); while (nl < left[idx])upd(2, nl++, nr); while (nr > right[idx])upd(3, nl, nr--); set(idx); } } // TLEするときはこっちを使うと通るかも /*const int logn = 20; const int maxn = 1 << logn; long long hilbertorder(int x, int y) { long long d = 0; for (int s = 1 << logn - 1; s; s >>= 1) { bool rx = x & s, ry = y & s; d = d << 2 | rx * 3 ^ static_cast(ry); if (ry) continue; if (rx) { x = maxn - x; y = maxn - y; } std::swap(x, y); } return d; }*/ }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, q; cin >> n >> q; vectora(n); rep(i, n)cin >> a[i]; vectorl(q), r(q); rep(i, q)cin >> l[i] >> r[i]; rep(i, q) { l[i]--, r[i]--; } Mo mo(n, q); mo.query(l, r); vectorans(q); vectorcnt(26); long long val = 0; rep(i, 26)cnt[i] = 1 & (a[0] >> i); auto upd = [&](int type, int l, int r) { if (type == 0) {// l-- int sz = r - l; rep(i, 26) { long long pw = 1LL << i; if (1 & (a[l] >> i)) { val += pw * (sz - cnt[i]); cnt[i]++; } else val += pw * cnt[i]; } } else if (type == 1) {// r++ int sz = r - l; rep(i, 26) { long long pw = 1LL << i; if (1 & (a[r] >> i)) { val += pw * (sz - cnt[i]); cnt[i]++; } else val += pw * cnt[i]; } } else if (type == 2) {// l++ int sz = r - l; rep(i, 26) { long long pw = 1LL << i; if (1 & (a[l] >> i)) { cnt[i]--; val -= pw * (sz - cnt[i]); } else val -= pw * cnt[i]; } } else if (type == 3) {// r-- int sz = r - l; rep(i, 26) { long long pw = 1LL << i; if (1 & (a[r] >> i)) { cnt[i]--; val -= pw * (sz - cnt[i]); } else val -= pw * cnt[i]; } } }; auto set = [&](int idx) { ans[idx] = val; }; mo.run(upd, set); rep(i, q)cout << ans[i] << endl; return 0; }