Pagini recente » Cod sursa (job #49918) | Cod sursa (job #1040581) | Cod sursa (job #1526861) | Cod sursa (job #2878005) | Cod sursa (job #1087049)
#include<cstdio>
using namespace std;
const int MAXN=100005;
int n,m;
int arb[MAXN];
int sum(int i)
{
int suma=0;
while (i>0)
{
suma+=arb[i];
i-=(i&-i);
}
return suma;
}
void update(int pos, int val)
{
while (pos<=n && pos)
{
arb[pos]+=val;
pos+=(pos& -pos);
}
}
int query(int a, int b)
{
return sum(b)-sum(a-1);
}
int binary_search(int suma)
{
int pos=-1,left=1,right=n,mid=0;
while (left<=right)
{
mid=(left+right)/2;
if (sum(mid)==suma)
{
pos=mid;
while (suma==sum(pos-1))
{
--pos;
}
break;
}
else if (suma<sum(mid))
{
right=mid-1;
}
else if (suma>sum(mid))
{
left=mid+1;
}
}
return pos;
}
int main()
{
int i,x,opt,a,b;
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d%d",&n,&m);
for (i=1; i<=n; ++i)
{
scanf("%d",&x);
update(i,x);
}
for (i=1; i<=m; ++i)
{
scanf("%d%d",&opt,&a);
if (!opt)
{
scanf("%d",&b);
update(a,b);
}
else if (opt==1)
{
scanf("%d",&b);
printf("%d\n",query(a,b));
}
else if (opt==2)
{
printf("%d\n",binary_search(a));
}
}
return 0;
}