-
Notifications
You must be signed in to change notification settings - Fork 2
/
1041.cpp
66 lines (58 loc) · 1.45 KB
/
1041.cpp
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
#include <iostream>
#include <algorithm>
#define ERRORNUM 0x7fffffff
using namespace std;
const int SIZE = 100010;
int plane[SIZE];
pair<int, int> heap[SIZE];
int N;
void MinHeap(int pos) {
pair<int, int> tmp;
for (; pos > 1 && heap[pos >> 1] > heap[pos]; pos >>= 1) {
tmp = heap[pos >> 1];
heap[pos >> 1] = heap[pos];
heap[pos] = tmp;
}
}
void Maintain(int planeIndex, int decrease_height) {
int pos = plane[planeIndex];
pair<int, int> tmp;
heap[pos].first -= decrease_height;
for (; pos > 1 && heap[pos >> 1] > heap[pos]; pos >>= 1) {
tmp = heap[pos >> 1];
heap[pos >> 1] = heap[pos];
heap[pos] = tmp;
plane[heap[pos].second] = pos;
plane[heap[pos >> 1].second] = pos >> 1;
}
}
int FindMin(int min_height, int root_pos = 1) {
if (root_pos > N) return ERRORNUM;
if (heap[root_pos].first > min_height) return heap[root_pos].second;
return min(FindMin(min_height, root_pos << 1), FindMin(min_height, root_pos << 1 | 1));
}
int main() {
ios::sync_with_stdio(false);
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> heap[i].first;
heap[i].second = i;
MinHeap(i);
}
for (int i = 1; i <= N; ++i) plane[heap[i].second] = i;
char op[10];
while (cin >> op) {
if (op[0] == 'd') {
int planeIndex, decrease_height;
cin >> planeIndex >> decrease_height;
Maintain(planeIndex, decrease_height);
}
else {
int min_height;
cin >> min_height;
int planeIndex = FindMin(min_height);
cout << planeIndex << '\n';
}
}
return 0;
}