結果

問題 No.1742 Binary Indexed Train
ユーザー auaua
提出日時 2021-11-12 23:14:01
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 563 ms / 3,000 ms
コード長 2,083 bytes
コンパイル時間 1,519 ms
コンパイル使用メモリ 172,064 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-25 21:24:00
合計ジャッジ時間 12,643 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=200010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;



void solve(){
    ll n,q;cin>>n>>q;
    vector<ll>c(n+2);

    c[0]=1;
    REP(i,1,n+2)c[i]=c[i-1]*2LL;
    while(q--){
        ll s,t;cin>>s>>t;
        ll a=0,b=0;
        if(s){
        while(s%c[a]==0){
            a++;
        }
        a--;
        }
        while(t%c[b]==0){
            b++;
        }
        b--;
        vector<ll>x(1),y(1);
        x[0]=s,y[0]=t;
        ll now=s;
        REP(i,a+1,n+2){
            now=((now+c[i]-1LL)/c[i])*c[i];
            x.pb(now);
            //cout<<i<<' '<<now<<endl;
        }
        now=t;
        REP(i,b+1,n+2){
            now=(now/c[i])*c[i];
            y.pb(now);
            //cout<<i<<' '<<now<<endl;
        }
        ll i=0,j=0;
        ll ans=1;
        while(a<b){
            ans+=(x[i+1]-x[i])/c[a];
            a++;
            i++;
        }
        while(a>b){
            ans+=(y[j]-y[j+1])/c[b];
            b++;
            j++;
        }
        while(x[i+1]<y[j+1]){
            ans+=(x[i+1]-x[i])/c[a];
            a++;
            i++;
            ans+=(y[j]-y[j+1])/c[b];
            b++;
            j++;
        }
        //cout<<ans<<endl;
        //cout<<y[j]<<' '<<x[i]<<' '<<c[a]<<endl;
        if(y[j]>x[i])ans+=(y[j]-x[i])/c[a]-1;
        else ans--;
        cout<<ans<<endl;
    }
}

signed main(){
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    solve();
}
0