結果

問題 No.3101 Range Eratosthenes Query
ユーザー tnakao0123
提出日時 2025-04-14 12:02:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 214 ms / 3,000 ms
コード長 1,624 bytes
コンパイル時間 1,028 ms
コンパイル使用メモリ 59,004 KB
実行使用メモリ 43,568 KB
最終ジャッジ日時 2025-04-14 12:02:37
合計ジャッジ時間 7,092 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:67:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   67 |   scanf("%d", &qn);
      |   ~~~~~^~~~~~~~~~~
main.cpp:68:37: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |   for (int i = 0; i < qn; i++) scanf("%d%d", ls + i, rs + i);
      |                                ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3101.cc:  No.3101 Range Eratosthenes Query - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 1000000;
const int MAX_QN = 200000;

/* typedef */

using vi = vector<int>;

template <typename T>
struct BIT {
  int n;
  vector<T> bits;
  
  BIT() {}
  BIT(int _n) { init(_n); }

  void init(int _n) {
    n = _n;
    bits.assign(n + 1, 0);
  }

  T sum(int x) {
    x = min(x, n);
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    if (x <= 0) return;
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

/* global variables */

bool primes[MAX_N + 1];
int xds[MAX_N + 1];
int ls[MAX_QN], rs[MAX_QN], res[MAX_QN];
vi rvs[MAX_N + 1];
BIT<int> bit;

/* subroutines */

/* main */

int main() {
  int qn;
  scanf("%d", &qn);
  for (int i = 0; i < qn; i++) scanf("%d%d", ls + i, rs + i);

  int n = *max_element(rs, rs + qn);

  fill(primes, primes + n + 1, true);
  primes[0] = primes[1] = false;
  fill(xds, xds + n + 1, 1);
  
  for (int p = 2; p * p <= n; p++)
    if (primes[p]) {
      for (int q = p * 2; q <= n; q += p) {
	primes[q] = false;
	xds[q] = max(xds[q], q / p);
      }
    }
  
  fill(res, res + qn, 1);
  for (int i = 0; i < qn; i++)
    if (ls[i] > 1) rvs[rs[i]].push_back(i);

  bit.init(n);
  for (int x = 1; x <= n; x++) {
    bit.add(xds[x], 1);

    for (auto i: rvs[x])
      res[i] = bit.sum(ls[i] - 1) - (ls[i] - 1);
  }

  for (int i = 0; i < qn; i++) printf("%d\n", res[i]);
  
  return 0;
}
0