結果

問題 No.3205 Range Pairwise Xor Query
ユーザー kwm_t
提出日時 2025-07-18 21:55:24
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,829 bytes
コンパイル時間 3,982 ms
コンパイル使用メモリ 289,488 KB
実行使用メモリ 15,936 KB
最終ジャッジ日時 2025-07-18 21:55:47
合計ジャッジ時間 8,114 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 5 TLE * 1 -- * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
#include <algorithm>
#include <vector>
#include <iostream>
#include <numeric>
#include <functional>
struct Mo {
	int width;
	std::vector<int>left, right, order;
	//std::vector<long long>horder;
	int n, q;
	Mo(int _n, int _q) :n(_n), q(_q) {
		width = n / min<int>(n, std::sqrt(q));
		order.resize(q);
		std::iota(order.begin(), order.end(), 0);
	}
	// add query[l,r)
	void query(const std::vector<int> &l, const std::vector<int> &r) {
		left = l;
		right = r;
		/*
		horder.resize(q);
		for (int i = 0; i < q; ++i) horder[i] = hilbertorder(left[i], right[i]);
		*/
	}
	// run
	void run(const std::function<void(int, int, int)> &upd, const std::function<void(int)> &set) {
		std::sort(order.begin(), order.end(), [&](int lh, int rh) {
			//return horder[lh] < horder[rh];
			int lblock = left[lh] / width;
			int rblock = left[rh] / width;
			if (lblock != rblock)return lblock < rblock;
			if (lblock & 1) return right[lh] < right[rh];
			return right[lh] > right[rh];
			});
		int nl = 0, nr = 0;
		for (auto idx : order) {
			while (nl > left[idx])upd(0, --nl, nr);
			while (nr < right[idx])upd(1, nl, ++nr);
			while (nl < left[idx])upd(2, nl++, nr);
			while (nr > right[idx])upd(3, nl, nr--);
			set(idx);
		}
	}
	// TLEするときはこっちを使うと通るかも
	/*const int logn = 20;
	const int maxn = 1 << logn;
	long long hilbertorder(int x, int y) {
		long long d = 0;
		for (int s = 1 << logn - 1; s; s >>= 1) {
			bool rx = x & s, ry = y & s;
			d = d << 2 | rx * 3 ^ static_cast<int>(ry);
			if (ry) continue;
			if (rx) {
				x = maxn - x;
				y = maxn - y;
			}
			std::swap(x, y);
		}
		return d;
	}*/
};
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n, q; cin >> n >> q;
	vector<long long>a(n);
	rep(i, n)cin >> a[i];
	vector<int>l(q), r(q);
	rep(i, q)cin >> l[i] >> r[i];
	rep(i, q) {
		l[i]--, r[i]--;
	}
	Mo mo(n, q);
	mo.query(l, r);
	vector<long long>ans(q);
	vector<long long>cnt(26);
	long long val = 0;
	rep(i, 26)cnt[i] = 1 & (a[0] >> i);
	auto upd = [&](int type, int l, int r) {
		if (type == 0) {// l--
			int sz = r - l;
			rep(i, 26) {
				long long pw = 1LL << i;
				if (1 & (a[l] >> i)) {
					val += pw * (sz - cnt[i]);
					cnt[i]++;
				}
				else val += pw * cnt[i];
			}
		}
		else if (type == 1) {// r++
			int sz = r - l;
			rep(i, 26) {
				long long pw = 1LL << i;
				if (1 & (a[r] >> i)) {
					val += pw * (sz - cnt[i]);
					cnt[i]++;
				}
				else val += pw * cnt[i];
			}
		}
		else if (type == 2) {// l++
			int sz = r - l;
			rep(i, 26) {
				long long pw = 1LL << i;
				if (1 & (a[l] >> i)) {
					cnt[i]--;
					val -= pw * (sz - cnt[i]);
				}
				else val -= pw * cnt[i];
			}
		}
		else if (type == 3) {// r--
			int sz = r - l;
			rep(i, 26) {
				long long pw = 1LL << i;
				if (1 & (a[r] >> i)) {
					cnt[i]--;
					val -= pw * (sz - cnt[i]);
				}
				else val -= pw * cnt[i];
			}
		}
	};
	auto set = [&](int idx) {
		ans[idx] = val;
	};
	mo.run(upd, set);
	rep(i, q)cout << ans[i] << endl;
	return 0;
}
0