Pagini recente » Cod sursa (job #987207) | Cod sursa (job #2033634) | Cod sursa (job #2226812) | Cod sursa (job #73645) | Cod sursa (job #1684151)
#include <cstdio>
using namespace std;
const int NMAX = 100000;
int n;
int aib[NMAX+5];
inline int lsb(int x)
{
return x&-x;
}
void update(int pos, int val)
{
for(int i=pos; i<=n; i+=lsb(i))
aib[i]+=val;
}
int query(int pos)
{
int ans = 0;
for(int i=pos; i>0; i-=lsb(i))
ans+=aib[i];
return ans;
}
int bs(int x)
{
int ans = 0;
int CurrentSum = 0;
for(int i=1<<15; i>0; i>>=1)
if(ans+i<=n && CurrentSum+aib[ans+i]<=x)
{
ans+=i;
CurrentSum+=aib[ans];
}
if(CurrentSum!=x || ans == 0)return -1;
return ans;
}
int main()
{
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
int m;
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++)
{
int x;
scanf("%d", &x);
update(i, x);
}
for(int i=0; i<m; i++)
{
int type;
scanf("%d", &type);
if(type == 0)
{
int pos, val;
scanf("%d%d", &pos, &val);
update(pos, val);
}
else if(type == 1)
{
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", query(y)-query(x-1));
}
else if(type == 2)
{
int x;
scanf("%d", &x);
printf("%d\n", bs(x));
}
}
return 0;
}