Pagini recente » Cod sursa (job #2399389) | Cod sursa (job #2549760) | Cod sursa (job #1263562) | Cod sursa (job #734035) | Cod sursa (job #1203004)
using namespace std;
#include <fstream>
ifstream fin ("aib.in");
ofstream fout("aib.out");
const int Nmax = 100005;
int n, log;
int v[Nmax], AIB[Nmax];
void update(int, int) ;
int sum(int) ;
int search(int) ;
int main()
{
int i, a, b, m, tip;
fin >> n >> m;
for(log = 0; (1<<log) <= n; ++log) ; --log;
for(i = 1; i <= n; ++i) {fin >> a; update(i, a);}
for(; m; --m)
{
fin >> tip;
if(tip == 0)
{fin >> a >> b; update(a, b);}
else if(tip == 1)
{
fin >> a >> b;
fout << sum(b) - sum(a-1) << '\n';
}
else
{
fin >> a;
fout << search(a) << '\n';
}
}
return 0;
}
void update(int poz, int x)
{
v[poz] += x;
for(; poz <= n; poz += (poz & -poz))
AIB[poz] += x;
}
int sum(int poz)
{
int s = 0;
while(poz) {s += AIB[poz]; poz &= poz-1;}
return s;
}
int search(int sum)
{
int lg = (1 << log), idx = 0;
while(lg && sum)
{
if(idx + lg <= n)
{
if(AIB[idx + lg] == sum) return idx + lg;
else if(AIB[idx + lg] < sum) sum -= AIB[idx], idx += lg;
}
lg >>= 1;
}
if(!sum) return -1;
return idx;
}