Pagini recente » Cod sursa (job #4730) | Cod sursa (job #2069365) | Cod sursa (job #2493408) | Cod sursa (job #581917) | Cod sursa (job #3003869)
#include <fstream>
#define dim 100000
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int a[4 * dim + 5], indice[dim + 5], n, m, v[dim + 5];
int x, y, a1, a2;
void build(int st, int dr, int poz);
int query(int st, int dr, int poz);
void update(int poz);
int main()
{
int i, caz;
fin >> n >> m;
for (i = 1; i <= n; i++)
fin >> v[i];
build(1, n, 1);
for (i = 1; i <= m; i++) {
fin >> caz >> x >> y;
if (caz == 0)
query(1, n, 1);
else {
a[indice[x]] = y;
update(indice[i] / 2);
}
}
return 0;
}
void build(int st, int dr, int poz)
{
if (st == dr) {
a[poz] = v[st];
indice[v[st]] = poz;
return;
}
else {
int mij = (st + dr) / 2;
build(st, mij, 2 * poz);
build(mij + 1, dr, 2 * poz + 1);
a[poz] = a[2 * poz] + a[2 * poz + 1];
}
}
int query(int st, int dr, int poz)
{
if (x <= st && y >= dr)
return a[poz];
int mij = (st + dr) / 2;
if (poz <= mij)
a1 = query(st, mij, 2 * poz);
else
a2 = query(mij + 1, dr, 2 * poz + 1);
return a1 + a2;
}
void update(int poz)
{
if (poz) {
a[poz] = a[2 * poz] + a[2 * poz + 1];
update(poz / 2);
}
}