Pagini recente » Cod sursa (job #2206814) | Cod sursa (job #1745197) | Cod sursa (job #1919343) | Cod sursa (job #3199225) | Cod sursa (job #2720866)
#include <iostream>
#include <fstream>
#define NMAX 100000
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n, q, aib[NMAX+10];
void update(int poz, int val)
{ while(poz <= n)
{ aib[poz] += val;
int lastBit = poz & (-poz);
poz += lastBit;
}
}
int query(int poz)
{ int ans = 0;
while(poz)
{ ans += aib[poz];
int lastBit = poz & (-poz);
poz -= lastBit;
}
return ans;
}
int binSearch(int sum)
{ int poz = 0, curr = 0;
for(int bit=16; bit>=0; bit--)
{ if(poz + (1 << bit) > n) continue;
if(curr + aib[poz+(1<<bit)] <= sum)
{ poz += (1 << bit);
curr += aib[poz];
}
}
if(curr == sum) return poz;
return -1;
}
int main()
{
fin >> n >> q;
for(int i=1; i<=n; i++)
{ int x;
fin >> x;
update(i, x);
}
while(q--)
{ int type;
fin >> type;
if(!type)
{ int a, b;
fin >> a >> b;
update(a, b);
}
else if(type == 1)
{ int a, b;
fin >> a >> b;
fout << query(b) - query(a - 1) << '\n';
}
else
{ int sum;
fin >> sum;
fout << binSearch(sum) << '\n';
}
}
return 0;
}