Advertisement
juaniisuar

Untitled

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