Pagini recente » Cod sursa (job #372973) | Cod sursa (job #2174877) | Cod sursa (job #346921) | Cod sursa (job #232770) | Cod sursa (job #2167090)
#include <fstream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#define pu(x) ((x^(x-1))&x)
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n,m,i,aib[100010];
void update(int poz, int val)
{
for (int i=poz; i<=n; i+=pu(i))
aib[i]+=val;
}
int query(int poz)
{
int ret=0;
for (int i=poz; i; i-=pu(i))
ret+=aib[i];
return ret;
}
int opdoi(int x)
{
if (a==0)
return -1;
int bit,sc,poz;
poz=sc=0;
bit=1;
while (bit<=n)
bit<<=1;
bit>>=1;
while (bit>0)
{
if (poz+bit<=n&&aib[poz+bit]+sc<=x)
{
sc+=aib[poz+bit];
poz+=bit;
}
bit>>=1;
}
if (sc!=x)
return -1;
return poz;
}
int main()
{
fin >> n >> m;
for (i=1; i<=n; i++)
{
int x;
fin >> x;
update(i,x);
}
for (i=1; i<=m; i++)
{
int op,x;
fin >> op >> x;
if (op==2)
fout << opdoi(x) << '\n';
else
{
int y;
fin >> y;
if (op)
fout << query(y)-query(x-1) << '\n';
else
update(x,y);
}
}
}