Pagini recente » Cod sursa (job #1019498) | Cod sursa (job #2490788) | Cod sursa (job #3285900) | Cod sursa (job #1781125) | Cod sursa (job #1203018)
using namespace std;
#include <fstream>
ifstream fin ("aib.in");
ofstream fout("aib.out");
const int Nmax = 100005;
int n, log;
int 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) ;
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)
{
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) idx += lg, sum -= AIB[idx];
}
lg >>= 1;
}
return -1;
}