結果

問題 No.1742 Binary Indexed Train
ユーザー tentententen
提出日時 2021-11-15 10:19:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 371 ms / 3,000 ms
コード長 1,869 bytes
コンパイル時間 1,939 ms
コンパイル使用メモリ 74,228 KB
実行使用メモリ 59,040 KB
最終ジャッジ日時 2023-08-21 00:31:35
合計ジャッジ時間 14,846 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,328 KB
testcase_01 AC 42 ms
49,364 KB
testcase_02 AC 41 ms
49,364 KB
testcase_03 AC 323 ms
58,560 KB
testcase_04 AC 324 ms
58,972 KB
testcase_05 AC 323 ms
58,992 KB
testcase_06 AC 236 ms
56,532 KB
testcase_07 AC 254 ms
56,496 KB
testcase_08 AC 357 ms
58,424 KB
testcase_09 AC 357 ms
58,292 KB
testcase_10 AC 355 ms
58,272 KB
testcase_11 AC 365 ms
58,616 KB
testcase_12 AC 371 ms
58,280 KB
testcase_13 AC 330 ms
59,040 KB
testcase_14 AC 322 ms
58,980 KB
testcase_15 AC 182 ms
55,264 KB
testcase_16 AC 172 ms
53,092 KB
testcase_17 AC 288 ms
58,484 KB
testcase_18 AC 284 ms
58,412 KB
testcase_19 AC 215 ms
54,828 KB
testcase_20 AC 120 ms
52,312 KB
testcase_21 AC 196 ms
54,936 KB
testcase_22 AC 291 ms
58,952 KB
testcase_23 AC 125 ms
53,120 KB
testcase_24 AC 112 ms
52,712 KB
testcase_25 AC 269 ms
57,328 KB
testcase_26 AC 227 ms
56,032 KB
testcase_27 AC 254 ms
57,388 KB
testcase_28 AC 133 ms
54,948 KB
testcase_29 AC 282 ms
58,552 KB
testcase_30 AC 206 ms
55,028 KB
testcase_31 AC 317 ms
58,980 KB
testcase_32 AC 200 ms
54,684 KB
testcase_33 AC 145 ms
54,632 KB
testcase_34 AC 253 ms
57,056 KB
権限があれば一括ダウンロードができます

ソースコード

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