Pagini recente » Cod sursa (job #437216) | Cod sursa (job #2263361) | Cod sursa (job #2605933) | Cod sursa (job #1502931) | Cod sursa (job #2890803)
#include <fstream>
#include <stack>
using namespace std;
ifstream fin ("datorii.in");
ofstream fout("datorii.out");
int n, m, a[15001], v[60001], res = 0;
// nod st dr
typedef pair<int, pair<int,int>> Entry;
void init(int nod, int st, int dr)
{
stack<pair<Entry, int>> stiva;
// nod+st+dr, STATE
// STATE == 0 am fost aici si m-am dus in stanga
// STATE == 1 am fost aici si m-am dus in dreapta
while (!stiva.empty() || nod != -1) {
if (nod == -1) {
auto &p = stiva.top();
auto e = p.first;
nod = e.first;
st = e.second.first;
dr = e.second.second;
if (p.second == 0) {
int mid = (st+dr) / 2;
nod = 2*nod+1;
st = mid+1;
p.second = 1;
}
else {
stiva.pop();
v[nod] = v[2*nod] + v[2*nod+1];
nod = -1;
}
}
else {
if(st == dr) {
v[nod] = a[st];
nod = -1;
}
else {
int mid = (st+dr) / 2;
stiva.push({{nod, {st,dr}}, 0});
nod = 2*nod;
dr = mid;
}
}
}
}
void update(int nod, int st, int dr, int poz, int sum)
{
stack<int> stiva;
while (st != dr) {
int mid = (st+dr)/2;
if(poz <= mid) {
dr = mid;
stiva.push(nod);
nod = 2 * nod;
}
else {
st = mid+1;
stiva.push(nod);
nod = 2 * nod + 1;
}
}
v[nod] -= sum;
while (!stiva.empty()) {
int nod = stiva.top();
stiva.pop();
v[nod] = v[2*nod] + v[2*nod+1];
}
}
void query(int nod, int st, int dr, int s, int d)
{
stack<pair<int, pair<int,int>>> stiva; // {nod, {st,dr}}
while (!stiva.empty() || nod != -1)
{
if (nod == -1) {
auto p = stiva.top();
stiva.pop();
nod = p.first;
st = p.second.first;
dr = p.second.second;
}
if(st >= s && dr <= d)
{
res += v[nod];
nod = -1;
}
else {
int mid = (st+dr)/2;
if(s <= mid)
stiva.push({2*nod, {st, mid}});
if(d > mid) {
nod = 2*nod+1;
st = mid+1;
}
else {
nod = -1;
}
}
}
}
int main ()
{
fin >> n >> m;
for (int i = 1; i <= n; i++)
fin >> a[i];
init(1,1,n);
for(int i = 1; i <= m; i++)
{
int t, x, y;
fin >> t >> x >> y;
if(t)
{
query(1, 1, n, x, y);
fout << res << '\n';
res = 0;
}
else
update(1, 1, n, x, y);
}
return 0;
}