Guest User

Segment vs Fenwick

a guest
Oct 8th, 2016
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <deque>
  7. #include <iomanip>
  8. #include <iostream>
  9. #include <queue>
  10. #include <map>
  11. #include <numeric>
  12. #include <set>
  13. #include <sstream>
  14. #include <stack>
  15. #include <utility>
  16. #include <vector>
  17.  
  18. #define INF 1000000000
  19. #define FOR(i, a, b) for(int i=int(a); i<int(b); i++)
  20. #define FORC(cont, it) for(decltype((cont).begin()) it = (cont).begin(); it != (cont).end(); it++)
  21. #define pb push_back
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef pair<int, int> ii;
  27. typedef vector<int> vi;
  28. typedef vector<ii> vii;
  29. typedef vector<vi> vvi;
  30.  
  31. #define maxN 100000
  32. #define maxQ 100000
  33. #define tests 10000
  34.  
  35. struct Query {
  36.     int type, l, r;
  37. };
  38.  
  39. int N, Q;
  40. ll segTree[2 * maxN], bit[maxN+1], a[maxN], ansSeg[maxQ], ansBit[maxQ];
  41. Query q[maxQ];
  42.  
  43. ll querySeg(int l, int r) { // [l,r)
  44.     ll ans = 0;
  45.     for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
  46.         if (l & 1) ans += segTree[l++];
  47.         if (r & 1) ans += segTree[--r];
  48.     }
  49.     return ans;
  50. }
  51.  
  52. void modifySeg(int n, int m) {
  53.     segTree[n += N] += m;
  54.     while (n >>= 1) {
  55.         segTree[n] = segTree[n << 1] + segTree[n << 1 | 1];
  56.     }
  57. }
  58.  
  59. ll queryFenwick(int l) {
  60.     ll sum = 0;
  61.     while (l) {
  62.         sum += bit[l];
  63.         l -= (l & -l);
  64.     }
  65.     return sum;
  66. }
  67.  
  68. void modifyFenwick(int n, ll m) {
  69.     while (n <= N) {
  70.         bit[n] += m;
  71.         n += n & -n;
  72.     }
  73. }
  74.  
  75. int main() {
  76.     double totalSeg = 0, totalFenwick = 0;
  77.     FOR(test, 0, tests) {
  78.         // Generate random case
  79.         N = rand() % maxN;
  80.         Q = rand() % maxQ;
  81.         FOR(i, 0, N) {
  82.             a[i] = (ll)rand()<<16 | rand();
  83.         }
  84.         FOR(i, 0, Q) {
  85.             q[i].type = rand() & 1;
  86.             q[i].l = rand() % N;
  87.             if (q[i].type) {
  88.                 q[i].r = rand() % N;
  89.                 if (q[i].l > q[i].r) swap(q[i].l, q[i].r);
  90.             }
  91.             else {
  92.                 q[i].r = rand();
  93.             }
  94.         }
  95.  
  96.         // Start Segment tree
  97.         clock_t t = clock();
  98.            
  99.         // Initialization
  100.         FOR(i, 0, N) segTree[i + N] = a[i];
  101.         for (int i = N - 1; i; i--) segTree[i] = segTree[i << 1] + segTree[i << 1 | 1];
  102.  
  103.         // Queries
  104.         FOR(i,0,Q) {
  105.             if (q[i].type) ansSeg[i] = querySeg(q[i].l, q[i].r + 1);
  106.             else modifySeg(q[i].l, q[i].r), ansSeg[i] = 0;
  107.         }
  108.        
  109.         double testTime = (float)(clock() - t)/CLOCKS_PER_SEC;
  110.         //printf("Segment tree: %.3f\n", testTime);
  111.         totalSeg += testTime;
  112.         // End Segment Tree
  113.  
  114.         // Start Fenwick
  115.         t = clock();
  116.            
  117.         // Initialization
  118.         memset(bit, 0, sizeof(bit));
  119.         FOR(i,1,N+1) {
  120.             modifyFenwick(i, a[i - 1]);
  121.         }
  122.  
  123.         // Queries
  124.         FOR(i, 0, Q) {
  125.             if (q[i].type) ansBit[i] = queryFenwick(q[i].r + 1) - queryFenwick(q[i].l);
  126.             else {
  127.                 modifyFenwick(q[i].l + 1, q[i].r);
  128.                 ansBit[i] = 0;
  129.             }
  130.         }
  131.  
  132.         testTime = (float)(clock() - t) / CLOCKS_PER_SEC;
  133.         //printf("Fenwick tree: %.3f\n", testTime);
  134.         totalFenwick += testTime;
  135.         // End Fenwick
  136.  
  137.         // To make sure my code is not bugged :)
  138.         FOR(i, 0, Q) {
  139.             if (ansBit[i] != ansSeg[i]) {
  140.                 printf("Something is wrong =(\n");
  141.             }
  142.         }
  143.     }
  144.     printf("Total Segment tree: %.3f\n", totalSeg);
  145.     printf("Total Fenwick tree: %.3f\n", totalFenwick);
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment