Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by Pedro ( 6 years ago )
#include <bits/stdc++.h>

using namespace std;

const int MAX = 2<<3;
int tree[MAX * 2 + 10];

void add(int x, int a, int b, int l, int r, int v)
{
    //cout << x << " " << a << " " << b << " " << l << " " << r << " " << v << " " << tree[x] << "\n";
    if(l >= a && r <= b)
    {
        tree[x] += v;
        return;
    }

    if(b >= l && a <= r - (r-l+1)/2)
        add(x * 2, a, b, l, r - (r-l+1)/2, v);
    if(b >= l + (r-l+1)/2 && a <= r)
        add(x * 2 + 1, a, b, l + (r-l+1)/2, r, v);
}

int check(int x, int l, int r, int a)
{
    //cout << x << " " << l << " " << r << " " << a << " " << tree[x] << "\n";
    if(l == r)
        return tree[x];
    tree[2 * x] += tree[x];
    tree[2 * x + 1] += tree[x];
    tree[x] = 0;
    if(a <= r - (r-l+1)/2)
    {
        //cout << "0\n";
        return check(x * 2, l, r - (r-l+1)/2, a);
    }
    else
    {
        //cout << "1\n";
        return check(x * 2 + 1, l + (r-l+1)/2, r, a);
    }
}

int main()
{
    add(1, 3, 5, 1, MAX, 5);

    cout << "\n";

    add(1, 3, 5, 1, MAX, 0);

    cout << "\n";

    for(int i = 1; i <= MAX; i++)
        cout << i << " ";

    cout << "\n";

    for(int i = 1; i <= MAX; i++)
    {
        cout << check(1, 1, MAX, i) << " ";
        if (i >= 10)
            cout << " ";
    }

    cout << "\n";

    add(1, 4, 9, 1, MAX, 2);

    add(1, 7, 13, 1, MAX, 3);

    for(int i = 1; i <= MAX; i++)
    {
        cout << check(1, 1, MAX, i) << " ";
        if (i >= 10)
            cout << " ";
    }


    cout << "\n";

    cout << MAX << "\n";
    cout << "Hello world!" << endl;
    return 0;
}

 

Revise this Paste

Your Name: Code Language: