Advertisement
Guest User

Fenwick_20.08.13

a guest
Aug 20th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <bitset>
  7. #include <iostream>
  8. #include <stack>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. #include <string>
  13. #include <algorithm>
  14. using namespace std;
  15.  
  16. int h(int t) {
  17.     int r = 0;
  18.     while (t % (1 << r) == 0)
  19.         r++;
  20.     return --r;
  21. }
  22.  
  23. int l(int t) {
  24.     return 1 << h(t);
  25. }
  26.  
  27. int n;
  28. long long int a[100100];
  29.  
  30. void solve(int v) {
  31.     int lV = l(v);
  32.     if (lV == 1) return;
  33.    
  34.     long long int s = 0;
  35.     for(int i = v-lV+1; i < v - 1; ++i) {
  36.         s += a[i];
  37.     }
  38.     a[v-1] = -s;
  39. }
  40.  
  41. int main() {
  42.     //freopen("input.txt", "r", stdin);
  43.     //freopen("output.txt", "w", stdout);
  44.     freopen("fenwick.in", "r", stdin);
  45.     freopen("fenwick.out", "w", stdout);
  46.  
  47.     scanf("%d", &n);
  48.     for(int i = 1; i <= n; ++i) scanf("%lld", &(a[i]));
  49.  
  50.     for(int i = 1; i <= n; ++i) {
  51.         solve(i);
  52.     }  
  53.  
  54.     for(int i = 1; i <= n; ++i) {
  55.         printf("%lld ", a[i]);
  56.     }
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement