ghorardim

toph-Playing With BITs

Mar 30th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<ctype.h>
  5. #include<stdlib.h>
  6. #include<cstdio>
  7. #include<cstring>
  8. #include<string>
  9. #include<cmath>
  10. #include<iostream>
  11. #include<cctype>
  12. #include<map>
  13. #include<stack>
  14. #include<cstdlib>
  15. #include<queue>
  16. #include<vector>
  17. #include<algorithm>
  18. #include<bitset>
  19. #include<algorithm>
  20. #include<set>
  21. #include<limits.h>
  22. #include <sstream>
  23.  
  24. //#include <bits/stdc++.h>
  25.  
  26.  
  27. using namespace std;
  28. typedef long int LD;
  29. typedef long long int LLD;
  30. typedef float F;
  31. typedef double LF;
  32. typedef unsigned int U;
  33. typedef unsigned long int LU;
  34. typedef unsigned long long int LLU;
  35. typedef char C;
  36.  
  37.  
  38. /*----------simple input section ------------ */
  39. #define sf scanf
  40. #define sfi(x) scanf("%d",&x)
  41. #define sfc(x) scanf("%c",&x)
  42. #define sfi2(x,y) scanf("%d%d",&x,&y)
  43. #define sfll2(x,y) scanf("%lld%lld",&x,&y)
  44. #define sfu2(x,y) scanf("%llu%llu",&x,&y)
  45. #define sfl(x) scanf("%ld",&x)
  46. #define sfll(x) scanf("%lld",&x)
  47. #define sfd(x) scanf("%lf",&x)
  48. #define sfu(x) scanf("%llu",&x)
  49. #define sfs(x) scanf("%s",x)
  50.  
  51.  
  52. /*----------simple input section ------------ */
  53. #define pf printf
  54. #define pfi(x) printf("%d\n",x)
  55. #define pfl(x) printf("%ld\n",x)
  56. #define pfll(x) printf("%lld\n",x)
  57. #define pfu(x) printf("%llu\n",x)
  58. #define pfs(x) printf("%s\n",x)
  59. #define pfc(x) printf("%c\n",x)
  60. #define pfd(x) printf("%lf\n",x)
  61.  
  62. /*----------file input section ------------ */
  63.  
  64. #define f_input  freopen("input.txt","r",stdin);
  65. #define f_output freopen("output.txt","w",stdout);
  66.  
  67. #define pb(x) push_back(x)
  68. #define PI acos(-1.0)
  69. #define sq(x) (x)*(x)
  70. #define mem(x,y) memset(x,y,sizeof(x))
  71. #define TEST int cs,T;sfi(T);getchar();for(cs=1;cs<=T;cs++)
  72. #define nn printf("\n")
  73. // xx-> diagonal -> 8 horizontal/vertical->4
  74.  
  75. const int xx[] = {0, 1, 0, -1, -1, 1, -1, 1};
  76. const int yy[] = {1, 0, -1, 0, 1, 1, -1, -1};
  77.  
  78. // KX-> Knight moves
  79. const int kx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
  80. const int ky[] = {1, 2, 2, 1, -1, -2, -2, -1};
  81.  
  82.  
  83. /******** debug **********/
  84. #define chk1 printf("hi......1  \n")
  85. #define chk2 printf("hi......2  \n")
  86.  
  87.  
  88. template<class T> T power(T N,T P)
  89. {
  90.     return (P==0)?  1: N*power(N,P-1);   //N^P
  91. }
  92.  
  93. #define mod  1000000007
  94. #define eps 10e-8
  95. #define sz 100005
  96. #define sz1 65
  97. #define sz2 1000005
  98. /******* start my code ********/
  99. LLU tree[4*sz][sz1];
  100. LLU arr[sz];
  101. void build(int pos,int l,int r)
  102. {
  103.     if(l==r)
  104.     {
  105.         LLU x,i;
  106.         x=arr[l];
  107.         i=0;
  108.         while(x)
  109.         {
  110.             tree[pos][i++]=(x&1);
  111.             x=x>>1;
  112.         }
  113.         return;
  114.     }
  115.     int mid=(l+r)/2;
  116.     build(pos*2,l,mid);
  117.     build(pos*2+1,mid+1,r);
  118.     for(int i=0; i<64; i++)
  119.         tree[pos][i]=tree[pos*2][i]+tree[pos*2+1][i];
  120. }
  121. void update(int pos,int l,int r,int idx,LLU val)
  122. {
  123.     if(l==r)
  124.     {
  125.         LLU x,i;
  126.         x=val;
  127.         i=0;
  128.         while(x)
  129.         {
  130.             tree[pos][i++]=(x&1);
  131.             x=x>>1;
  132.         }
  133.         return;
  134.         // tree[pos]=val;
  135.         // return;
  136.     }
  137.     int mid=(l+r)/2;
  138.     if(idx<=mid) update(pos*2,l,mid,idx,val);
  139.     else update(pos*2+1,mid+1,r,idx,val);
  140.     for(int i=0; i<64; i++)
  141.         tree[pos][i]=tree[pos*2][i]+tree[pos*2+1][i];
  142. }
  143. LLU query(int pos,int l,int r,int s,int e,int k)
  144. {
  145.     if(l>e || r<s) return 0;
  146.     if(l>=s && r<=e) return tree[pos][k];
  147.     int mid=(l+r)/2;
  148.     LLU q1=query(pos*2,l,mid,s,e,k);
  149.     LLU q2=query(pos*2+1,mid+1,r,s,e,k);
  150.     return q1+q2;
  151. }
  152. int main()
  153. {
  154.     int i,j,k,n,m,l,a,b,q,t;
  155.     LLU x;
  156.     while(sfi(n)!=EOF)
  157.     {
  158.         mem(tree,0);
  159.         for(i=1; i<=n; i++)
  160.         {
  161.             sfu(arr[i]);
  162.         }
  163.         build(1,1,n);
  164.         sfi(q);
  165.  
  166.         while(q--)
  167.         {
  168.             sfi(t);
  169.             if(t==1)
  170.             {
  171.                 sfi(a);
  172.                 sfu(x);
  173.                 update(1,1,n,a,x);
  174.             }
  175.             else
  176.             {
  177.                 sfi2(a,b);
  178.                 sfi(k);
  179.                 pfu(query(1,1,n,a,b,k));
  180.             }
  181.         }
  182.  
  183.     }
  184.  
  185.     return 0;
  186. }
  187.  
  188. /*
  189. 5
  190. 1152921504606846976 1224979098644774912 1227230898458460160 1515461274610171904 1515602012098527232
  191.  
  192. */
Advertisement
Add Comment
Please, Sign In to add comment