Pagini recente » Cod sursa (job #1021931) | Cod sursa (job #142307) | Cod sursa (job #331143) | Cod sursa (job #1714380) | Cod sursa (job #1139424)
#include <cstdio>
#include <algorithm>
#define nmax 100005
using namespace std;
long long aib[nmax];
int n,m;
void update(unsigned int pos,int val) {
while (pos <= n) {
int lsb = pos&(-pos);
aib[pos] += val;
pos += lsb;
}
}
long long query(int pos) {
long long s = 0;
while (pos > 0) {
int lsb = pos&(-pos);
s += aib[pos];
pos -= lsb;
}
return s;
}
void Q0() {
int a,b;
scanf("%d %d",&a,&b);
update(a,b);
}
void Q1() {
int a,b;
scanf("%d %d",&a,&b);
if (a > b) swap(a,b);
long long sum;
if (a>1) sum = query(b) - query(a-1);
else sum = query(b);
printf("%lld\n",sum);
}
void Q2() {
int a;
int start=1;
scanf("%d",&a);
while (1 << start <= n && query(1 << start) <= a) start += 1;
start -= 1;
int conf = 1<<start;
for (int i=start-1;i>=0;i--) {
if (conf+(1<<i) <= n && query(conf+(1<<i)) <= a) conf += 1<<i;
}
if (query(conf) == a) printf("%d\n",conf);
else printf("-1\n");
}
int main() {
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d %d\n",&n,&m);
for (int i=1;i<=n;i++) {
int aux;
scanf("%d",&aux);
update(i,aux);
}
for (int i=1;i<=m;i++) {
int type;
scanf("%d",&type);
if (type == 0) Q0();
else if (type == 1) Q1();
else Q2();
}
return 0;
}