Hydracody

Untitled

Nov 10th, 2023
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #include<ext/pb_ds/assoc_container.hpp>
  4. #include<ext/pb_ds/tree_policy.hpp>
  5. using namespace __gnu_pbds;
  6. #define LagaKar ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  7. #define pb push_back
  8. #define pf push_front
  9. #define pob pop_back
  10. #define pof pop_front
  11. #define mp make_pair
  12. #define fi first
  13. #define se second
  14. #define lb lower_bound
  15. #define ub upper_bound
  16. #define ook order_of_key   //number of elements less than k
  17. #define fbo find_by_order  //k th element
  18. #define nline "\n"
  19. #define YES cout<<"YES"<<nline
  20. #define NO cout<<"NO"<<nline
  21. #define Yes cout<<"Yes"<<nline
  22. #define No cout<<"No"<<nline
  23. #define precise(ans)  cout<<fixed<<setprecision(15)<<ans
  24. #define fo(i,n) for(ll i=0;i<n;i++)
  25. #define Fo(i,k,n) for(ll i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
  26. #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
  27. #define sz(x) ((ll)(x).size())
  28. #define all(x) x.begin(), x.end()
  29. #define MAX(x) *max_element(all(x))
  30. #define MIN(x) *min_element(all(x))
  31. #define SUM(x) accumulate(all(x), 0LL)
  32. #define CNT(x) __builtin_popcountll(x)
  33. typedef long long ll; typedef unsigned long long ull; typedef long double lld;
  34. template <typename T> using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  35. template <typename T, typename R> using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  36. typedef pair<ll, ll>  pl;   typedef vector<ll>  vl;typedef vector<vl>  vvl;
  37. typedef vector<pl>  vpl;    template <typename T> using prq_mx  = priority_queue<T>;
  38. template <typename T> using prq_mn = priority_queue<T, vector<T>, greater<T>>;
  39. #ifdef hydracody
  40.     #include <C:\Users\Anurag Dwivedi\Desktop\cpc\debug.h>
  41. #else
  42. #define debug(x)
  43. #endif
  44. const double eps=1e-9;const ll INF=(ll)1e9;const ll inf64=2e18;const ll INF64=9e18;
  45. #define PI 3.1415926535897932384626
  46. #define MOD 1000000007
  47. #define MOD1 998244353
  48. #define MOD2 1000000009
  49.  
  50. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. ll power(ll x,ll y, ll p){if(y==0)return 1;ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}
  52. ll pinv(ll a, ll m){ return power(a, m - 2, m); }
  53. ll int_sqrt (ll x) {ll ans = 0; for (ll k = 1LL << 30; k != 0; k /= 2) {if ((ans + k) * (ans + k) <= x) {ans += k;}} return ans;}
  54. ll logceil(ll x){ll s=0;while(x>0){s++;x=x/2;}return s;}
  55. ll mod_add(ll a, ll b,ll mod) {
  56.     a = a % mod; b = b % mod;
  57.     return (((a + b) % mod) + mod) % mod;
  58. }
  59. ll mod_mul(ll a, ll b,ll mod) {
  60.     a = a % mod; b = b % mod;
  61.     return (((a * b) % mod) + mod) % mod;
  62. }
  63. ll mod_sub(ll a, ll b,ll mod) {
  64.     a = a % mod; b = b % mod;
  65.      return (((a - b) % mod) + mod) % mod;
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////////////////////////
  69. /*Don't loss hope, one day you will make it. Till then work hard.
  70. One day these bad days will go and you will rock.*/
  71.  
  72.  
  73.  
  74.  
  75.  
  76. struct node{
  77.   vector<ll> cc=vector<ll>(45,0);
  78.   ll count=0;
  79. };
  80.  
  81. struct Segtree {
  82.   vector<node> t;
  83.   Segtree(ll n) {
  84.     t.resize(4*n);
  85.   }
  86.   void build(vector<ll>& a,ll n, ll b, ll e) {
  87.     if (b == e) {
  88.       t[n].cc[a[b]] =1;
  89.       t[n].count=0;
  90.       return;
  91.     }
  92.     ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
  93.     build(a,l, b, mid);
  94.     build(a,r, mid + 1, e);
  95.     t[n].count=t[l].count +t[r].count;
  96.     for(ll i=40;i>0;i--){
  97.       ll s=0;
  98.       for(ll j=i+1;j<40;j++){
  99.         s+=t[l].cc[j];
  100.       }
  101.       t[n].count+=t[r].cc[i]*s;
  102.       t[n].cc[i]=t[l].cc[i]+t[r].cc[i];
  103.     }
  104.  
  105.   }
  106.   void update(ll n, ll b, ll e, ll i, ll x) {
  107.     if (b > i || e < i) return;
  108.     if (b == e && b == i) {
  109.       // t[n]= (b%2==0? x:-x);
  110.       for(ll j=1;j<41;j++){
  111.         t[n].cc[j]=0;
  112.       }
  113.       t[n].cc[x]=1;
  114.       return;
  115.     }
  116.     ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
  117.     update(l, b, mid, i, x);
  118.     update(r, mid + 1, e, i, x);
  119.     t[n].count=t[l].count +t[r].count;
  120.     for(ll i=40;i>0;i--){
  121.       ll s=0;
  122.       for(ll j=i+1;j<40;j++){
  123.         s+=t[l].cc[j];
  124.       }
  125.       t[n].count+=t[r].cc[i]*s;
  126.       t[n].cc[i]=t[l].cc[i]+t[r].cc[i];
  127.     }
  128.   }
  129.   node query(ll n, ll b, ll e, ll i, ll j) {
  130.     if (b > j || e < i) return node();
  131.     if (b >= i && e <= j){
  132.       return t[n];
  133.     }
  134.     ll mid = (b + e) >> 1, l = n << 1, r = l | 1;
  135.     node L = query(l, b, mid, i, j);
  136.     node R = query(r, mid + 1, e, i, j);
  137.     node ans;
  138.     ans.count=L.count +R.count;
  139.     for(ll i=40;i>0;i--){
  140.       ll s=0;
  141.       for(ll j=i+1;j<40;j++){
  142.         s+=L.cc[j];
  143.       }
  144.       ans.count+=R.cc[i]*s;
  145.       ans.cc[i]=L.cc[i]+R.cc[i];
  146.     }
  147.     return ans;
  148.   }
  149. };
  150. /*  
  151. [i,j]
  152. Segtree s(n);
  153. s.build(1,0,n-1);
  154. s.update(1,0,n-1,l,val);
  155. s.query(1,0,n-1,l,r);
  156. */
  157.  
  158.  
  159.  
  160.  
  161.  
  162. void  chal(){
  163.   ll n,m;
  164.   cin>>n>>m;
  165.   vl aa(n);
  166.   fo(i,n){
  167.     cin>>aa[i];
  168.   }
  169.   Segtree s(n);
  170.   s.build(aa,1,0,n-1);
  171.   fo(i,m){
  172.     ll ty;
  173.     cin>>ty;
  174.     if(ty==1){
  175.       ll l,r;
  176.       cin>>l>>r;
  177.       l--;r--;
  178.       cout<<s.query(1,0,n-1,l,r).count<<nline;
  179.     }else{
  180.       ll l,val;
  181.       cin>>l>>val;
  182.       l--;
  183.       s.update(1,0,n-1,l,val);
  184.     }
  185.   }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. }
  208.  
  209.  
  210.  
  211.  
  212. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  213. int32_t main() {
  214.   LagaKar;
  215. #ifdef hydracody
  216.   freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr);
  217. #endif
  218.   ll  t; t = 1;
  219.   // cin>>t;
  220.   for (ll i = 1; i <= t; i++) {
  221.     // cerr<<"Case : "<<i<<endl;
  222.     // cout<<"Case #"<<i<<": ";
  223.     chal();
  224.   }
  225.   return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment