int monta(int ini, int fim) {
    int at = cabeca++;
    tree[at].ini = ini;
    tree[at].fim = fim;
    tree[at].trocas = 0;
    if(ini == fim) {
        tree[at].esq = tree[at].dir = -1;
        tree[at].qtd[1] = tree[at].qtd[2] = 0;
        tree[at].qtd[0] = 1;
        return at;
    }
    int e = tree[at].esq = monta(ini, (ini+fim)/2);
    int d = tree[at].dir = monta((ini+fim)/2 + 1, fim);
    for(int i = 0; i < 3; i++) {
        tree[at].qtd[i] = tree[e].qtd[i] + tree[d].qtd[i];
    }
    return at;
}
int main() {
    int n,q,op,a,b;
    scanf("%d %d", &n, &q);
    cabeca = 0; monta(0,n-1);
    for(int i = 0; i < q; i++) {
        scanf("%d %d %d", &op, &a, &b);
        if(op == 0) {
            soma(0,a,b);
        } else {
            printf("%d\n", query(a,b));
        }
    }
}
Example #3
0
int main () {
    while(scanf("%d %d", &m, &n) != EOF && n && m) {
        for (int i = 0; i < n; i++) {
            scanf("%d", &v[i]);
        }

        for (int i = 0; i <= m; i++) {
            memo[i] = 0;
        }
        
        monta(m);

        if (memo[m] >= 50010) printf("Impossivel\n");
        else printf("%d\n", memo[m]);
    }
}
Example #4
0
int monta (int p) {
    int min, aux;

    if (p == 0) return 0;
    if (memo[p]) {
//        printf("memo[%d] == %d\n", p, memo[p]);
        return memo[p];
    }

    min = 50010;
    for (int i = 0; i < n; i++) {
        if (p >= v[i]) {
            aux = monta(p-v[i]) + 1;
//            printf("%d - %d = %d -> %d\n", p, v[i], p-v[i], aux);
            if (aux < min) min = aux;
        }
    }

    memo[p] = min;
    return min;
}