結果
| 問題 |
No.206 数の積集合を求めるクエリ
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-01-27 09:23:41 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 6,695 ms / 7,000 ms |
| コード長 | 3,111 bytes |
| コンパイル時間 | 2,686 ms |
| コンパイル使用メモリ | 89,284 KB |
| 実行使用メモリ | 46,884 KB |
| 最終ジャッジ日時 | 2024-06-27 21:27:54 |
| 合計ジャッジ時間 | 46,738 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
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;
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;
}
}
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();
}
}
tenten