#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; namespace nachia{ int Popcount(unsigned long long c) noexcept { #ifdef __GNUC__ return __builtin_popcountll(c); #else c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3)); c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5)); c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17)); c = (c * (~0ull/257)) >> 56; return c; #endif } // please ensure x != 0 int MsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return 63 - __builtin_clzll(x); #else using u64 = unsigned long long; int q = (x >> 32) ? 32 : 0; auto m = x >> q; constexpr u64 hi = 0x88888888; constexpr u64 mi = 0x11111111; m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 35; m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 31; q += (m & 0xf) << 2; q += 0x3333333322221100 >> (((x >> q) & 0xf) << 2) & 0xf; return q; #endif } // please ensure x != 0 int LsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return __builtin_ctzll(x); #else return MsbIndex(x & -x); #endif } } namespace nachia { struct BitVectorRank32{ using u64 = unsigned long long; struct WordBlock{ u64 x; int r; }; std::vector a; BitVectorRank32(){} BitVectorRank32(int n){ a.assign(n/64+2, {0,0}); } int rank(int p) const { return a[p/64].r + Popcount(a[p/64].x & ~(~(u64)0 << (p%64))); } u64& operator[](int i){ return a[i].x; } u64 operator[](int i) const { return a[i].x; } void precalc(){ for(int i=1; i+1> N >> Q; vector bits(26); vector A(N); rep(i,N) cin >> A[i]; rep(i,bits.size()){ bits[i] = nachia::BitVectorRank32(N); rep(p,N) bits[i][p>>6] |= (u64)((A[p] >> i) % 2) << (p % 64); bits[i].precalc(); } rep(qi,Q){ i64 l,r; cin >> l >> r; l--; i64 ans = 0; rep(b,bits.size()){ i64 h = bits[b].rank(r) - bits[b].rank(l); ans += (h * (r-l-h)) << b; } cout << ans << "\n"; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }