include cstdlib include iostream template typename only_t int max cons

  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
#include <cstdlib>
#include <iostream>
template <typename only_t>
int max(const only_t& a){
return a;
}
template <typename head_t, typename... tail_t>
int max(const head_t& a, const tail_t&... b){
auto c = max(b...);
return (a > c)?(a):(c);
}
template <typename only_t>
int min(const only_t& a){
return a;
}
template <typename head_t, typename... tail_t>
int min(const head_t& a, const tail_t&... b){
auto c = min(b...);
return (a < c)?(a):(c);
}
int is_zero(int num){
return (num == 0)?(1):(0);
}
template <typename T>
class FenwickTree_t {
private:
T* tree = nullptr;
T* array = nullptr;
const int sz = 0;
public:
FenwickTree_t(T* array, int n): array(array), sz(n){
tree = new T[n]();
tree[0] = is_zero(array[0]);
for (int i = 1; i < n; ++i){
tree[i] = tree[i - 1] + is_zero(array[i]);
}
int j = 0;
for (int i = n - 1; i > 0; --i){
j = (i & (i + 1)) - 1;
if (j >= 0){
tree[i] -= tree[j];
}
}
}
void add(int elem, T val){
for (int i = elem; i < sz; i = i | (i + 1)){
tree[i] += val;
}
}
int count_zeroes(int elem){
int s = 0;
for (int i = elem; i >= 0; i = (i & (i + 1)) - 1){
s += tree[i];
}
return s;
}
T count_zeroes(int l, int r){
return count_zeroes(r) - count_zeroes(l - 1);
}
void update(int elem_i, int new_val){
int old_val = array[elem_i];
array[elem_i] = new_val;
if (is_zero(new_val) == is_zero(old_val)) return;
int d = (is_zero(new_val)?(1):(-1));
for (int i = elem_i; i < sz; i = i | (i + 1)){
tree[i] += d;
}
}
int find_kth_zero(int k, int l_bound, int r_bound){
int start = l_bound;
int middle = 0;
int count = 0;
if (k > count_zeroes(l_bound, r_bound)){
return -2;
}
while (l_bound != r_bound){
middle = (r_bound + l_bound) >> 1;
count = count_zeroes(l_bound, middle);
if (k <= count){
r_bound = middle;
}
else {
l_bound = middle + 1;
k -= count;
}
}
return (l_bound);
}
};
int main(void){
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n = 0;
std::cin >> n;
int* data = new int[n]();
for (int i = 0; i < n; ++i){
std::cin >> data[i];
};
auto t = FenwickTree_t<int>(data, n);
int m = 0;
std::cin >> m;
char c;
int elem_i, new_val;
int l_bound, r_bound, k;
for (int i = 0; i < m; ++i){
std::cin >> c;
if (c == 'u'){
std::cin >> elem_i;
std::cin >> new_val;
t.update(elem_i - 1, new_val);
}
else if (c == 's'){
std::cin >> l_bound;
std::cin >> r_bound;
std::cin >> k;
std::cout << t.find_kth_zero(k, l_bound - 1, r_bound - 1) + 1 << " ";
}
}
return 0;
}
//