結果
問題 |
No.3101 Range Eratosthenes Query
|
ユーザー |
![]() |
提出日時 | 2025-04-21 15:31:24 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 2,270 ms / 3,000 ms |
コード長 | 5,115 bytes |
コンパイル時間 | 3,116 ms |
コンパイル使用メモリ | 84,240 KB |
実行使用メモリ | 82,664 KB |
最終ジャッジ日時 | 2025-04-21 15:32:13 |
合計ジャッジ時間 | 48,589 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 24 |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(); int q = sc.nextInt(); Query[] queries = new Query[q]; for (int i = 0; i < q; i++) { queries[i] = new Query(i, sc.nextInt(), sc.nextInt()); } Arrays.sort(queries); SegmentTree st = new SegmentTree(1000001); int prev = 1000001; int[] ans = new int[q]; for (Query x : queries) { for (int i = x.left; i < prev; i++) { for (int j = 2; j * i <= 1000000; j++) { st.setOne(j * i); } } prev = x.left; ans[x.idx] = x.right - x.left + 1 - st.getSum(x.left, x.right + 1); } System.out.println(Arrays.stream(ans).mapToObj(String::valueOf).collect(Collectors.joining("\n"))); } static class SegmentTree { int size; int[] values; public SegmentTree(int x) { size = 2; while (size < x) { size <<= 1; } values = new int[size * 2 - 1]; } public void setOne(int idx) { values[idx + size - 1] = 1; calc((idx + size - 2) / 2); } private void calc(int idx) { values[idx] = values[idx * 2 + 1] + values[idx * 2 + 2]; if (idx > 0) { calc((idx - 1) / 2); } } public int getSum(int left, int right) { return getSum(0, 0, size, left, right); } private int getSum(int idx, int min, int max, int left, int right) { if (max <= left || right <= min) { return 0; } else if (left <= min && max <= right) { return values[idx]; } else { return getSum(idx * 2 + 1, min, (min + max) / 2, left, right) + getSum(idx * 2 + 2, (min + max) / 2, max, left, right); } } } static class Query implements Comparable<Query> { int idx; int left; int right; public Query(int idx, int left, int right) { this.idx = idx; this.left = left; this.right = right; } public int compareTo(Query another) { return another.left - left; } } } class FastScanner { private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private static final int BUFFER_SIZE = 1 << 16; // 65,536 バイト public FastScanner() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private byte read() { try { if (bufferPointer == bytesRead) { bytesRead = din.read(buffer, 0, BUFFER_SIZE); if (bytesRead == -1) return -1; bufferPointer = 0; } return buffer[bufferPointer++]; } catch (IOException e) { throw new RuntimeException(e); } } public String next() { byte b = read(); // 空白文字を読み飛ばす while (isSpaceChar(b)) { b = read(); } StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { sb.append((char) b); b = read(); } return sb.toString(); } public int nextInt() { int ret = 0; byte c = read(); while (isSpaceChar(c)) c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (!isSpaceChar(c)); return neg ? -ret : ret; } public long nextLong() { long ret = 0; byte c = read(); while (isSpaceChar(c)) c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10L + c - '0'; c = read(); } while (!isSpaceChar(c)); return neg ? -ret : ret; } public double nextDouble() { double ret = 0, div = 1; byte c = read(); while (isSpaceChar(c)) c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (!isSpaceChar(c) && c != '.'); if (c == '.') { while (!isSpaceChar(c = read())) { ret += (c - '0') / (div *= 10); } } return neg ? -ret : ret; } private boolean isSpaceChar(byte c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public void close() { try { if (din != null) din.close(); } catch (IOException e) { throw new RuntimeException(e); } } }