Pagini recente » simulare-cartita-29 | Cod sursa (job #1705045) | Cod sursa (job #235448) | Cod sursa (job #2109295) | Cod sursa (job #1164672)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "aib.in";
const char outfile[] = "aib.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
const int lim = (1<<20);
char buff[lim];
int pos;
inline void get(int &x) {
x = 0;
while(!('0' <= buff[pos] && buff[pos] <= '9'))
if(++ pos == lim) {
pos = 0;
fread(buff, 1, lim, stdin);
}
while('0' <= buff[pos] && buff[pos] <= '9') {
x = x * 10 + buff[pos] - '0';
if(++ pos == lim) {
pos = 0;
fread(buff, 1, lim, stdin);
}
}
}
int N, M, A[MAXN], aib[MAXN];
inline int lsb(int x) {
return x & (-x);
}
inline void Update(int pos, int value) {
for(int i = pos ; i <= N ; i += lsb(i))
aib[i] += value;
}
inline int Query(int pos) {
int sum = 0;
for(int i = pos ; i ; i -= lsb(i))
sum += aib[i];
return sum;
}
inline int binarySearch(int value) {
int li = 1, ls = N, ans = -1;
while(li <= ls) {
int mid = ((li + ls) >> 1);
int val = Query(mid);
if(val >= value) {
ls = mid - 1;
if(val == value)
ans = mid;
}
else li = mid + 1;
}
return ans;
}
int main() {
freopen(infile, "r", stdin);
get(N);
get(M);
for(int i = 1; i <= N ; ++ i) {
get(A[i]);
Update(i, A[i]);
}
for(int i = 1 ; i <= M ; ++ i) {
int op, a, b;
get(op);
switch(op) {
case 0:
get(a); get(b);
Update(a, b);
break;
case 1:
get(a); get(b);
fout << Query(b) - Query(a - 1) << '\n';
break;
case 2:
get(a);
fout << binarySearch(a) << '\n';
break;
}
}
fout.close();
return 0;
}