Pagini recente » Cod sursa (job #3358776) | Cod sursa (job #3361302)
#include <bits/stdc++.h>
#define int long long int
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
vector<int> tree;
int n,m;
void update(int ind, int val)
{
while(ind <= n)
{
tree[ind] += val;
ind += ind & (- ind);
}
}
int query(int ind)
{
int sum = 0;
while(ind >= 1)
{
sum += tree[ind];
ind -= ind & (-ind);
}
return sum;
}
int find(int target)
{
int pos = 0;
int sum = 0;
int pas = 1;
while (pas * 2 <= n)
pas *= 2;
while (pas > 0)
{
int nextpos = pos + pas;
if (nextpos <= n && sum + tree[nextpos] < target)
{
pos = nextpos;
sum += tree[nextpos];
}
pas /= 2;
}
int cand = pos + 1;
if(cand <= n && query(cand) == target)
return cand;
return -1;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
fin >> n >> m;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++)
fin >> a[i];
tree.resize(n + 1);
for(int i = 1; i <= n; i++)
update(i, a[i]);
while(m--)
{
int type;
fin >> type;
if(type == 0)
{
int a,b;
fin >> a >> b;
update(a, b);
}
else if(type == 1)
{
int a, b;
fin >> a >> b;
fout << query(b) - query(a - 1);
fout << '\n';
}
else
{
int targ;
fin >> targ;
fout << find(targ);
fout << '\n';
}
}
return 0;
}