Advertisement
Guest User

Untitled

a guest
May 1st, 2011
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. vector<int> t[10];
  7.  
  8.  
  9. // some auxillary function, that returns sometimes 0, sometimes non-zero
  10. int auxillary (const vector<int> & v, int arg) {
  11.     int result = 0;
  12.  
  13.     // <smth>
  14.     // here may be anything, complex enough to stop optimizer
  15.     for (size_t i=0; i<v.size(); ++i)
  16.         if (arg <= v[i])
  17.             ++result;
  18.     // </smth>
  19.  
  20.     cout << "auxillary = " << result << endl;
  21.     return result;
  22. }
  23.  
  24.  
  25. // some complex recursive function, that performs calls to auxillary()
  26. int query (int v, int tl, int tr, int lx, int rx, int arg) {
  27.     if (lx > rx)  return 0;
  28.  
  29.     // sometimes the recursion results in calling auxillary()
  30.     if (tl == lx && rx == tr) {
  31.         cout << "call auxillary (t[" << v << "], " << arg << "):" << endl;
  32.         return auxillary (t[v], arg);
  33.     }
  34.  
  35.     // <smth>
  36.     // here might be any complex recursive calls, resulting in auxillary() calls
  37.     int tm = (tl + tr) / 2;
  38.     int ans1 = query (v*2, tl, tm, lx, min(rx,tm), arg);
  39.     int ans2 = query (v*2+1, tm+1, tr, max(lx,tm+1), rx, arg);
  40.     int ans = ans1 + ans2;
  41.     // </smth>
  42.  
  43.     //cout << "query = " << ans << endl;
  44.     // ^^^^^ UNCOMMENT THIS LINE TO MAKE THE PROGRAM WORK OK
  45.  
  46.     return ans;
  47. }
  48.  
  49.  
  50. int main() {
  51.     t[9].push_back (456); // some data to make auxillary() return non-zero sometimes
  52.  
  53.     cout << "global ans = " << query (1, 0, 5, 1, 2, 123) << endl;
  54.     // ^^^^^ SHOULD OUTPUT "1"
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement