1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

int main() {
	std::ios::sync_with_stdio(false);
	int n, m;
	std::cin >> n >> m;
	std::vector<int> t(n);
	std::vector<std::pair<int, int>> px(m);
	std::vector<std::map<int, int>> domain(n);
	for (int i = 0; i < n; i++) {
		int value;
		std::cin >> value;
		t[i] = value;
		domain[i].emplace(value, 0);
	}
	for (int i = 1; i < m; i++) {
		int p, x;
		std::cin >> p >> x;
		p--;
		px[i] = {p, x};
		domain[p].emplace(x, 0);
	}
	std::vector<std::pair<int, int>> location(n);
	int cur = 0;
	for (int i = 0; i < n; i++) {
		int prev = cur;
		int j = 0;
		for (auto& pr : domain[i]) {
			pr.second = j++;
		}
		for (j--; j; j >>= 1) {
			cur++;
		}
		int add = ((cur - 1) & ~31) - prev;
		if (add > 0) {
			cur += add;
			prev += add;
		}
		location[i] = {prev, cur - prev};
	}
	std::vector<std::pair<std::vector<unsigned>, int>> bitfields(m);
	auto& firstBitfield = bitfields.front();
	firstBitfield.first.resize((cur + 31) >> 5);
	firstBitfield.second = 0;
	for (int i = 0; i < n; i++) {
		unsigned length = location[i].second;
		if (length) {
			unsigned position = location[i].first;
			unsigned value = domain[i][t[i]];
			firstBitfield.first[position >> 5] |= value << (32 - length - (position & 31));
		}
	}
	for (int i = 1; i < m; i++) {
		auto& bitfield = bitfields[i];
		bitfield.first = bitfields[i - 1].first;
		bitfield.second = i;
		unsigned index = px[i].first;
		unsigned length = location[index].second;
		if (length) {
			unsigned position = location[index].first;
			unsigned value = domain[index][px[i].second];
			bitfield.first[position >> 5] &= ~(((1 << length) - 1) << (32 - length - (position & 31)));
			bitfield.first[position >> 5] |= value << (32 - length - (position & 31));
		}
	}
	std::sort(bitfields.begin(), bitfields.end());
	for (const auto& b : bitfields) {
		std::cout << (b.second + 1) << ' ';
	}
	std::cout << '\n';
	return 0;
}