Pagini recente » Cod sursa (job #3229018) | Cod sursa (job #308442) | Cod sursa (job #1499013) | Cod sursa (job #2664406) | Cod sursa (job #990873)
Cod sursa(job #990873)
#include <fstream>
short aib[15001];
unsigned N;
inline void increase(unsigned idx, const short val){
while(idx<=N){
aib[idx]+=val;
idx += idx&(-idx); //add the last significant digit to the number
}
}
inline unsigned calc(unsigned idx){
unsigned Sum=0;
while(idx>0){
Sum+=aib[idx];
idx&=idx-1; //unset the last significant bit
}
return Sum;
}
int main(){
std::ifstream fin("datorii.in");
std::ofstream fout("datorii.out");
unsigned M;
fin>>N>>M;
unsigned temp1; int temp2; char c;
for(unsigned temp1=1;temp1<=N;++temp1){ fin>>temp2; increase(temp1,temp2); }
while(M--){
fin>>c>>temp1>>temp2;
if(c=='0') increase(temp1,-temp2);
else fout<<calc(temp2)-calc(temp1-1)<<'\n';
}
}