結果

問題 No.1240 Or Sum of Xor Pair
ユーザー SHIJOUSHIJOU
提出日時 2020-09-26 18:19:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,012 ms / 2,000 ms
コード長 2,461 bytes
コンパイル時間 2,579 ms
コンパイル使用メモリ 210,140 KB
実行使用メモリ 60,340 KB
最終ジャッジ日時 2023-09-11 23:31:14
合計ジャッジ時間 19,058 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
60,300 KB
testcase_01 AC 61 ms
60,084 KB
testcase_02 AC 62 ms
60,084 KB
testcase_03 AC 78 ms
60,308 KB
testcase_04 AC 79 ms
60,088 KB
testcase_05 AC 77 ms
60,336 KB
testcase_06 AC 74 ms
60,272 KB
testcase_07 AC 73 ms
60,300 KB
testcase_08 AC 72 ms
60,084 KB
testcase_09 AC 74 ms
60,336 KB
testcase_10 AC 153 ms
60,340 KB
testcase_11 AC 156 ms
60,308 KB
testcase_12 AC 242 ms
60,260 KB
testcase_13 AC 225 ms
60,140 KB
testcase_14 AC 232 ms
60,140 KB
testcase_15 AC 995 ms
60,252 KB
testcase_16 AC 958 ms
60,296 KB
testcase_17 AC 896 ms
60,340 KB
testcase_18 AC 935 ms
60,256 KB
testcase_19 AC 877 ms
60,296 KB
testcase_20 AC 1,002 ms
60,332 KB
testcase_21 AC 1,002 ms
60,304 KB
testcase_22 AC 1,012 ms
60,084 KB
testcase_23 AC 884 ms
60,300 KB
testcase_24 AC 981 ms
60,256 KB
testcase_25 AC 775 ms
60,312 KB
testcase_26 AC 985 ms
60,080 KB
testcase_27 AC 61 ms
60,272 KB
testcase_28 AC 62 ms
60,160 KB
testcase_29 AC 156 ms
60,304 KB
testcase_30 AC 151 ms
60,296 KB
testcase_31 AC 200 ms
60,160 KB
testcase_32 AC 436 ms
60,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}


struct SegTree {
	struct node {
		int sum;
		vector<int> cc;
		node():sum(0), cc(18) {}
		int &operator[] (int i) {return cc[i];}
	};
	using T = node;
	int n;
	const T ex;
	vector<T> dat;
	SegTree(T ex_ = node()):ex(ex_), n(1<<18) {
		dat.assign((n<<1)-1, ex);
	}
	inline int chld(int k) {return (k<<1)+1;}
	inline int chrd(int k) {return (k<<1)+2;}
	inline ll query(int a, int x) {return query(a, x, 17, 0, 0, n);}
	ll query(int a, int x, int z, int k, int l, int r) {
		if(l >= x) return 0;
		if(r <= x) {
			ll res = 0;
			rep(i, 18) {
				if(a>>i&1) res += dat[k].sum * (1LL<<i);
				else res += dat[k][i] * (1LL<<i);
			}
			return res;
		}
		ll vl = query(a, x, z-1, (~a>>z&1)?chld(k):chrd(k), l, (l+r)>>1);
		ll vr = query(a, x, z-1, (~a>>z&1)?chrd(k):chld(k), (l+r)>>1, r);
		return vl + vr;
	}
	void update(int a) {update(a, 0, 0, n);}
	void update(int a,int k, int l, int r) {
		if(r-l == 1) {
			dat[k].sum++;
			rep(j, 18) if(a>>j&1) {
				dat[k][j]++;
			}
		}
		else {
			if(a < ((l+r)>>1)) update(a, chld(k), l, (l+r)>>1);
			else update(a, chrd(k), (l+r)>>1, r);
			T &c = dat[k];
			c.sum++;
			rep(i, 18) if(a>>i&1) c[i]++;
		}
	}
	const T &operator[](int idx) const {return dat[idx+n-1];}
	T &operator[](int idx) {return dat[idx+n-1];}
};

#define N 200100

//head

int n, x;
SegTree seg;
ll ans;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> x;
	rep(i, n) {
		int a;
		cin >> a;
		ans += seg.query(a, x);
		seg.update(a);
	}
	cout << ans << endl;
}
0