Pagini recente » Cod sursa (job #2658917) | Cod sursa (job #1527971) | Cod sursa (job #2039815) | Cod sursa (job #2185171) | Cod sursa (job #598645)
Cod sursa(job #598645)
#include <fstream>
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int n,m,i,c,x,y,a[210005],k=1;
void modifica(int poz, int val)
{
while (poz<=k)
a[poz]+=val,
poz+=(poz ^ (poz & (poz-1)));
}
int suma(int poz)
{
int t=0;
while (poz>0)
t+=a[poz],
poz-=(poz ^ (poz & (poz-1)));
return t;
}
int pozitia(int sum)
{
int poz=0,t=1,s=0;
while (s<sum && poz<n)
{
while (poz+t+(t ^ (t & (t-1)))<=k && s+a[poz+t+(t ^ (t & (t-1)))]<=sum)
t+=(t ^ (t & (t-1)));
poz+=t;
s+=a[poz];
t=1;
}
if (s==sum) return poz; else return -1;
}
int main()
{
f >> n >> m;
while (k<n) k*=2;
for (i=1; i<=n; i++)
{
f >> c;
modifica(i,c);
}
for (i=0; i<m; i++)
{
f >> c;
if (c==0) f >> x >> y, modifica(x,y); else
if (c==1) f >> x >> y, g << suma(y)-suma(x-1) << "\n"; else
f >> x, g << pozitia(x) << "\n";
}
return 0;
}