1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| #include <iostream> #include <vector> #include <map> #include <set> #include <bitset> #include <cmath> #include <algorithm>
using namespace std;
#define lson (u<<1) #define rson (lson|1) #define lmid ((l+r)>>1) #define rmid (lmid+1)
int qu = 0; int up = 0;
const int maxn = 2e5 + 100; int a[maxn]; int mn1[maxn << 2], mn2[maxn << 2]; int lzy[maxn << 2], bit[maxn << 2][32]; int cnt[maxn << 2], xrr[maxn << 2];
void pushup(int u, int l, int r) {
map<int, int> ma; ma[mn1[lson]] += cnt[lson]; ma[mn1[rson]] += cnt[rson];
if (lmid - l + 1 != cnt[lson]) { ma[mn2[lson]]++; } if (r - rmid + 1 != cnt[rson]) { ma[mn2[rson]]++; }
if (ma.size() > 1) { mn2[u] = (++ma.begin())->first; } cnt[u] = ma.begin()->second;
for (int i = 0; i < 31; ++i) { bit[u][i] = bit[lson][i] + bit[rson][i]; }
mn1[u] = min(mn1[lson], mn1[rson]); xrr[u] = xrr[lson] ^ xrr[rson]; }
void build(int u, int l, int r) { if (l == r) { xrr[u] = mn1[u] = mn2[u] = a[l]; lzy[u] = 0; cnt[u] = 1; for (int i = 0; i < 31; ++i) { bit[u][i] = ((1 << i) & a[l]) >> i; } return; } build(lson, l, lmid); build(rson, rmid, r); lzy[u] = 0; pushup(u, l, r); }
void pushSon(int son, int l, int r, int value) { if (mn1[son] < value) { for (int i = 0; i < 31; ++i) { bit[son][i] -= (((1 << i) & mn1[son]) >> i) * cnt[son]; bit[son][i] += (((1 << i) & value) >> i) * cnt[son]; } lzy[son] = max(lzy[son], value); if (cnt[son] & 1) { xrr[son] ^= mn1[son]; xrr[son] ^= value; } mn1[son] = value; } }
void pushdown(int u, int l, int r) { pushSon(lson, l, lmid, lzy[u]); pushSon(rson, rmid, r, lzy[u]); }
void modify(int u, int l, int r, int ql, int qr, int mx) { up++; if (ql <= l && r <= qr) { if (r - l + 1 == cnt[u] || mx < mn2[u]) { pushSon(u, l, r, mx); return; } } pushdown(u, l, r); if (ql <= lmid) { modify(lson, l, lmid, ql, qr, mx); } if (qr > lmid) { modify(rson, rmid, r, ql, qr, mx); } pushup(u, l, r); }
int query(int u, int l, int r, int ql, int qr, int bt) { qu++; if (ql <= l && r <= qr) { return bit[u][bt]; } int res = 0; pushdown(u, l, r); if (ql <= lmid) { res += query(lson, l, lmid, ql, qr, bt); } if (qr > lmid) { res += query(rson, rmid, r, ql, qr, bt); } return res; }
int query_xr(int u, int l, int r, int ql, int qr) { qu++; if (ql <= l && r <= qr) { return xrr[u]; } int res = 0; pushdown(u, l, r); if (ql <= lmid) { res ^= (query_xr(lson, l, lmid, ql, qr)); } if (qr > lmid) { res ^= (query_xr(rson, rmid, r, ql, qr)); } return res; }
int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; for (int i = 1; i <= n; ++i) { cin >> a[i]; } build(1, 1, n); for (int i = 1; i <= q; ++i) { int op, l, r, x; cin >> op >> l >> r >> x; if (op == 1) { modify(1, 1, n, l, r, x);
} else { int value = query_xr(1, 1, n, l, r) ^x; if (value == 0) { cout << "0\n"; continue; } int bt = 30; while (1) { if ((1 << bt) & value) { break; } --bt; } int add = 0; if ((value ^ x) < x) { add = 1; } cout << query(1, 1, n, l, r, bt) + add << "\n"; } } }
|