Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #define forn(i,x,n) for(i=x; i<n; i++)
- #define MAXN 1000000
- #define MAGIC_NUMBAH 1048576
- using namespace std;
- int steps[MAXN];
- int stree[2097152];
- int dp(long long a){
- if( a<MAXN )
- if( steps[a] ) return steps[a];
- if( a&1 )
- if(a>=MAXN) return dp(3*a+1) + 1;
- else return steps[a] = dp(3*a+1) + 1;
- else
- if(a>=MAXN) return dp(a/2) + 1;
- else return steps[a] = dp(a/2) + 1;
- }
- int query(int index, int s, int e, int qs, int qe){
- if( s>qe || qs>e ) return 0;
- if( s>=qs && e<=qe ) return stree[index];
- int mid = (s+e)/2;
- return max( query(index*2, s, mid, qs, qe) ,
- query(index*2+1, mid+1, e, qs, qe) );
- }
- int main(){
- int i, p, q, n;
- steps[1] = 1;
- forn(i, 1, MAXN+1)
- dp(i);
- /** magic number does his stuff **/
- int index;
- forn(i, 0, MAXN)
- for(index = MAGIC_NUMBAH+i; index>0; index>>=1)
- stree[index] = max(stree[index], steps[i+1]);
- cin >> n;
- forn(i, 0, n){
- cin >> p >> q;
- printf("%d %d %d\n", p, q, query(1, 0, MAXN, p, q));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement