phantom654

Untitled

Jun 30th, 2020
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4.  
  5. #define t_case ll test;cin>>test;while(test--)
  6. #define left(nod) 2*nod
  7. #define right(nod) 2*nod+1
  8. #define ss second
  9. #define ff first
  10. #define pb push_back
  11. #define pll pair<ll,ll>
  12. #define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  13. #define deb(a) cout<<#a<<": "<<a;cout<<endl;
  14. #define deba(a...) cout<<endl;cout<<#a<<": ";for(auto it:a)cout<<it<<" ";cout<<endl;
  15.  
  16. struct fenwick_tree
  17. {
  18. vector <ll> bit;
  19. ll n;
  20.  
  21. fenwick_tree(ll size)
  22. {
  23. n=size;
  24. bit.assign(n,0);
  25. }
  26.  
  27. ll sum(ll l)
  28. {
  29. ll r=0;
  30. while(l>=0)
  31. {
  32. r+=bit[l];
  33. l=(l&(l+1))-1;
  34. }
  35. return r;
  36. }
  37.  
  38. void inc(ll i,ll del)// increse by del
  39. {
  40. while(i<=n)
  41. {
  42. bit[i]+=del;
  43. i=i|(i+1);
  44. }
  45. }
  46.  
  47. ll sum(ll l,ll r)
  48. {
  49. return sum(r)-sum(l-1);
  50. }
  51.  
  52. };
  53.  
  54. int main()
  55. {
  56. FAST;//not faster than scanf printf
  57.  
  58. ll t;cin>>t;
  59. for(ll tc=1;tc<=t;tc++)
  60. {
  61. cout<<"Case #"<<tc<<": ";
  62.  
  63. ll n,q;cin>>n>>q;ll a[200009];
  64. for(ll i=1;i<=n;i++)cin>>a[i];
  65.  
  66. fenwick_tree b1(n+9), b2(n+9);// here memory is allocated inside a funtion during runtime
  67.  
  68. for(ll i=1;i<=n;i++)
  69. {
  70. ll p;
  71. if(i%2==1)p=-1;else p=1;
  72. b1.inc(i, a[i]*i*p);
  73. b2.inc(i, a[i]*p);
  74. }
  75. ll ans=0;
  76. while(q--)
  77. {
  78. char ch;cin>>ch;
  79. if(ch=='U')
  80. {
  81. ll x,v;cin>>x>>v;ll p=1;if(x%2==1)p=-1;
  82. ll del=p*v*x - p*a[x]*x;
  83. b1.inc(x,del);
  84. del=p*v - p*a[x];
  85. b2.inc(x,del);
  86. a[x]=v;
  87. }
  88. else
  89. {
  90. ll l,r;cin>>l>>r;
  91.  
  92. ll te=b1.sum(l,r)+b2.sum(l,r)*(-l)+b2.sum(l,r);
  93. if(l%2==1)te=-te;
  94. ans+=te;
  95. }
  96. }
  97. cout<<ans<<endl;
  98. }
  99.  
  100. }
Add Comment
Please, Sign In to add comment