Pagini recente » Cod sursa (job #2300003) | Cod sursa (job #1023186) | Cod sursa (job #1372558) | Cod sursa (job #1354439) | Cod sursa (job #792278)
Cod sursa(job #792278)
using namespace std;
#include<fstream>
#include<vector>
#include<algorithm>
#define nmax 100001
#define mic unsigned short int
vector<unsigned int> aib(nmax);
vector<mic>v(nmax);
unsigned int n;
ofstream g("aib.out");
inline int zeros(mic x)
{
mic nr=0;
x=x&(-x);
while(x>1)
++nr,x>>=1;
return nr;
}
void update(mic poz,mic x)
{
while(poz<=n)
{
aib[poz]+=x;
poz+=(1<<zeros(poz));
}
}
int query(mic a)
{
unsigned int s=0;
while(a>0)
{
s+=aib[a];
a-=(1<<zeros(a));
}
return s;
}
int search(unsigned int k)
{
unsigned int S,pos = n+1, ind, limst=0, limdr=n+1;
ind = n;
S = query(ind);
if ( S == k )
pos = n;
while ( ind )
{
ind = (limst+limdr)>>1;
S = query(ind);
if ( S > k )
{
if ( limdr > ind ) limdr = ind;
ind -= 1;
}
else if ( S == k ) pos = min(pos,ind), limdr = ind,ind-= 1;
else
{
if ( limst < ind ) limst =ind;
ind += 1;
}
if ( ind <= limst ) break;
if ( ind >= limdr ) break;
}
if ( pos == n+1 ) return -1;
return pos;
}
int main()
{
mic i,m,op,a,b;
unsigned int k;
ifstream f("aib.in");
f>>n>>m;
for(i=1;i<=n;i++)
{
f>>v[i];
update(i,v[i]);
}
for(i=1;i<=m;i++)
{
f>>op;
if(op&1)
{
f>>a>>b;
g<<query(b)-query(a-1)<<"\n";
}
else
if(op)
{
f>>k;
g<<search(k)<<"\n";
}
else
{ f>>a>>b;
update(a,b);
}
}
f.close();
g.close();
return 0;
}