結果

問題 No.1742 Binary Indexed Train
ユーザー tenten
提出日時 2021-11-15 10:19:51
言語 Java
(openjdk 23)
結果
AC  
実行時間 393 ms / 3,000 ms
コード長 1,869 bytes
コンパイル時間 2,500 ms
コンパイル使用メモリ 77,860 KB
実行使用メモリ 48,592 KB
最終ジャッジ日時 2024-12-14 07:47:13
合計ジャッジ時間 15,206 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            sb.append(getCount(sc.nextLong(), sc.nextLong())).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getCount(long start, long end) {
        long mid = getMid(start, end);
        return getPop(mid - start) + getPop(end - mid);
    }
    
    static long getMid(long start, long end) {
        long ans = 0;
        for (int i = 60; i >= 0; i--) {
            if ((end & (1L << i)) == 0) {
                continue;
            }
            ans += (1L << i);
            if ((start & (1L << i)) == 0) {
                break;
            }
        }
        return ans;
    }
    
    static int getPop(long x) {
        long pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return (int)pop;
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0