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
| #include <bits/stdc++.h>
using namespace std; #define LL long long #define ll long long #define ULL unsigned long long #define Pair pair<LL,LL> #define ls rt<<1 #define rs rt<<1|1 #define Pi acos(-1.0) #define eps 1e-6 #define DBINF 1e100 #define mod 1000000007 #define MAXN 100000 #define MXLEN 17 #define MS 100009
int n,m; struct node{ int cnt[22]; int la[22]; bool isla; }p[MS<<2];
void push_up(int rt){ for(int i=20;i>=0;i--){ p[rt].cnt[i] = p[ls].cnt[i] + p[rs].cnt[i]; } }
void build(int l,int r,int rt){ if(l == r){ int x; cin >> x; for(int i=20;i>=0;i--){ p[rt].cnt[i] = ( (x>>i)&1 ); } return; } int m = l+r>>1; build(l,m,ls); build(m+1,r,rs); push_up(rt); }
void push_down(int rt,int l,int r){ if(p[rt].isla){ int m = l+r>>1; int ln = m-l+1; int rn = r-m; p[ls].isla = p[rs].isla = false; for(int i=20;i>=0;i--){ int t = p[rt].la[i]; p[rt].la[i] = 0; if(t){ p[ls].cnt[i] = ln - p[ls].cnt[i]; p[rs].cnt[i] = rn - p[rs].cnt[i]; } p[ls].la[i] ^= t; p[rs].la[i] ^= t; if(p[ls].la[i]) p[ls].isla = true; if(p[rs].la[i]) p[rs].isla = true; } p[rt].isla = false; } }
void modify(int L,int R,int l,int r,int rt,int x){ if(L <= l && r <= R){ int sum = r-l+1; p[rt].isla = false; for(int i=20;i>=0;i--){ int t = (x>>i)&1; if(t) p[rt].cnt[i] = sum-p[rt].cnt[i]; p[rt].la[i] ^= t; if(p[rt].la[i]) p[rt].isla = true; } return; } int m = l+r>>1; push_down(rt,l,r); if(m >= L) modify(L,R,l,m,ls,x); if(m < R) modify(L,R,m+1,r,rs,x); push_up(rt); }
LL query(int L,int R,int l,int r,int rt){ if(L <= l && r <= R){ LL sum = 0; for(int i=20;i>=0;i--){ LL t = p[rt].cnt[i]; sum += (1ll<<i)*t; } return sum; } int m = l+r>>1; push_down(rt,l,r); LL ans = 0; if(m >= L) ans += query(L,R,l,m,ls); if(m < R) ans += query(L,R,m+1,r,rs); return ans; }
int main() { ios::sync_with_stdio(false); cin >> n; build(1,n,1); cin >> m; for(int i=1;i<=m;i++){ int op,l,r,x; cin >> op >> l >> r; if(op == 1){ cout << query(l,r,1,n,1) << "\n"; } else if(op == 2){ cin >> x; modify(l,r,1,n,1,x); } } return 0; }
|