Pagini recente » Cod sursa (job #1949632) | Cod sursa (job #1715495) | Cod sursa (job #55643) | Cod sursa (job #2923925) | Cod sursa (job #1583672)
#include <fstream>
#define lsb(x) ((x) & (-(x)))
using namespace std;
const int NMAX = 100005;
const int QMAX = 15005;
class InputReader {
public:
InputReader() {}
InputReader(const char *file_name) {
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline InputReader &operator >>(int &n) {
while(buffer[cursor] < '0' || buffer[cursor] > '9') {
advance();
}
n = 0;
while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance() {
++ cursor;
if(cursor == SIZE) {
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
int n;
int aib[NMAX];
int query(int pos) {
int sum = 0;
for (; pos; pos -= lsb(pos))
sum += aib[pos];
return sum;
}
void update(int pos, int val = 1) {
for (; pos <= n; pos += lsb(pos))
aib[pos] += val;
}
int query(int st, int dr) {
return query(dr) - query(st - 1);
}
int v[NMAX];
int main()
{
InputReader cin("aib.in");
ofstream cout("aib.out");
int q = 0;
cin >> n >> q;
for (int i = 1; i <= n; ++ i) {
cin >> v[i];
update(i, v[i]);
}
int type, pos, v, a, b;
while (q --) {
cin >> type;
if (type == 0) {
cin >> pos >> v;
update(pos, +v);
}
else if (type == 1) {
cin >> a >> b;
cout << query(a, b) << '\n';
}
else {
cin >> v;
int st = 1;
int dr = n;
int mijl;
int ans = n + 1;
while (st <= dr) {
mijl = (st + dr) / 2;
if (query(mijl) < v)
st = mijl + 1;
else {
ans = mijl;
dr = mijl - 1;
}
}
cout << ans << '\n';
}
}
//cin.close();
cout.close();
return 0;
}