提交时间:2024-10-22 15:33:53
运行 ID: 33793
#include<bits/stdc++.h> #define endl '\n' #define int long long #define PII pair<int,int> #define mp make_pair #define pb push_back #define fir first #define sec second using namespace std; int l,r; void slv(){ cin>>l>>r; if(l==r){ cout<<1<<endl; return; } vector<PII > v;v.clear(); v.pb(mp(l,r)); int FL=0,LIM=-1;bool cl=0; for(int x=60;x>=0;x--){ if(((l>>x)&1ll)==((r>>x)&1ll))if(!FL)continue; FL++; if(FL==1ll)LIM=x; else{ if(!((l>>x)&1ll)){ int LB=((l>>x)<<x)|(1ll<<x),RB=LB+(1ll<<x)-1ll; v.pb(mp(LB,RB)),v.pb(mp(LB+(1ll<<LIM),RB+(1ll<<LIM))); } if((r>>x)&1ll){ if(!cl){ int c=(r>>LIM)<<LIM; v.pb(mp(c,c+(1ll<<(x+1))-1ll));cl=1; } } } }if(LIM!=-1)v.pb(mp(l+(1<<LIM),l+(1<<LIM))); sort(v.begin(),v.end()); int ans=0,lst=-1; for(PII R:v){ if(lst<R.fir)ans+=R.sec-R.fir+1,lst=R.sec; else if(R.sec>lst)ans+=R.sec-lst,lst=R.sec; }cout<<ans<<endl; return; } signed main(){ int T; cin>>T; while(T--)slv(); cout.flush(); }