Pagini recente » Cod sursa (job #2065006) | Cod sursa (job #3040898) | Cod sursa (job #473976) | Cod sursa (job #773245) | Cod sursa (job #1996809)
#include <fstream>
using namespace std;
ifstream fi ("aib.in");
ofstream fo ("aib.out");
struct aib{int st,dr,val;} v[100000];
int el[100004],loc[100004];
int nr_el,nr_op,i,op;
void Generare(int x)
{
if (v[x].st==v[x].dr)
{
v[x].val=el[v[x].st];
loc[v[x].st]=x;
}
else
{
v[2*x].st=v[x].st;
v[2*x].dr=(v[x].st+v[x].dr)/2;
v[2*x+1].st=(v[x].st+v[x].dr)/2+1;
v[2*x+1].dr=v[x].dr;
Generare(2*x);
Generare(2*x+1);
v[x].val=v[2*x].val+v[2*x+1].val;
}
}
void Operatie0()
{
int poz,delta;
fi>>poz>>delta;
poz=loc[poz];
while (poz>0)
{
v[poz].val+=delta;
poz=poz/2;
}
}
int Sum(int x,int capleft,int capright)
{
int left=v[x].st;
int right=v[x].dr;
if (right<capleft or left>capright) return 0;
if (left<capleft and right>=capleft or (left<=capright and right>capright)) return Sum(2*x,capleft,capright)+Sum(2*x+1,capleft,capright);
if (left>=capleft and right<=capright) return v[x].val;
}
int Pozitie(int x,int sum)
{
if (v[x].st==0) return -1;
if (v[x].val==sum) return v[x].dr;
if (v[x].val>sum) return Pozitie(2*x,sum);
if (v[x].val<sum) return Pozitie(x+1,sum-v[2*x].val);
}
int Operatie1()
{
int capatst,capatdr;
fi>>capatst>>capatdr;
return Sum(1,capatst,capatdr);
}
int Operatie2 ()
{
int suma,target;
fi>>suma;
return Pozitie(1,suma);
}
int main()
{
fi>>nr_el>>nr_op;
for (i=1;i<=nr_el;i++) fi>>el[i];
v[1].st=1;v[1].dr=nr_el;
Generare(1);
for (i=1;i<=nr_op;i++)
{
fi>>op;
if (op==0) Operatie0();
if (op==1) fo<<Operatie1()<<'\n';
if (op==2) fo<<Operatie2()<<'\n';
}
// for (i=1;i<=15;i++) fo<<v[i].st<<' '<<v[i].dr<<' '<<v[i].val<<'\n';
return 0;
}