Pagini recente » Cod sursa (job #1051929) | Cod sursa (job #1798838) | Cod sursa (job #1766086) | Cod sursa (job #1131925) | Cod sursa (job #2362300)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n,m,x,y,c,inc;
int aib[100005];
void add(int pos,int val)
{
while(pos<=n)
{
aib[pos]+=val;
pos+=pos&(-pos);
}
}
int sum(int where)
{
int s=0;
while(where>0)
{
s+=aib[where];
where-=where&(-where);
}
return s;
}
int find_sum(int limit)
{
int i,where=inc;
for(i=0;where>0 && limit>0;where>>=1)
{
if(i+where>n || aib[i+where]>limit)continue;
i+=where;
limit-=aib[i];
}
if(!limit)return i;
return -1;
}
int main()
{
fin>>n>>m;
for(int i=1;i<=n;i++)
{
fin>>x;
add(i,x);
}
for(inc=1;inc<n;inc<<=1);
for(int i=1;i<=m;i++)
{
fin>>c;
if(c==0)
{
fin>>x>>y;
add(x,y);
continue;
}
if(c==1)
{
fin>>x>>y;
fout<<sum(y)-sum(x-1)<<"\n";
continue;
}
fin>>x;
fout<<find_sum(x)<<"\n";
}
return 0;
}