-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
99 lines (78 loc) · 2.3 KB
/
solution.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
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
#include <algorithm>
#include <iostream>
#include <cassert>
template <class T>
class SegmentTree {
private:
size_t size;
T* tree;
int get_first(int node, size_t left, size_t right, size_t l, size_t r, double x) {
if (r < left || right < l)
return -1;
if (l <= left && right <= r) {
if (tree[node] < x)
return -1;
while (left != right) {
int mid = left + (right - left) / 2;
if (x <= tree[2 * node]) {
node = 2 * node;
right = mid;
} else {
node = 2 * node + 1;
left = mid + 1;
}
}
return left;
}
int mid = left + (right - left) / 2;
int result = get_first(2 * node, left, mid, l, r, x);
if (result != -1)
return result;
return get_first(2 * node + 1, mid + 1, right, l, r, x);
}
public:
SegmentTree(size_t size) : size(size) {
tree = new T[4 * size];
}
SegmentTree(T* array, size_t size) : size(size) {
tree = new T[4 * size];
build(array, 1, 0, size - 1);
}
~SegmentTree() {
delete[] tree;
}
void build(T* array, size_t node, size_t left, size_t right) {
if (left == right) {
tree[node] = array[left];
} else {
size_t mid = left + (right - left) / 2;
build(array, 2 * node, left, mid);
build(array, 2 * node + 1, mid + 1, right);
tree[node] = std::max({tree[2 * node], tree[2 * node + 1]});
}
}
int get_first(int l, int r, double x) {
return get_first(1, 0, size - 1, l, r, x);
}
};
int main() {
int n, q;
scanf("%d%d", &n, &q);
assert(2 <= n && n <= 1e6);
assert(2 <= q && q <= 1e6);
int* array = new int[n];
for (int i = 0; i < n; i++)
scanf("%d", array + i);
SegmentTree<int> tree(array, n);
for (int i = 0; i < q; i++) {
int x, a;
scanf("%d%d", &x, &a);
double target = array[x] * (100 + a) / 100.;
int position = tree.get_first(x, n - 1, target);
if (position != -1)
printf("%d\n", position - x);
else
printf("-1\n");
}
return 0;
}