結果

問題 No.3423 Minimum Xor Query
コンテスト
ユーザー Ohuton_Racing
提出日時 2026-01-11 16:04:22
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
TLE  
実行時間 -
コード長 6,085 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,830 ms
コンパイル使用メモリ 292,164 KB
実行使用メモリ 13,712 KB
最終ジャッジ日時 2026-01-11 16:04:39
合計ジャッジ時間 17,434 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;

template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template <int m> istream& operator>>(istream& is, static_modint<m>& a) {long long x; is >> x; a = x; return is;}
template <int m> istream& operator>>(istream& is, dynamic_modint<m>& a) {long long x; is >> x; a = x; return is;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}
template<typename T> ostream& operator<<(ostream& os, const set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename T> ostream& operator<<(ostream& os, const multiset<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename T> ostream& operator<<(ostream& os, const unordered_set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename S, auto op, auto e> ostream& operator<<(ostream& os, const atcoder::segtree<S, op, e>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}
template<typename S, auto op, auto e, typename F, auto mapping, auto composition, auto id> ostream& operator<<(ostream& os, const atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}

template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}

// Mo's algorithm for q queries(Ranges [l, r] should be 0-indexed!)
// T is the typename of Answers

template<typename T = long long>
struct Mo{
	int n;
	vector<int> a;
	int q;
	vector<int> l, r;
	vector<int> qs;
	vector<T> ans;
	int Ma = 500005;
	Mo(int _n, vector<int> _a, int _q, vector<int> _l, vector<int> _r) : n(_n), a(_a), q(_q), l(_l), r(_r), qs(_q), ans(_q) {
		assert(int(a.size()) == n);
		assert(int(l.size()) == q);
		assert(int(r.size()) == q);
		rep(i, q) r[i]++;
		rep(i, q) qs[i] = i;
		int D = n / (sqrt(q) + 1) + 1;
		sort(qs.begin(), qs.end(), [&](int i, int j){
			int di = r[i] / D, dj = r[j] / D;
			if(di == dj) return l[i] < l[j];
			return r[i] < r[j];
		});
		rep(i, n) Ma = max(Ma, a[i] + 5);
	}
	vector<T> solve(){
		int li = 0, ri = 0;
		T now = 0;
		vector<int> cnt(Ma);
		auto calc = [&](int x){
			T res = x / 2;
			return res;
		};
		auto add = [&](int i){
			now -= calc(cnt[a[i]]);
			cnt[a[i]]++;
			now += calc(cnt[a[i]]);
		};
		auto del = [&](int i){
			now -= calc(cnt[a[i]]);
			cnt[a[i]]--;
			now += calc(cnt[a[i]]);
		};
		for(int qi : qs){
			int nl = l[qi], nr = r[qi];
			while(ri < nr) add(ri), ri++;
			while(li > nl) li--, add(li);
			while(ri > nr) ri--, del(ri);
			while(li < nl) del(li), li++;
			ans[qi] = now;
			
		}
	return ans;
	}
};

int main(){
	int N, Q;
	cin >> N >> Q;
	vector<int> A(N);
	cin >> A;
	vector<tuple<int, int, int>> ChangeQuery;
	vector<pair<int, int>> AnswerQuery;
	int ChangeCount = 0;
	int AnswerCount = 0;
	{
		vector<int> A_copy = A;
		rep(i, Q){
			int t;
			cin >> t;
			if(t == 1){
				int i, x;
				cin >> i >> x;
				i--;
				ChangeQuery.emplace_back(i, A_copy[i], x);
				A_copy[i] = x;
				ChangeCount++;
			}
			if(t == 2){
				int r;
				cin >> r;
				AnswerQuery.emplace_back(r, ChangeCount);
				AnswerCount++;	
			}
		}
	}
	vector<int> AnswerSort(AnswerCount);
	rep(i, AnswerCount) AnswerSort[i] = i;
	
//	int D = sqrt(N + AnswerCount);
	int D = 500;
	
	sort(AnswerSort.begin(), AnswerSort.end(), [&](int i, int j){
		auto [xi, yi] = AnswerQuery[i];
		auto [xj, yj] = AnswerQuery[j];
		int di = yi / D, dj = yj / D;
		if(di == dj) return xi < xj;
		return yi < yj;
	});
	
	multiset<int> alpha;
	multiset<int> beta;
	
	auto add = [&](int val){
		auto right = alpha.upper_bound(val);
		auto left = prev(right);
		
		if(right != alpha.begin() and right != alpha.end()){
			beta.erase(beta.find(*right xor *left));
		}
		if(right != alpha.begin()){
			beta.insert(val xor *left);
		}
		if(right != alpha.end()){
			beta.insert(val xor *right);
		}
		alpha.insert(val);
	};
	auto del = [&](int val){
		auto itr = alpha.find(val);
		auto left = prev(itr);
		auto right = next(itr);
		if(itr != alpha.begin()){
			beta.erase(beta.find(val xor *left));
		}
		if(right != alpha.end()){
			beta.erase(beta.find(val xor *right));
		}
		if(itr != alpha.begin() and right != alpha.end()){
			beta.insert(*left xor *right);
		}
		alpha.erase(itr);
	};
	vector<int> Ans(AnswerCount);
	{
		int x = 2, y = 0;
		alpha.insert(A[0]);
		alpha.insert(A[1]);
		beta.insert(A[0] xor A[1]);
		
		auto right = [&](){
			add(A[x]);
			x++;
		};
		auto left = [&](){
			x--;
			del(A[x]);
		};
		auto up = [&](){
			auto [i, from, to] = ChangeQuery[y];
			if(i < x) del(A[i]);
			A[i] = to;
			if(i < x) add(A[i]);
			y++;
		};
		auto down = [&](){
			y--;
			auto [i, from, to] = ChangeQuery[y];
			if(i < x) del(A[i]);
			A[i] = from;
			if(i < x) add(A[i]);
		};
		
		
		for(int qi : AnswerSort){
			auto [x_goal, y_goal] = AnswerQuery[qi];
			while(x < x_goal) right();
			while(y > y_goal) down();
			while(x > x_goal) left();
			while(y < y_goal) up();
			Ans[qi] = *beta.begin();
			
			/*
			cerr << x << ' ' << y << "\n";
			cerr << alpha;
			cerr << beta;
			cerr<< "--------\n";
			*/
		}
	}
	for(auto x : Ans){
		cout << x << "\n";
	}
	
	
	return 0;
}
0