Pagini recente » Cod sursa (job #928400) | Cod sursa (job #2918315) | Cod sursa (job #1631000) | Cod sursa (job #259383) | Cod sursa (job #214287)
Cod sursa(job #214287)
#include <fstream>
using namespace std;
long a[100002],n,m;
int k(int x)
{
int z=0;
while(!(x&1))
{
x>>=1;
z++;
}
return z;
}
void aduna(long i,int x)
{
while(i<=n)
{
a[i]+=x;
i+=(1<<k(i));
}
}
long suma(long poz)
{
long s=0;
while(poz)
{
s+=a[poz];
poz-=(1<<k(poz));
}
return s;
}
int secventa(int val)
{
int i,poz;
for(poz=1;poz<n;poz<<=1);
for(i=0;poz;poz>>=1)
{
if(i+poz<=n)
if(val>=a[i+poz] )
{
i+=poz;
val-= a[i];
if (!val) return i;
}
}
return -1;
}
void citire()
{
memset(a,0,sizeof(a));
ifstream f("aib.in");
ofstream g("aib.out");
f>>n>>m;
for(int i=1;i<=n;i++)
{
int x;
f>>x;
aduna(i,x);
}
for(int i=1;i<=m;i++)
{
int x;
f>>x;
if(x==0)
{
int a,b;
f>>a>>b;
aduna(a,b);
}
if(x==1)
{
int a,b;
f>>a>>b;
long s=suma(b)-suma(a-1);
g<<s<<endl;
}
if(x==2)
{
int a;
f>>a;
g<<secventa(a)<<endl;
}
}
}
int main()
{
citire();
return 0;
}