結果

問題 No.206 数の積集合を求めるクエリ
ユーザー tentententen
提出日時 2023-01-27 09:30:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 6,891 ms / 7,000 ms
コード長 3,259 bytes
コンパイル時間 2,511 ms
コンパイル使用メモリ 80,760 KB
実行使用メモリ 58,448 KB
最終ジャッジ日時 2023-09-10 06:01:39
合計ジャッジ時間 45,802 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,492 KB
testcase_01 AC 45 ms
49,316 KB
testcase_02 AC 44 ms
49,756 KB
testcase_03 AC 45 ms
49,456 KB
testcase_04 AC 46 ms
49,396 KB
testcase_05 AC 47 ms
49,524 KB
testcase_06 AC 67 ms
50,580 KB
testcase_07 AC 77 ms
50,488 KB
testcase_08 AC 72 ms
51,876 KB
testcase_09 AC 70 ms
51,620 KB
testcase_10 AC 45 ms
49,332 KB
testcase_11 AC 48 ms
49,600 KB
testcase_12 AC 106 ms
52,172 KB
testcase_13 AC 89 ms
51,388 KB
testcase_14 AC 85 ms
51,964 KB
testcase_15 AC 82 ms
52,276 KB
testcase_16 AC 83 ms
51,428 KB
testcase_17 AC 256 ms
56,260 KB
testcase_18 AC 211 ms
55,968 KB
testcase_19 AC 241 ms
56,384 KB
testcase_20 AC 200 ms
55,564 KB
testcase_21 AC 222 ms
56,444 KB
testcase_22 AC 225 ms
56,308 KB
testcase_23 AC 263 ms
55,984 KB
testcase_24 AC 6,638 ms
58,020 KB
testcase_25 AC 6,364 ms
57,464 KB
testcase_26 AC 6,891 ms
56,988 KB
testcase_27 AC 3,299 ms
57,844 KB
testcase_28 AC 5,626 ms
57,608 KB
testcase_29 AC 6,164 ms
57,696 KB
testcase_30 AC 3,412 ms
58,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int l = sc.nextInt();
        int m = sc.nextInt();
        int n = sc.nextInt();
        BitArray a = new BitArray(n);
        for (int i = 0; i < l; i++) {
            a.setBit(sc.nextInt() - 1);
        }
        BitArray b = new BitArray(n);
        for (int i = 0; i < m; i++) {
            b.setBit(sc.nextInt() - 1);
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            sb.append(a.getAndCount(b)).append("\n");
            a.shift();
        }
        System.out.print(sb);
    }
    
    static class BitArray {
        long[] arr;
        int size;
        int counter = 0;
        
        public BitArray(int x) {
            size = (x + 60 - 1) / 60;
            arr = new long[size];
        }
        
        public BitArray(int size, long[] arr) {
            this.size = size;
            this.arr = arr;
        }
        
        public void setBit(int x) {
            arr[x / 60] |= (1L << (x % 60));
        }
        
        public void shift() {
            long add = 0;
            for (int i = size - 1; i >= 0; i--) {
                arr[i] += (add << 60);
                add = arr[i] % 2;
                arr[i] >>= 1;
            }
            counter++;
            if (counter >= 60) {
                size--;
                counter = 0;
            }
        }
        
        public long getAndCount(BitArray x) {
            long count = 0;
            for (int i = 0; i < size; i++) {
                count += pitCount(arr[i] & x.arr[i]);
            }
            return count;
        }
        
        private long pitCount(long x) {
            long count = 0;
            while (x > 0) {
                count += x % 2;
                x >>= 1;
            }
            return count;
        }
        
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0