Pagini recente » Cod sursa (job #2495216) | Cod sursa (job #522879) | Cod sursa (job #2674678) | Cod sursa (job #1888686) | Cod sursa (job #1214448)
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <string.h>
#include <queue>
#include <math.h>
#include <set>
#include <stack>
#define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a<b)?b:a)
using namespace std;
//#define TEST
#ifdef TEST
ifstream fin("input.txt");
ofstream fout("output.txt");
#else
ifstream fin("aib.in");
ofstream fout("aib.out");
#endif // TEST
int n,m;
int a[100001];
void add(int p, int key)
{
if (p>n) return;
a[p]+=key;
add(p+(p&(-p)),key);
}
int query(int x)
{
if (!x) return 0;
return a[x] + query(x - (x&(-x)));
}
int main()
{
fin>>n>>m;
for (int i=0; i<n; i++)
{
int x;
fin>>x;
add(i+1,x);
}
for (int i=0; i<m; i++)
{
int x,y,z;
fin>>x>>y;
if (x==0)
{
fin>>z;
add(y,z);
} else if (x==1)
{
fin>>z;
fout<<query(z)-query(y-1);
fout<<'\n';
} else
{
int l = 1,r=n;
while (l<r)
{
int mm = (l+r)/2;
if (query(mm)>y)
r=mm-1;
else if (query(mm)<y)
l=mm+1;
else
{
l=mm;
r=mm;
}
}
if (query(l)==y)
fout<<l<<'\n';
else fout<<"-1\n";
}
}
return 0;
}