Advertisement
esraa_syam

-><-

Jul 23rd, 2023
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define nl "\n"
  3. #define fi first
  4. #define se second
  5. #define pi 3.14159
  6. #define ll long long
  7. #define odd(a)  (a&1)
  8. #define even(a)  !(a&1)
  9. #define Mod 1'000'000'007
  10. #define INF 2'000'000'000
  11. #define sz(x) int(x.size())
  12. #define charToInt(s) (s - '0')
  13. #define ull unsigned long long
  14. #define number_line iota(all(vec) , 1)
  15. #define all(s) s.begin(), s.end()
  16. #define rall(v) v.rbegin() , v.rend()
  17. #define fixed(n) fixed << setprecision(n)
  18. #define Num_of_Digits(n) ((int)log10(n) + 1)
  19. #define to_decimal(bin) stoll(bin, nullptr, 2)
  20. #define Ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  21. #define Floor(n, m) (((n) / (m)) - ((n) % (m) ? 0 : 1))
  22. #define Upper(s) transform(all(s), s.begin(), ::toupper);
  23. #define Lower(s) transform(all(s), s.begin(), ::tolower);
  24. #define cout_map(mp) for(auto& [f, s] : mp) cout << f << "  " << s << "\n";
  25. //  ----- bits-------
  26. #define pcnt(n) __builtin_popcount(n)  
  27. #define pcntll(n) __builtin_popcountll(n)
  28. #define clz(n) __builtin_clz(n)    // <---100
  29. #define clzll(n) __builtin_clzll(n)
  30. #define ctz(n) __builtin_ctz(n)  // 0001---->
  31. #define ctzll(n) __builtin_ctzll(n)
  32.  
  33. using namespace std;
  34.  
  35. template < typename T = int > istream& operator >> (istream &in, vector < T > & v){
  36.     for(auto & x : v) in >> x;
  37.     return in;
  38. }
  39.  
  40. template < typename T = int > ostream& operator << (ostream &out, const vector < T > & v){
  41.     for(const T & x : v) out << x << " ";
  42.     return out;
  43. }
  44. void esraa()
  45. {
  46.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  47. #ifndef ONLINE_JUDGE
  48.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  49. #endif
  50. }
  51. template < typename T >
  52. struct FenwickTree{
  53.     vector < T > tree;
  54.     int n;
  55.     FenwickTree(int _n){
  56.         n = _n;
  57.         tree.resize(n + 1);
  58.     }
  59.     void add(int idx, T val){
  60.         while(idx <= n){
  61.             tree[idx] += val;
  62.             idx += (idx & -idx);
  63.         }
  64.     }
  65.     T sum(int idx){
  66.         T ret = 0;
  67.         while(idx > 0){
  68.             ret += tree[idx];
  69.             idx -= (idx & -idx);
  70.         }
  71.         return ret;
  72.     }
  73.     T sum(int l, int r){
  74.         if(l > r) return 0;
  75.         return sum(r) - sum(l - 1);
  76.     }
  77.     void upadte_range(int l, int r, T val){
  78.         add(l, val);
  79.         add(r + 1 , -val);
  80.     }
  81.     T get(int idx){
  82.         return sum(idx, idx);
  83.     }
  84. };
  85. void solve(){
  86.     int n, q;
  87.     cin >> n >> q;
  88.     FenwickTree < ll > tree(n);
  89.     vector < ll > v(n);
  90.     cin >> v;
  91.     for(int i = 0; i < n; i++){
  92.         tree.add(i + 1, v[i]);
  93.     }
  94.     while(q--){
  95.         int type;
  96.         cin >> type;
  97.         if(type == 1){
  98.             int l, r, val;
  99.             cin >> l >> r >> val;
  100.             tree.upadte_range(l, r, val);
  101.         }else{
  102.             int idx;
  103.             cin >> idx;
  104.             cout << tree.get(idx) << nl;
  105.         }
  106.     }    
  107. }
  108. int main()
  109. {
  110.     esraa();
  111.     int t = 1;
  112.     //cin >> t;
  113.     while(t--)
  114.       solve();
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement