Pagini recente » Cod sursa (job #2737720) | Cod sursa (job #3180959) | Cod sursa (job #585208) | Cod sursa (job #704242) | Cod sursa (job #290210)
Cod sursa(job #290210)
#include <stdio.h>
#define nm 100010
int v[nm];
int n, m, i, x, y, t, tmp;
void t_update(int poz, int val)
{
while (poz <= n)
{
v[poz] += val;
poz += (poz & -poz);
}
}
int t_query(int poz)
{
int s = 0;
while (poz > 0)
{
s += v[poz];
poz -= (poz & -poz);
}
return s;
}
int t_search(int l, int r, int val)
{
int mid = (l+r)/2;
int qmid = t_query(mid);
if (l > r)
return -1;
if (qmid == val)
return mid;
if (qmid < val)
{
return (t_search(mid+1, r, val));
}
else
{
return (t_search(l, mid-1, val));
}
}
void read()
{
scanf("%d %d ", &n, &m);
for (i=1; i<=n; ++i)
{
scanf("%d ", &x);
t_update(i, x);
}
}
void solve()
{
for (i=1; i<=m; ++i)
{
scanf("%d ", &t);
if (t == 0)
{
scanf("%d %d", &x, &y);
t_update(x, y);
}
else
if (t == 1)
{
scanf("%d %d", &x, &y);
tmp = t_query(y) - t_query(x-1);
printf("%d\n", tmp);
}
else
{
scanf("%d ", &x);
tmp = t_search(1, n, x);
printf("%d\n", tmp);
}
}
}
void write()
{
}
int main()
{
freopen("aib.in", "r", stdin);
freopen("aib.out","w",stdout);
read();
solve();
write();
return 0;
}