Pagini recente » Profil Gabi_Laza | Cod sursa (job #767946) | Cod sursa (job #2143040) | Rating Tomas Nicoara (MansNotHot) | Cod sursa (job #2548964)
#include <bits/stdc++.h>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
const int dim = 100001;
int n, m, tree[dim], c, a, b;
inline int nr(int i)
{
return (i & (-i));
}
int suma(int i)
{
int s = 0;
while(i > 0)
{
s += tree[i];
i -= nr(i);
}
return s;
}
int add(int poz, int val)
{
while(poz <= n)
{
tree[poz] += val;
poz += nr(poz);
}
}
int cauta(int s)
{
int st = 1;
int poz = 0;
for(st;st <= n;st <<= 1);
for(poz;st;st >>= 1)
{
if(poz + st <= n)
{
if(s >= tree[poz + st])
{
poz += st;
s -= tree[poz];
if(s == 0) return poz;
}
}
}
return -1;
}
void read()
{
in>>n>>m;
int x;
for(int i = 1;i <= n;i++)
{
in>>x;
add(i, x);
}
}
void solve()
{
for(int i = 1;i <= m;i++)
{
in>>c;
if(c == 0)
{
in>>a>>b;
add(a, b);
}
if(c == 1)
{
in>>a>>b;
out<<suma(b) - suma(a - 1)<<'\n';
}
if(c == 2)
{
in>>a;
out<<cauta(a)<<'\n';
}
}
}
int main()
{
read();
solve();
return 0;
}