Pagini recente » Cod sursa (job #313059) | Cod sursa (job #2898403) | Cod sursa (job #2481312) | Cod sursa (job #1447952) | Cod sursa (job #990871)
Cod sursa(job #990871)
#include <fstream>
short aib[15001];
unsigned N;
inline void increase(unsigned idx, const short val){
unsigned bitmask=1,temp=N;
while(temp>>=1) bitmask<<=1;
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';
}
}