Pagini recente » Cod sursa (job #1884918) | Cod sursa (job #3254366) | Cod sursa (job #1424108) | Cod sursa (job #1546531) | Cod sursa (job #1121601)
//Include
#include <fstream>
using namespace std;
//Constante
const int sz = (int)1e5+1;
//Functii
void update(int pos, int val);
int query(int pos);
int search(int val);
//Variabile
int num, type, questions, elements;
int tree[sz];
ifstream in("aib.in");
ofstream out("aib.out");
//Main
int main()
{
in >> num >> questions;
int val;
for(int i=1 ; i<=num; ++i)
{
in >> val;
update(i, val);
}
while(questions--)
{
int type, rLeft, rRight, value, position;
in >> type;
if(type)
{
if(type==1)
{
in >> rLeft >> rRight;
out << query(rRight) - query(rLeft-1) << '\n';
}
else
{
in >> value;
out << search(value) << '\n';
}
}
else
{
in >> position >> value;
update(position,value);
}
}
in.close();
out.close();
return 0;
}
void update(int pos, int val)
{
for(; pos <=num; pos += pos^(pos&(pos-1)))
tree[pos] += val;
}
int query(int pos)
{
int sum = 0;
for(; pos; pos&=pos-1)
sum += tree[pos];
return sum;
}
int search(int val)
{
int left = 1, right = num;
while(left <= right)
{
int mid = (left+right) >>1;
int sum = query(mid);
if(sum == val)
return mid;
if(val < sum)
right = mid -1 ;
else left = mid + 1;
}
return -1;
}