Pagini recente » Cod sursa (job #2988837) | Cod sursa (job #2819316) | Cod sursa (job #2959730) | Cod sursa (job #3232224) | Cod sursa (job #891393)
Cod sursa(job #891393)
#include <fstream>
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");
int n,m,v[15005];
int p2[]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384};
void update(int i, int x)
{
int bn=0;
while(i<=n)
{
//if(i==3) g<<"Modific 3 cu "<<x<<"\n\n\n";
v[i]+=x;
while(i&p2[bn]==0) ++bn;
i+=p2[bn];
++bn;
}
}
int query(int a, int b)
{
int bn=0,s1=0,s2=0;
while(b>0)
{
s2+=v[b];
while(b&p2[bn]==0) ++bn;
b-=p2[bn];
++bn;
}
bn=0; a--;
while(a>0)
{
s1+=v[a];
while(a&p2[bn]==0) ++bn;
a-=p2[bn];
++bn;
}
return s2-s1;
}
int main()
{
int i,o,a,b,x;
f>>n>>m;
//g<<(2&1)<<'\n';
for(i=1;i<=n;++i)
{
f>>x;
update(i,x);
}
for(;m;--m)
{
f>>o>>a>>b;
//for(i=1;i<=n;++i) g<<v[i]<<' ';
//g<<'\n';
if(o==0) update(a,-b);
else g<<query(a,b)<<'\n';
}
f.close(); g.close();
return 0;
}