Maruf_Hasan

BIT_G

Oct 12th, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. //In the Name of Allah Most Gracious, Most Merciful//
  2. /*If you want something you've never had, you have to do something you never did.*/
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8. #define pb push_back
  9. #define ll long long
  10. #define pii pair<ll,ll>
  11. #define pll pair<ll,ll>
  12. #define MaxN 201007
  13. #define INF 1e9
  14. #define INFL 1e18
  15. #define PI acos(-1)
  16. #define mp make_pair
  17.  
  18. //const ll fx[]= {+1,-1,+0,+0};
  19. //const ll fy[]= {+0,+0,+1,-1};
  20. //const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  21. //const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  22. //const ll fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  23. //const ll fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  24.  
  25.  
  26. ll gcd(ll a, ll b)
  27. {
  28. return b ? gcd(b, a%b) : a;
  29. }
  30.  
  31. ll bigmod(ll b,ll p,ll m)
  32. {
  33. ll res=1LL%m;
  34. ll x=b%m;
  35. while(p)
  36. {
  37. if( p & 1LL )res=(res*x) % m;
  38. x=(x*x)%m;
  39. p >>=1LL;
  40. }
  41. return res;
  42. }
  43.  
  44.  
  45. ll BIT[MaxN*10];
  46. ll arr[MaxN];
  47. ll n;
  48. ll lmt;
  49.  
  50. void update(ll pos,ll val)
  51. {
  52. for( ;pos<=(lmt);pos+= pos & -pos)
  53. {
  54. BIT[pos]+=val;
  55. }
  56. }
  57. ll query(ll pos)
  58. {
  59. ll sum=0;
  60. for(;pos>0;pos-= pos&-pos)
  61. {
  62. sum+=BIT[pos];
  63. }
  64. return sum;
  65. }
  66.  
  67.  
  68.  
  69. void blurryface()
  70. {
  71. scanf("%lld",&n);
  72. ll mx=0;
  73. ll ans=0;
  74. for(ll i=1;i<=n;i++)
  75. {
  76. scanf("%lld",&arr[i]);
  77. mx=max(mx,arr[i]);
  78.  
  79. }
  80. lmt=mx+10;
  81. for(int i=1;i<=n;i++)
  82. {
  83. ans+=query(arr[i]-1);
  84. update(arr[i],arr[i]);
  85. }
  86. printf("%lld\n",ans);
  87. int val=log2(mx)+1;
  88. int lmt=(1<<val);
  89. // cout<<lmt<<endl;
  90. for(int i=0;i<=min(lmt,1000000);i++)
  91. {
  92. BIT[i]=0;
  93. }
  94. }
  95.  
  96. int main()
  97. {
  98.  
  99. // #ifndef ONLINE_JUDGE
  100. // freopen("input.txt","r",stdin);
  101. // freopen("output.txt","w",stdout);
  102. // #endif
  103. // ios_base::sync_with_stdio(false);
  104. // cin.tie(NULL);
  105. ll t;
  106. cin>>t;
  107. while(t--)
  108. {
  109. blurryface();
  110. }
  111.  
  112.  
  113. //#ifdef LOCAL_DEFINE
  114. // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  115. // #endif
  116. ///Before submit=>
  117. /// *check for lleger overflow,array bounds
  118. /// *check for n=1
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment