Cod sursa(job #2086733)

Utilizator GrandmasterSoucup Bogdan Grandmaster Data 12 decembrie 2017 13:47:05
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.57 kb
#include <fstream>
#include <math.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
//#include <unordered_map>
#include <iomanip>
#include <time.h>
#include <stdio.h>
#include <bitset>
#include <map>
#include <ctime>
#include <stdlib.h>
#define MAX 500000000000
//#include <iostream>
//#include <windows.h>
#include <deque>
//#include "PEZai.h"
//#include <Tlhelp32.h>
#define dim 1004
using namespace std;
ifstream cin("jocul.in");
ofstream cout("jocul.out");
//ifstream cin("indep.in");
//ofstream cout("indep.out");
class Segmented_tree{
private:
    int n;
    vector<int> tree;
public:
    void Create_tree(int x[], int n, int neutral_element)
    {
        int v = n;
        this->n = 2 * n + 1;
        if((n & (n - 1)) == 0)
            this->n = 2 * n + 1;
        else
            this->n = (1<<int(log2(n) + 2)) + 1;
        for(int i = 0; i <= this->n; i++)
            tree.push_back(neutral_element);
        for(int i = this->n / 2; i < this->n / 2 + v; i++)
            tree[i] = x[i - this->n / 2];
        for(int i = this->n / 2 - 1; i >= 0; i--)
            tree[i] = tree[i * 2] + tree[i * 2 + 1];
    }
    void afis()
    {
        for(int i = 0; i < n; i++)
            cout << tree[i] << " ";
    }
    void addTree(int pos, int elem)
    {
        int tree_pos = pos + n / 2;
        tree[tree_pos] = elem;
        tree_pos /= 2;
        while(tree_pos > 0)
        {
            tree[tree_pos] = tree[tree_pos * 2] + tree[tree_pos * 2 + 1];
            tree_pos /= 2;
        }
    }
    int query(int left, int right)
    {
        int sum = 0;
        left += n / 2;
        right += n / 2;
        while(left < right)
        {
            if(left % 2 != 0)
            {
                sum += tree[left];
                left++;
            }
            if(right % 2 == 0)
            {
                sum += tree[right];
                right--;
            }
            left /= 2;
            right /= 2;
        }
        if(left == right)
            sum += tree[left];
        return sum;
    }
};
int x[15004];
int main()
{
    int n, m;
    Segmented_tree y;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> x[i];
    y.Create_tree(x, n, 0);
    for(int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        b--;
        if(a == 0)
        {
            x[b] -= c;
            y.addTree(b, x[b]);
        }
        else
        {
            c--;
            cout << y.query(b, c) << "\n";
        }
    }
}