fork download
  1. /*
  2. De bai:
  3. Cho mảng A có N số và Q truy vấn:
  4.  
  5. Có hai loại truy vấn:
  6. 1 pos val: chỉnh a[pos] += val
  7. 2 l r: Tính tổng từ l đến r
  8. */
  9.  
  10. #include <bits/stdc++.h>
  11. #define endl '\n'
  12. typedef long long ll;
  13. using namespace std;
  14.  
  15. const ll MAXN = 1e5;
  16. ll f[MAXN], n, q;
  17.  
  18. void update(ll pos, ll val) { // Cap nhat vi tri pos len val don vi
  19. for (ll i = pos; i <= n; i += i & -i) f[i] += val;
  20. // Em khong can hieu i & -i la gi dau, chi can hoc thuoc
  21. }
  22.  
  23. ll get(ll pos) { // Ham get nay se tra ve tong tu 1 den pos
  24. ll res = 0;
  25. for (ll i = pos; i; i -= i & -i) res += f[i];
  26. return res;
  27. /*
  28.   Vi du em muon tinh tong tu l den r thi giong prefix sum: get(r) - get(l - 1)
  29.   */
  30. }
  31.  
  32. void sol() {
  33. cin >> n >> q;
  34. for (ll i = 1; i <= n; i++) {
  35. ll x; cin >> x;
  36. update(i, x);
  37. }
  38. for (ll i = 1; i <= q; i++) {
  39. ll t, u, v; cin >> t >> u >> v;
  40. if (t == 1) update(u, v);
  41. else cout << get(v) - get(u - 1) << endl;
  42. }
  43. }
  44.  
  45. int main() {
  46. ios::sync_with_stdio(0);
  47. cin.tie(0);
  48.  
  49. // freopen(".INP", "r", stdin);
  50. // freopen(".OUT", "w", stdout);
  51.  
  52. sol();
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 5320KB
stdin
4 3
1 2 3 4
2 2 4
1 2 1
2 2 4
9
10
stdout
9
10