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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <bits/stdc++.h>

using namespace std;

typedef vector<pair<int,int>> VectorPair; 
static int valuesSize = 0;
static VectorPair values[500010];

class Node
{
public:
	int value;
	int index = -1;

	vector<Node*> nodes;

	~Node()
	{
		for(auto p: nodes)
			delete p;
	}
};


void solution(int start, int end, int step, int value, Node* root)
{
	if (step == valuesSize || start + 1 == end)
	{
		while(start != end)
		{
			Node* newN = new Node;
			newN->index = start;
			newN->value = value;

			root->nodes.push_back(newN);
			start++;
		}
	}
	else
	{
		vector<pair<int,int>>& current = values[step];

		static auto comp = [] (const pair<int,int>& p1, const pair<int,int>& p2)
			{
				return p1.first < p2.first;
			};

		auto itS = lower_bound(current.begin(), current.end(), make_pair(start,0), comp);
		if (itS == current.end() || itS->first > start)
			itS--;

		auto itE = lower_bound(itS, current.end(), make_pair(end,0), comp);


			auto itPrev = itS;
			auto iter = itS+1;
			while(iter != itE)
			{
				int prevFirst = itPrev->first;
				int prevSec = itPrev->second;
				int curFirst = iter->first;
				int newStep = step + 1;

				if (itPrev == itS)
				{
					if (prevFirst < start)
						prevFirst = start;
				}
					
				{
					Node* newN = new Node;
					newN->value = prevSec;

					root->nodes.push_back(newN);
					
					solution(prevFirst, curFirst, newStep, prevSec, newN);
				}

				itPrev++;
				iter++;
			}

		if (itPrev->first != end)
		{
			int prevFirst = itPrev->first;
			int prevSec = itPrev->second;
			int curFirst = end;
			int newStep = step + 1;

			if (itPrev == itS)
			{
				if (prevFirst < start)
					prevFirst = start;
			}


			{
				Node* newN = new Node;
				newN->value = prevSec;

				root->nodes.push_back(newN);

				solution(prevFirst, curFirst, newStep, prevSec, newN);
			}
		}

	}
}

void print(Node& node)
{
	if (node.nodes.empty())
	{
		printf("%d ", node.index + 1);
	}
	else
	{
		stable_sort(node.nodes.begin(), node.nodes.end(), 
			[](const Node* n1, const Node* n2)
			{
				return n1->value < n2->value;
			}
			);

		for (auto p: node.nodes)
		{
			assert(p);
			print(*p);
		}
	}
}

int main()
{
	ios::sync_with_stdio(0);
	vector<pair<int,int>> changes = {{1,2}, {3,9}, {0,7}, {0,1}, {1,6}, {0,3}, {2,1}, {1,1}};

	int valuesQ, changesQ;
	cin >> valuesQ >> changesQ;

	valuesSize = valuesQ;

	changes.resize(changesQ-1);

	for (int i=0; i<valuesQ; ++i)
	{
		auto& value = values[i];
		value.resize(1, {0,0});
		cin >> value.back().second;
	}

	for (auto& c: changes)
	{
		cin >> c.first >> c.second;
		c.first--;
	}

	for (int i=0; i<changes.size(); ++i)
	{
		auto& c = changes[i];
		values[c.first].push_back({i+1, c.second});
	}
	Node root;
	solution(0, changesQ, 0, 0, &root);

	print(root);
	puts("");

	return 0;
}