Guest User

Untitled

a guest
Feb 5th, 2019
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define mod 1000000007
  4. #define mp make_pair
  5. #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  6. #define pb push_back
  7. #define endl '\n'
  8. #define pii pair<int,int>
  9. #define all(v) v.begin(),v.end()
  10. ll poww(ll x,ll y) {ll res=1;x%=mod; for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
  11. using namespace std;
  12.  
  13. int a[25][100005],seg[25][400005],n,lazy[25][400005];
  14.  
  15. void build(int i,int l,int r,int x){
  16.  
  17. if(l>r)
  18. return;
  19. if(l==r){
  20. seg[i][x]=a[i][l];
  21. return;
  22. }
  23. int mid=(l+r)/2;
  24. build(i,l,mid,x*2+1);
  25. build(i,mid+1,r,x*2+2);
  26. seg[i][x]=seg[i][x*2+1]+seg[i][x*2+2];
  27.  
  28. }
  29.  
  30. void setbit(int i){
  31.  
  32. int x=a[24][i];
  33. int c=0;
  34. while(x>0){
  35. a[c++][i]=x%2;
  36. x/=2;
  37. }
  38.  
  39. }
  40.  
  41. ll sum(int i,int ql,int qr,int l,int r,int x){
  42.  
  43. if(l>r||ql>r||qr<l) return 0;
  44.  
  45. if(lazy[i][x]){
  46.  
  47. seg[i][x]=(r-l+1)-seg[i][x];
  48.  
  49. if(l!=r){
  50. lazy[i][2*x+1]^=lazy[i][x];
  51. lazy[i][2*x+2]^=lazy[i][x];
  52. }
  53.  
  54. lazy[i][x]=0;
  55.  
  56. }
  57.  
  58. if(ql<=l&&qr>=r) return seg[i][x];
  59.  
  60. int mid=l+r>>1;
  61. return (sum(i,ql,qr,l,mid,x*2+1)+sum(i,ql,qr,mid+1,r,2*x+2));
  62.  
  63. }
  64.  
  65. void upd(int i,int ql,int qr,int l,int r,int x){
  66.  
  67. if(lazy[i][x]){
  68.  
  69. seg[i][x]=(r-l+1)-seg[i][x];
  70.  
  71. if(l!=r){
  72. lazy[i][2*x+1]^=lazy[i][x];
  73. lazy[i][2*x+2]^=lazy[i][x];
  74. }
  75.  
  76. lazy[i][x]=0;
  77.  
  78. }
  79.  
  80. if(l>r||ql>r||l>qr) return;
  81.  
  82. if(ql<=l&&r<=qr){
  83.  
  84. int temp=5123123;
  85. temp=seg[i][x];
  86. temp=seg[i][x]=(r-l+1)-seg[i][x];
  87. temp=2;
  88. if(l!=r){
  89. lazy[i][2*x+1]^=1;
  90. lazy[i][2*x+2]^=1;
  91. }
  92. return;
  93. }
  94. int mid=l+r>>1;
  95. upd(i,ql,qr,l,mid,x*2+1);
  96. upd(i,ql,qr,mid+1,r,x*2+2);
  97. seg[i][x]=seg[i][x*2+1]+seg[i][x*2+2];
  98.  
  99. }
  100.  
  101. int main()
  102. {
  103. fast;
  104. //freopen("C:/Users/PRAJAL/Desktop/input.txt", "r+", stdin);
  105. //freopen("C:/Users/PRAJAL/Desktop/input5.txt", "w+", stdout);
  106.  
  107. cin>>n;
  108.  
  109. for(int i=0;i<n;++i) cin>>a[24][i];
  110. for(int i=0;i<n;++i) setbit(i);
  111. for(int i=0;i<20;++i) build(i,0,n-1,0);
  112.  
  113. int m,l,r,x;
  114. cin>>m;
  115.  
  116. while(m--){
  117.  
  118. cin>>x>>l>>r;
  119. l--;
  120. r--;
  121. if(x==1){
  122. ll ans=0;
  123. for(ll i=0;i<20;++i)
  124. ans+=sum(i,l,r,0,n-1,0)<<i;
  125. cout<<ans<<endl;
  126. }
  127. else{
  128. cin>>x;
  129. for(int i=0;i<20;++i,x/=2) if (x%2) upd(i,l,r,0,n-1,0);
  130. }
  131.  
  132. }
  133. return 0;
  134. }
Add Comment
Please, Sign In to add comment