Pagini recente » Cod sursa (job #277100) | Cod sursa (job #2452678) | Cod sursa (job #839577) | Cod sursa (job #2305211) | Cod sursa (job #2230395)
#include <bits/stdc++.h>
#define MaxN 15005
#define TreeSize (int)(4 * MaxN + 50)
std::ifstream InFile("datorii.in");
std::ofstream OutFile("datorii.out");
int N, M;
int BinTree[TreeSize];
int Val[MaxN];
void Update(int Position, int Value, int TreeIndex = 1, int Left = 1, int Right = N) {
if(Left == Right) {
BinTree[TreeIndex] = Value;
return;
}
int LeftSon = 2*TreeIndex,
RightSon = 2*TreeIndex+1,
Middle = (Left+Right)/2;
if(Position <= Middle)
Update (Position, Value, LeftSon, Left, Middle);
else
Update (Position, Value, RightSon, Middle+1, Right);
BinTree[TreeIndex] = BinTree[LeftSon] + BinTree[RightSon];
}
int Query(int A, int B, int TreeIndex = 1, int Left = 1, int Right = N) {
if(A <= Left && B >= Right)
return BinTree[TreeIndex];
int LeftSon = 2*TreeIndex,
RightSon = 2*TreeIndex+1,
Middle = (Left+Right)/2;
int Sum = 0;
if(A <= Middle) Sum += Query(A, B, LeftSon, Left, Middle);
if(Middle < B) Sum += Query(A, B, RightSon, Middle+1, Right);
return Sum;
}
void Citire() {
InFile >> N >> M;
for (int i=0, x; i<N; i++) {
InFile >> x;
Val[i+1] = x;
Update(i+1, x);
}
}
void Rezolvare() {
for (int i=0, QueryType, x, y; i<M; i++) {
InFile >> QueryType;
InFile >> x >> y;
if(QueryType == 1)
OutFile << Query(x, y) << '\n';
else {
Val[x] -= y;
Update(x, Val[x]);
}
}
}
int main()
{
Citire();
Rezolvare();
return 0;
}