-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathINSERTION.cpp
More file actions
185 lines (161 loc) · 3.03 KB
/
Copy pathINSERTION.cpp
File metadata and controls
185 lines (161 loc) · 3.03 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <algorithm>
using namespace std;
template<typename KeyType>
class treap
{
public:
struct Node
{
KeyType key;
int priority, size;
Node *left, *right;
Node(const KeyType& _key) : key(_key), priority(rand()), size(1), left(NULL), right(NULL) {}
void setLeft(Node *newLeft)
{
left = newLeft;
calcSize();
}
void setRight(Node *newRight)
{
right = newRight;
calcSize();
}
void calcSize()
{
size = 1;
if(left)
size += left->size;
if(right)
size += right->size;
}
};
typedef pair<Node*,Node*> NodePair;
treap() : root(NULL) {}
NodePair split(Node *root, KeyType key)
{
if(root == NULL)
return NodePair(NULL, NULL);
if(root->key < key)
{
NodePair rs = split(root->right, key);
root->setRight(rs.first);
return NodePair(root, rs.second);
}
NodePair ls = split(root->left, key);
root->setLeft(ls.second);
return NodePair(ls.first, root);
}
Node *insert(Node *root, Node *node)
{
if(root == NULL)
return node;
if(root->priority < node->priority)
{
NodePair splitted = split(root, node->key);
node->setLeft(splitted.first);
node->setRight(splitted.second);
return node;
}
else if(node->key < root->key)
root->setLeft(insert(root->left, node));
else
root->setRight(insert(root->right, node));
return root;
}
void insert(KeyType key)
{
Node *node = new Node(key);
root = insert(root, node);
}
Node *merge(Node *a, Node *b)
{
if(a == NULL)
return b;
if(b == NULL)
return a;
if(a->priority < b->priority)
{
b->setLeft(merge(a, b->left));
return b;
}
a->setRight(merge(a->right, b));
return a;
}
Node *erase(Node *root, KeyType key)
{
if(root == NULL)
return root;
if(root->key == key)
{
Node *ret = merge(root->left, root->right);
delete root;
return ret;
}
if(key < root->key)
root->setLeft(erase(root->left, key));
else
root->setRight(erase(root->right, key));
return root;
}
void erase(KeyType key)
{
root = erase(root, key);
}
Node *kth(Node *root, int k)
{
int leftSize = 0;
if(root->left != NULL)
leftSize = root->left->size;
if(k <= leftSize)
return kth(root->left, k);
if(k == leftSize + 1)
return root;
return kth(root->right, k - leftSize - 1);
}
KeyType kth(int k)
{
return kth(root, k)->key;
}
int countLessThan(Node *root, KeyType key)
{
if(root == NULL)
return 0;
if(root->key >= key)
return countLessThan(root->left, key);
int ls = (root->left? root->left->size : 0);
return ls + 1 + countLessThan(root->right, key);
}
int countLessThan(KeyType key)
{
return countLessThan(root, key);
}
private:
Node *root;
};
int main()
{
int T;
cin >> T;
while(T--)
{
int N;
cin >> N;
vector<int> a(N);
for(int i=0; i < N; i++)
cin >> a[i];
treap<int> tr;
for(int i=1; i <= N; i++)
tr.insert(i);
vector<int> ans(N);
for(int i=N-1; i >= 0; i--)
{
ans[i] = tr.kth(i - a[i] + 1);
tr.erase(ans[i]);
}
for(int i=0; i < N; i++)
cout << ans[i] << " ";
cout << '\n';
}
return 0;
}