Pagini recente » Cod sursa (job #2695289) | Cod sursa (job #703515) | Cod sursa (job #2291163) | Cod sursa (job #3132551) | Cod sursa (job #2331478)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int a[100001],n,m;
int op,r,b,x;
void op0(int i,int x)
{
while(i<=n){
a[i]+=x;
i=i+(i&(-i));
}
}
int suma(int i){
int sol=0;
while(i>0){
sol+=a[i];
i=i-(i&(-i));
}
return sol;
}
int op1(int val) {
int l = 1, r = n;
while (l <= r) {
int m = (l + r) / 2;
int sum = suma(m);
if (sum == val)
return m;
else if (sum < val)
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main()
{
f>>n>>m;
for(int i=1;i<=n;i++){f>>x; op0(i,x);}
for(int i=1;i<=m;i++){
f>>op;
if(op==0){
f>>r>>b; op0(r,b);
}
if(op==2)
{f>>r; g<<op1(r)<<endl;}
if(op==1) {f>>r>>b; g<<suma(b)-suma(r-1)<<endl;}
}
return 0;
}