Pagini recente » Cod sursa (job #1289422) | Cod sursa (job #2439088) | Cod sursa (job #2252833) | Cod sursa (job #611357) | Cod sursa (job #990870)
Cod sursa(job #990870)
#include <fstream>
#include <vector>
using std::vector;
inline void increase(vector<short> &aib, unsigned idx, const short val){
unsigned bitmask=1,temp=aib.size()-1;
while(temp>>=1) bitmask<<=1;
while(idx<aib.size()){
aib[idx]+=val;
idx += idx&(-idx); //add the last significant digit to the number
}
}
inline unsigned calc(const vector<short> &aib, 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 n,m;
fin>>n>>m;
vector<short> aib(n+1,0);
unsigned temp1; int temp2; char c;
for(unsigned temp1=1;temp1<=n;++temp1){ fin>>temp2; increase(aib,temp1,temp2); }
while(m--){
fin>>c>>temp1>>temp2;
if(c=='0') increase(aib,temp1,-temp2);
else fout<<calc(aib,temp2)-calc(aib,temp1-1)<<'\n';
}
}