fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int N = 100000;
  5. int tr[101010];
  6. void add(int pos, int val) {
  7. for (int i = pos; i <= N; i |= (i + 1)) {
  8. tr[i] += val;
  9. }
  10. }
  11. int sum(int pos) {
  12. int res = 0;
  13. for (int i = pos; i >= 0; i = (i&(i + 1)) - 1) {
  14. res += tr[i];
  15. }
  16. return res;
  17. }
  18.  
  19. int main() {
  20. // your code goes here
  21. add(0, 5);
  22. add(7, 10);
  23. cout << sum(0) << ' ' << sum(7) << ' ' << sum(100) << endl;
  24. return 0;
  25. }
Success #stdin #stdout 0s 3860KB
stdin
Standard input is empty
stdout
5 15 15