Cod sursa(job #3367986)

Utilizator andrei_sevescusevescu andrei andrei_sevescu Data 12 octombrie 2026 16:29:29
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <bits/stdc++.h>

using namespace  std;

#define N 100000

int aib[N+1],s[N+1];

void update(int a, int b, int n) {
    int p2=1U<<24;
    printf("%d " ,p2);
    while (a%p2>0)
        p2/=2;
    while (a<=n) {
        a+=p2;
        aib[a]+=b;
        do
            p2*=2;
        while (a%p2);
        p2/=2;

    }
}
int query(int a) {
    int p2=1U<<24,sum=0;
    while (a%p2>0)
        p2/=2;
    while (a>0) {
        sum+=aib[a];
        a-=p2;
        do
            p2*=2;
        while (a%p2);
        p2/=2;
    }
    return sum;
}

ifstream fin("aib.in");
ofstream fout("aib.out");

int main() {
    printf(".");
    int n,q,i,a,b,cer,p2;
    fin >> n >> q;
    for (i=1;i<=n;i++){
        fin >> a;
        s[i]=s[i-1]+a;
        p2 = 1U<<24;
        while (i%p2>0)
            p2/=2;
        aib[i]=s[i]-s[i-p2];
    }
    for (i=0;i<q;i++) {
        fin >> cer >> a;
        if (cer<2) {
            fin >> b;
            if (cer==0)
                update(a, b, n);
            else {
                cout << query(b)-query(a-1) << endl;
            }
        }else {
            int pas=1U<<24,sum,poz;
            while (pas>n)
                pas/=2;
            sum=0;
            poz=pas;
            pas/=2;
            while (pas) {
                sum+=aib[poz];
                if (sum>a) {
                    sum-=aib[poz];
                    poz-=pas;
                }else
                    poz+=pas;
                pas/=2;
            }
            fout << poz << endl;
        }
    }
    return 0;
}