結果

問題 No.3205 Range Pairwise Xor Query
ユーザー tnakao0123
提出日時 2025-07-20 18:15:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,217 bytes
コンパイル時間 1,070 ms
コンパイル使用メモリ 67,632 KB
実行使用メモリ 15,480 KB
最終ジャッジ日時 2025-07-20 18:15:49
合計ジャッジ時間 20,029 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14 TLE * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:88:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   88 |   scanf("%d%d", &n, &qn);
      |   ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:89:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   89 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~
main.cpp:90:37: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   90 |   for (int i = 0; i < qn; i++) scanf("%d%d", ls + i, rs + i), ls[i]--;
      |                                ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3205.cc:  No.3205 Range Pairwise Xor Query - yukicoder
 */

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

using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_QN = 200000;
const int BN = 26;

/* typedef */

using ll = long long;

struct Elm {
  int c0, c1;
  ll s;
  Elm(): c0(), c1(), s() {}
  Elm(int v): c0(v == 0 ? 1 : 0), c1(v == 1 ? 1 : 0), s() {}
  Elm(int _c0, int _c1, ll _s): c0(_c0), c1(_c1), s(_s) {}

  Elm operator+(const Elm &e) const {
    return Elm(c0 + e.c0, c1 + e.c1, s + e.s + (ll)c0 * e.c1 + (ll)c1 * e.c0);
  }
};

template <typename T>
struct SegTreeSum {
  int e2;
  vector<T> nodes;
  T defv;
  SegTreeSum() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
    }
  }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);
    return v0 + v1;
  }
  T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int as[MAX_N], ls[MAX_QN], rs[MAX_QN];
SegTreeSum<Elm> st;
ll res[MAX_QN];

/* subroutines */

/* main */

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

  st.init(n, Elm());

  for (int k = 0; k < BN; k++) {
    for (int i = 0; i < n; i++)
      st.seti(i, Elm((as[i] >> k) & 1));
    st.setall();

    for (int i = 0; i < qn; i++) {
      auto e = st.sum_range(ls[i], rs[i]);
      res[i] += (e.s << k);
    }
  }

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

  return 0;
}


0