Pagini recente » Cod sursa (job #3315045) | Monitorul de evaluare | Cod sursa (job #3338465) | Cod sursa (job #184579) | Cod sursa (job #3311040)
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
int a[100001];
void update(int poz, int val)
{
while(poz <= n)
{
a[poz] += val;
poz += poz & (-poz);
}
}
int query(int poz)
{
int s = 0;
while(poz > 0)
{
s += a[poz];
poz -= poz & (-poz);
}
return s;
}
void operation2(int x)
{
int left = 1, right = n;
while (left <= right)
{
int mid = (left + right) / 2;
if (query(mid) >= x)
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
if (left <= n)
{
cout << left << "\n";
}
else
{
cout << -1 << "\n";
}
}
int32_t main()
{
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
int x;
cin >> x;
update(i, x);
}
for(int i = 1; i <= m; i++)
{
int c, x, y;
cin >> c;
if(c == 0)
{
cin >> x >> y;
update(x, y);
}
else if(c == 1)
{
cin >> x >> y;
cout << query(y) - query(x - 1) << "\n";
}
else
{
cin >> x;
operation2(x);
}
}
}