Advertisement
juaniisuar

Untitled

May 2nd, 2015
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #define forn(i,x,n) for(i=x; i<n; i++)
  5. #define MAXN 1000000
  6. #define MAGIC_NUMBAH 1048576
  7.  
  8. using namespace std;
  9.  
  10. int steps[MAXN];
  11. int stree[2097152];
  12.  
  13. int dp(long long a){
  14. if( a<MAXN )
  15. if( steps[a] ) return steps[a];
  16.  
  17. if( a&1 )
  18. if(a>=MAXN) return dp(3*a+1) + 1;
  19. else return steps[a] = dp(3*a+1) + 1;
  20. else
  21. if(a>=MAXN) return dp(a/2) + 1;
  22. else return steps[a] = dp(a/2) + 1;
  23. }
  24.  
  25. int query(int index, int s, int e, int qs, int qe){
  26. if( s>qe || qs>e ) return 0;
  27. if( s>=qs && e<=qe ) return stree[index];
  28.  
  29. int mid = (s+e)/2;
  30. return max( query(index*2, s, mid, qs, qe) ,
  31. query(index*2+1, mid+1, e, qs, qe) );
  32. }
  33.  
  34. int main(){
  35. int i, p, q, n;
  36. steps[1] = 1;
  37.  
  38. forn(i, 1, MAXN+1)
  39. dp(i);
  40.  
  41. /** magic number does his stuff **/
  42. int index;
  43. forn(i, 0, MAXN)
  44. for(index = MAGIC_NUMBAH+i; index>0; index>>=1)
  45. stree[index] = max(stree[index], steps[i+1]);
  46.  
  47. cin >> n;
  48. forn(i, 0, n){
  49. cin >> p >> q;
  50. printf("%d %d %d\n", p, q, query(1, 0, MAXN, p, q));
  51. }
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement