Advertisement
pratiyush7

BIT(Sum on range)

Nov 13th, 2021
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. // Pratiyush Mishra
  2.  
  3. #include <bits/stdc++.h>
  4. #define ull unsigned long long int
  5. #define ll long long int
  6. #define LL_MAX 9223372036854775807
  7. #define pb push_back
  8. #define pf push_front
  9. #define mp make_pair
  10. #define popb pop_back
  11. #define vl vector<ll>
  12. #define pl pair<ll,ll>
  13. #define bs(v, x) binary_search(v.begin(), v.end(), x)
  14. #define mem1(a) memset(a, -1, sizeof(a))
  15. #define popf pop_front
  16. #define p0(x) cout << x << " "
  17. #define p1(x) cout << x << '\n'
  18. #define p2(x, y) cout << x << " " << y << '\n'
  19. #define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
  20. #define printv(v)                   \
  21.   for (ll i = 0; i < v.size(); ++i) \
  22.     cout << v[i] << " ";            \
  23.   cout << '\n'
  24. #define pr1(x) cout << fixed << setprecision(15) << x << '\n'
  25. #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update>
  26. // ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
  27. // s.find_by_order(i)  itertor to ith element (0 indexed)
  28. #define mod 1000000007
  29. #define mod1 998244353
  30. #define fio                         \
  31.   ios_base::sync_with_stdio(false); \
  32.   cin.tie(NULL)
  33. #define get(n) \
  34.   ll n;        \
  35.   cin >> n
  36. #define getvec(v, n)         \
  37.   vector<ll> v(n);           \
  38.   for (ll i = 0; i < n; i++) \
  39.     cin >> v[i];
  40. #define getstr(s) \
  41.   string s;       \
  42.   cin >> s
  43. #define all(x) x.begin(), x.end()
  44. #define countBits(x) __builtin_popcount(x)
  45. using namespace std;
  46.  
  47. // 1-indexed BIT to calculate sum in ranges...solves the following problem: https://codeforces.com/edu/course/2/lesson/4/1/practice/contest/273169/problem/A
  48.  
  49. const int M = 1e6;
  50. ll v[M], bit[M];
  51. int n, q;
  52.  
  53. void update(int pos, ll val)
  54. {
  55.     if (pos <= 0)
  56.         return;
  57.     for (; pos <= n; pos += (pos & -pos))
  58.         bit[pos] += val;
  59. }
  60.  
  61. ll query(int r)
  62. {
  63.     ll ans = 0;
  64.     for (; r > 0; r -= (r & -r))
  65.         ans += bit[r];
  66.     return ans;
  67. }
  68.  
  69. ll query(int l, int r)
  70. {
  71.     ll ans = query(r) - query(l - 1);
  72.     return ans;
  73. }
  74.  
  75. void mainSolve()
  76. {
  77.     cin >> n >> q;
  78.     for (int i = 1; i <= n; i++)
  79.         cin >> v[i], update(i, v[i]);
  80.     while (q--)
  81.     {
  82.         int t;
  83.         cin >> t;
  84.         if (t == 1)
  85.         {
  86.             ll pos, val;
  87.             cin >> pos >> val;
  88.             ++pos;
  89.             update(pos, val - v[pos]);
  90.             v[pos] = val;
  91.         }
  92.         else
  93.         {
  94.             int l, r;
  95.             cin >> l >> r;
  96.             ++l;
  97.             cout << query(l, r) << endl;
  98.         }
  99.     }
  100. }
  101.  
  102. int main()
  103. {
  104.     fio;
  105. #ifndef ONLINE_JUDGE
  106.     freopen("input.txt", "r", stdin);
  107.     freopen("output.txt", "w", stdout);
  108. #endif
  109.     //get(t);
  110.     ll t = 1;
  111.     while (t--)
  112.     {
  113.         mainSolve();
  114.     }
  115.     return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement