#ifndef ATCODER_INTERNAL_BITOP_HPP #define ATCODER_INTERNAL_BITOP_HPP 1 #ifdef _MSC_VER #include #endif #if __cplusplus >= 202002L #include #endif namespace atcoder { namespace internal { #if __cplusplus >= 202002L using std::bit_ceil; #else // @return same with std::bit::bit_ceil unsigned int bit_ceil(unsigned int n) { unsigned int x = 1; while (x < (unsigned int)(n)) x *= 2; return x; } #endif // @param n `1 <= n` // @return same with std::bit::countr_zero int countr_zero(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } // @param n `1 <= n` // @return same with std::bit::countr_zero constexpr int countr_zero_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } } // namespace internal } // namespace atcoder #endif // ATCODER_INTERNAL_BITOP_HPP #ifndef ATCODER_LAZYSEGTREE_HPP #define ATCODER_LAZYSEGTREE_HPP 1 #include #include #include #include #include "atcoder/internal_bit" namespace atcoder { #if __cplusplus >= 201703L template struct lazy_segtree { static_assert(std::is_convertible_v>, "op must work as S(S, S)"); static_assert(std::is_convertible_v>, "e must work as S()"); static_assert( std::is_convertible_v>, "mapping must work as S(F, S)"); static_assert( std::is_convertible_v>, "composition must work as F(F, F)"); static_assert(std::is_convertible_v>, "id must work as F()"); #else template struct lazy_segtree { #endif public: lazy_segtree() : lazy_segtree(0) {} explicit lazy_segtree(int n) : lazy_segtree(std::vector(n, e())) {} explicit lazy_segtree(const std::vector& v) : _n(int(v.size())) { size = (int)internal::bit_ceil((unsigned int)(_n)); log = internal::countr_zero((unsigned int)size); d = std::vector(2 * size, e()); lz = std::vector(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } history_d.clear(); } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); assign_d(p, x); for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); assign_d(p, mapping(f, d[p])); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } void reset(){ for(int i=(int)history_d.size()-1;i>=0;i--){ d[history_d[i].first]=history_d[i].second; } for(int i=(int)history_lz.size()-1;i>=0;i--){ lz[history_lz[i]]=id(); } history_d.clear(); history_lz.clear(); } private: int _n, size, log; std::vector d; std::vector lz; std::vector> history_d; std::vector history_lz; void update(int k) { assign_d(k, op(d[2 * k], d[2 * k + 1])); } void all_apply(int k, F f) { assign_d(k, mapping(f, d[k])); if (k < size) assign_lz(k, composition(f, lz[k])); } void push(int k) { if(lz[k]==id()){ return; } all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); assign_lz(k, id()); } void assign_d(int k, const S& val){ history_d.push_back({k,d[k]}); d[k] = val; } void assign_lz(int k, const F& val){ if(val!=id()){ history_lz.push_back(k); } lz[k]=val; } }; } // namespace atcoder #endif // ATCODER_LAZYSEGTREE_HPP #ifndef CPPLIB_SRC_STRUCTURE_IO_FASTIO_HPP_INCLUDED #define CPPLIB_SRC_STRUCTURE_IO_FASTIO_HPP_INCLUDED #include #include #define read_size 1000000 #define write_size 1000000 struct fastio { private: char read_data[read_size]; int read_pos = 0; int read_len = 0; char write_data[write_size]; int write_pos = 0; int getch() { if (read_pos == read_len) { read_len = fread(read_data, 1, read_size, stdin); read_pos = 0; if (read_len == 0) return EOF; } return read_data[read_pos++]; } void ungetch() { if (read_pos > 0) read_pos--; } void readspeoln() { int c; while (true) { c = getch(); if (c == EOF) return; if (c != ' ' && c != '\n' && c != '\r' && c != '\t') { ungetch(); return; } } } void flush() { if (write_pos != 0) { fwrite(write_data, 1, write_pos, stdout); write_pos = 0; } } public: fastio() {} ~fastio() { flush(); } void readint(int &x) { readspeoln(); int c = getch(); bool negative = false; unsigned int value = 0; if (c == '-') { negative = true; c = getch(); } while ('0' <= c && c <= '9') { value = value * 10U + static_cast(c & 15); c = getch(); } if (c != EOF) ungetch(); if (negative && value == static_cast( std::numeric_limits::max()) + 1U) { x = std::numeric_limits::min(); } else { x = negative ? -static_cast(value) : static_cast(value); } } void readll(long long &x) { readspeoln(); int c = getch(); bool negative = false; unsigned long long value = 0; if (c == '-') { negative = true; c = getch(); } while ('0' <= c && c <= '9') { value = value * 10ULL + static_cast(c & 15); c = getch(); } if (c != EOF) ungetch(); if (negative && value == static_cast( std::numeric_limits::max()) + 1ULL) { x = std::numeric_limits::min(); } else { x = negative ? -static_cast(value) : static_cast(value); } } // [a, z] void readstr(char *s) { readspeoln(); int c = getch(); while (c != EOF && c != ' ' && c != '\n' && c != '\r' && c != '\t') { *s++ = (char)c; c = getch(); } if (c != EOF) ungetch(); *s = '\0'; } void write(char c) { if (write_pos == write_size) flush(); write_data[write_pos++] = c; } void write(const char *s) { while (*s) write(*s++); } void writeint(int x) { if (x == 0) { write('0'); return; } unsigned int value; if (x < 0) { write('-'); value = 0U - static_cast(x); } else { value = static_cast(x); } char s[20]; int n = 0; while (value > 0) { s[n++] = char('0' + value % 10U); value /= 10U; } while (n--) write(s[n]); } void writell(long long x) { if (x == 0) { write('0'); return; } unsigned long long value; if (x < 0) { write('-'); value = 0ULL - static_cast(x); } else { value = static_cast(x); } char s[30]; int n = 0; while (value > 0) { s[n++] = char('0' + value % 10ULL); value /= 10ULL; } while (n--) write(s[n]); } }; #endif // CPPLIB_SRC_STRUCTURE_IO_FASTIO_HPP_INCLUDED #include using namespace std; const int mask=(1<<30)-1; // 0の数, 1の数 using S=array, 30>; S op(S a, S b){ for(int i=0;i<30;i++){ a[i].first+=b[i].first; a[i].second+=b[i].second; } return a; } S e(){ return S{}; } using F = array; S mapping(F f, S x){ for(int i=0;i<30;i++){ if(f[i] == 1){ // AND 0 x[i].first+=x[i].second; x[i].second=0; }else if(f[i] == 2){ // OR 1 x[i].second+=x[i].first; x[i].first=0; } } return x; } F composition(F f, F g){ for(int i=0;i<30;i++){ if(f[i]==1){ g[i]=1; }else if(f[i]==2){ g[i]=2; } } return g; } F id(){ return F{}; } int main(){ fastio io; int N, M; io.readint(N); io.readint(M); vector A(N); vector AA(N); for(int i=0;i>j)&1){ AA[i][j].second=1; }else{ AA[i][j].first=1; } } } vector l(M),r(M),x(M),L(M),R(M); for(int i=0;i seg(AA); int Q; io.readint(Q); for(int i=0;i>k)&1){ f[k]=2; } } seg.apply(ld, rd, f); }else{ F f{}; for(int k=0;k<30;k++){ if(((X>>k)&1)==0){ f[k]=1; } } seg.apply(ld, rd, f); } S res=seg.prod(Ld, Rd); y=0; for(int k=0;k<30;k++){ y+=((1LL<