Language: C++

#include <iostream>
#include <algorithm>
#include <array>
#include <cstdint>
#include <climits>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define INF 1000000000
#define MOD 1000000007
#define rep(i,a,b) for(uint32 i = (a); i < (b); ++i)
#define ALL(x) (x).begin(),(x).end()
//#define scanf scanf_s

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;

#include <iostream>
#include <algorithm>
#include <array>
#include <cstdint>
#include <climits>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define INF 1000000000
#define MOD 1000000007
#define rep(i,a,b) for(uint32 i = (a); i < (b); ++i)
#define bitget(a,b) (((a) >> (b)) & 1)
#define ALL(x) (x).begin(),(x).end()
#define C(x) std::cout << #x << " : " << x << std::endl
#define scanf scanf_s

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;

template<typename T>
class dynamic_wavelet_matrix {
	struct dynamic_FID {
		struct node_t {
			uint32 size, sum, ch[2];
			bool color, bit;
		};
		std::vector<node_t> tree;
		uint32 root, border,post;
		bool data, balance;
		dynamic_FID() {
			tree.push_back({ 0,0,0,0,1,0 });
			root = 0;
			border = 0;
			return;
		}
		uint32 recalc(uint32 node) {
			tree[node].size = tree[tree[node].ch[0]].size + tree[tree[node].ch[1]].size + 1;
			tree[node].sum = tree[tree[node].ch[0]].sum + tree[tree[node].ch[1]].sum + tree[node].bit;
			return node;
		}
		uint32 rotate(uint32 node, uint32 dir) {
			uint32 t = tree[node].ch[dir ^ 1];
			tree[t].color = tree[node].color;
			tree[node].ch[dir ^ 1] = tree[t].ch[dir];
			tree[node].color = 0;
			tree[t].ch[dir] = recalc(node);
			return recalc(t);
		}
		void insert(bool bit, uint32 pos) {
			border += !bit;
			data = bit;
			tree.push_back({ 1,bit,0,0,0,bit });
			post = tree.size() - 1;
			root = subins(root, pos);
			tree[root].color = 1;
			return;
		}
		void insert_cons(bool bit, uint32 pos) {
			border += !bit;
			data = bit;
			tree[post].size = 1;
			tree[post].sum = bit;
			tree[post].ch[0] = 0;
			tree[post].ch[1] = 0;
			tree[post].color = 0;
			tree[post].bit = bit;
			root = subins(root, pos);
			tree[root].color = 1;
			return;
		}
		uint32 subins(uint32 node, uint32 pos) {
			if (!node) {
				balance = 1;
				return post;
			}
			++tree[node].size;
			tree[node].sum += data;
			if (tree[tree[node].ch[0]].size < pos) {
				tree[node].ch[1] = subins(tree[node].ch[1], pos - tree[tree[node].ch[0]].size - 1);
				if (balance) {
					return insadj(node, 1);
				}
			}
			else {
				tree[node].ch[0] = subins(tree[node].ch[0], pos);
				if (balance) {
					return insadj(node, 0);
				}
			}
			return node;
		}
		uint32 insadj(uint32 node, bool dir) {
			if (!tree[node].color) {
				return node;
			}
			if (tree[tree[tree[node].ch[dir]].ch[0]].color&&tree[tree[tree[node].ch[dir]].ch[1]].color) {
				balance = 0;
				return node;
			}
			if (!tree[tree[node].ch[dir ^ 1]].color) {
				tree[tree[node].ch[0]].color = 1;
				tree[tree[node].ch[1]].color = 1;
				tree[node].color = 0;
				return node;
			}
			balance = 0;
			if (!tree[tree[tree[node].ch[dir]].ch[dir ^ 1]].color) {
				tree[node].ch[dir] = rotate(tree[node].ch[dir], dir);
			}
			return rotate(node, dir ^ 1);
		}
		bool erase(uint32 pos) {
			root = subera(root, pos);
			border -= !data;
			return data;
		}
		uint32 subera(uint32 node, uint32 pos) {
			--tree[node].size;
			if (tree[tree[node].ch[0]].size < pos) {
				tree[node].ch[1] = subera(tree[node].ch[1], pos - tree[tree[node].ch[0]].size - 1);
				tree[node].sum -= data;
				if (balance) {
					return eraadj(node, 1);
				}
				return node;
			}
			if (tree[tree[node].ch[0]].size > pos) {
				tree[node].ch[0] = subera(tree[node].ch[0], pos);
				tree[node].sum -= data;
				if (balance) {
					return eraadj(node, 0);
				}
				return node;
			}
			data = tree[node].bit;
			post = node;
			if (tree[node].ch[0]) {
				if (tree[node].ch[1]) {
					tree[node].ch[1] = subera(tree[node].ch[1], 0);
					std::swap(data, tree[node].bit);
					tree[node].sum -= data;
					if (balance) {
						return eraadj(node, 1);
					}
					return node;
				}
				tree[tree[node].ch[0]].color = 1;
				balance = 0;
				return tree[node].ch[0];
			}
			if (tree[node].ch[1]) {
				tree[tree[node].ch[1]].color = 1;
				balance = 0;
				return tree[node].ch[1];
			}
			balance = tree[node].color;
			return 0;
		}
		uint32 eraadj(uint32 node, bool dir) {
			if (tree[tree[tree[node].ch[dir ^ 1]].ch[0]].color&&tree[tree[tree[node].ch[dir ^ 1]].ch[1]].color) {
				if (tree[tree[node].ch[dir ^ 1]].color) {
					tree[tree[node].ch[dir ^ 1]].color = 0;
					balance = tree[node].color;
					tree[node].color = 1;
					return node;
				}
				node = rotate(node, dir);
				tree[node].ch[dir] = eraadj(tree[node].ch[dir], dir);
				return node;
			}
			balance = 0;
			if (tree[tree[tree[node].ch[dir ^ 1]].ch[dir ^ 1]].color) {
				tree[node].ch[dir ^ 1] = rotate(tree[node].ch[dir ^ 1], dir ^ 1);
			}
			node = rotate(node, dir);
			tree[tree[node].ch[0]].color = 1;
			tree[tree[node].ch[1]].color = 1;
			return node;
		}
		uint32 rank(uint32 pos) {
			uint32 ret = 0, node = root;
			while (node) {
				if (tree[tree[node].ch[0]].size < pos) {
					pos -= tree[tree[node].ch[0]].size + 1;
					ret += tree[tree[node].ch[0]].sum + tree[node].bit;
					node = tree[node].ch[1];
				}
				else {
					node = tree[node].ch[0];
				}
			}
			return ret;
		}
		bool access(uint32 pos) {
			uint32 node = root;
			while (tree[tree[node].ch[0]].size != pos) {
				if (tree[tree[node].ch[0]].size < pos) {
					pos -= tree[tree[node].ch[0]].size + 1;
					node = tree[node].ch[1];
				}
				else {
					node = tree[node].ch[0];
				}
			}
			return tree[node].bit;
		}
		void show(void) {
			show(root);
			std::cout << "\n";
			return;
		}
		void show(uint32 node) {
			std::cout << "(";
			if (tree[node].ch[0]) {
				show(tree[node].ch[0]);
				std::cout << "←";
			}
			std::cout << tree[node].bit << " " << (tree[node].color ? "黒" : "赤") << " " << tree[node].sum;
			if (tree[node].ch[1]) {
				std::cout << "→";
				show(tree[node].ch[1]);
			}
			std::cout << ")";
			return;
		}
	};
	uint32 length;
	std::vector<dynamic_FID> tree;
#define bitsize std::numeric_limits<T>::digits
public:
	dynamic_wavelet_matrix() {
		tree = std::vector<dynamic_FID>(bitsize);
	}
	void insert(uint32 position, const T &data) {
		++length;
		for (uint32 i = bitsize - 1;~i;--i) {
			tree[i].insert(data >> i & 1, position);
			if (data >> i & 1) {
				position = tree[i].border + tree[i].rank(position);
			}
			else {
				position -= tree[i].rank(position);
			}
		}
		return;
	}
	void erase(uint32 position) {
		--length;
		for (uint32 i = bitsize - 1;~i;--i) {
			if (tree[i].erase(position)) {
				position = tree[i].border + tree[i].rank(position);
			}
			else {
				position -= tree[i].rank(position);
			}
		}
		return;
	}
	void update(uint32 position, const T &data) {
		erase(position);
		for (uint32 i = bitsize - 1;~i;--i) {
			tree[i].insert_cons(data >> i & 1, position);
			if (data >> i & 1) {
				position = tree[i].border + tree[i].rank(position);
			}
			else {
				position -= tree[i].rank(position);
			}
		}
		return;
	}
	T min(uint32 begin, uint32 end) {
		T ret = 0;
		uint32 a, b;
		for (uint32 i = bitsize - 1;~i;--i) {
			a = tree[i].rank(begin);
			b = tree[i].rank(end);
			if (b + begin == a + end) {
				ret |= (T)1 << i;
				begin = a + tree[i].border;
				end = b + tree[i].border;
			}
			else {
				begin -= a;
				end -= b;
			}
		}
		return ret;
	}
	T access(uint32 position) {
		T ret = 0;
		for (uint32 i = bitsize - 1;~i;--i) {
			if (tree[i].access(position)) {
				ret |= (T)1 << i;
				position = tree[i].border + tree[i].rank(position);
			}
			else {
				position -= tree[i].rank(position);
			}
		}
		return ret;
	}
	T rank(uint32 begin, uint32 end, const T &data) {
		for (uint32 i = bitsize - 1;~i;--i) {
			if (data >> i & 1) {
				begin = tree[i].border + tree[i].rank(begin);
				end = tree[i].border + tree[i].rank(end);
			}
			else {
				begin -= tree[i].rank(begin);
				end -= tree[i].rank(end);
			}
		}
		return end - begin;
	}
	void show(void) {
		for (uint32 i = 0;i < length;++i) {
			std::cout << access(i) << " ";
		}
		std::cout << "\n";
		return;
	}
	void showbit() {
		for (uint32 j = 5;~j;--j) {
			for (uint32 i = 0;i < length;++i) {
				std::cout << tree[j].access(i) << " ";
			}
			std::cout << "bb:" << tree[j].border;
			std::cout << "\n";
		}
		return;
	}
};

int main(void) {
	dynamic_wavelet_matrix<uint32> T;
	uint32 n, q, c, x, y;
	scanf("%d %d", &n, &q);
	do {
		T.insert(0, ((uint32)1 << 31) - 1);
	} while (--n);
	do {
		scanf("%d %d %d", &c, &x, &y);
		if (c) {
			printf("%u", T.min(x, y + 1));
			puts("\n");
		}
		else {
			T.update(x, y);
		}
	} while (--q);
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:52:15: error: 'scanf_s' was not declared in this scope; did you mean 'scanf'?
   52 | #define scanf scanf_s
      |               ^~~~~~~
a.cc:377:9: note: in expansion of macro 'scanf'
  377 |         scanf("%d %d", &n, &q);
      |         ^~~~~