Pagini recente » Cod sursa (job #894433) | Cod sursa (job #1016299) | Cod sursa (job #371379) | Cod sursa (job #2186593) | Cod sursa (job #652521)
Cod sursa(job #652521)
#include <fstream>
#define LSB(x) (x&(-x))
#define aibL 100001
using namespace std;
ifstream in;
ofstream out;
int aib[aibL];
int N;
inline void update(int pos,int val)
{
for(;pos<=N;pos+=LSB(pos))
aib[pos]+=val;
}
inline int query(int pos)
{
int sum=0;
for(;pos>0;pos-=LSB(pos))
sum+=aib[pos];
return sum;
}
inline int BS(int x,int L,int R)
{
if(L<R)
{
int M=(L+R)>>1;
int sum=query(M);
if(x<sum) return BS(x,L,M-1);
else
if(x>sum) return BS(x,M+1,R);
else return M;
}
else
{
int sum=query(L);
if(sum==x) return L;
else return -1;
}
}
int main()
{
int M,x,y,o;
in.open("aib.in");
in>>N>>M;
for(int i=1;i<=N;++i)
{
in>>x;
update(i,x);
}
out.open("aib.out");
for(;M--;)
{
in>>o;
if(o!=2)
{
in>>x>>y;
if(o==0) update(x,y);
else
if(o==1) out<<query(y)-query(x-1)<<'\n';
continue;
}
in>>x;
out<<BS(x,1,N)<<'\n';
}
in.close();
out.close();
return 0;
}