Pagini recente » Cod sursa (job #1889253) | Cod sursa (job #2105007) | Cod sursa (job #1306312) | Cod sursa (job #574121) | Cod sursa (job #1815483)
#include <bits/stdc++.h>
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");
vector<int> v;
int n, m;
void read()
{
f >> n >> m; v.push_back(1234);
for(int i=0, x; i<n; ++i)
{
f >> x; v.push_back(x);
}
}
void update(int poz, const int val, vector<int> &buf){
poz+=n;
buf[poz]-=val;
for(poz /= 2; poz; poz /= 2)
{
buf[poz] = buf[2*poz] + buf[2*poz+1];
}
}
int query(int st, int dr, vector<int> &buf)
{
int rez=0;
st+=n;
for(dr += n+1; st < dr; st /= 2, dr /= 2)
{
if(st%2) rez = rez + buf[st++];
if(dr%2) rez = rez + buf[--dr];
}
return rez;
}
void out()
{
vector <int> buf(2*n+2);
copy(v.begin()+1,v.end(), buf.begin()+n+1); buf[2*n+1]=0;
for(int i = n; i > 0; --i)
{
buf[i] = buf[2*i] + buf[2*i+1];
}
for(int t, a=0, b=0; m--; )
{
f >> t >> a >> b;
if(t == 1) g << query(a, b, buf) << '\n';
else update(a, b, buf);
}
f.close();
g.close();
}
int main()
{
read();
out();
return 0;
}