Cod sursa(job #2224033)

Utilizator AlexandruPaulSirbu Alex AlexandruPaul Data 22 iulie 2018 15:59:01
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <iostream>
#include <fstream>
//problema simpla de boscheti de intervale pwp :*
using namespace std;
const int N=15002;
const int arb=4e5+5;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
bool op;
int n,m,st,dr;
int A[N];
int aint[arb];
int poz,sum,tot;
void build(int node,int left,int right){
    if (left==right){
        aint[node]=A[right]-A[left-1];
        return;
    }
    int mid=(left+right)/2;
    build(2*node,left,mid);
    build(2*node+1,mid+1,right);
    aint[node]=A[right]-A[left-1];
}
void update(int node,int left,int right){
    if (left==right){
        aint[node]-=sum;
        return;
    }
    int mid=(left+right)/2;
    if (poz<=mid) update(2*node,left,mid);
    else update(2*node+1,mid+1,right);
    aint[node]-=sum;
}
void querry(int node,int left,int right){
    if (st<=left && right<=dr){
        tot+=aint[node];
        return;
    }
    int mid=(left+right)/2;
    if (st<=mid) querry(2*node,left,mid);
    if (mid<dr)  querry(2*node+1,mid+1,right);
}
int main() {
    fin>>n>>m;
    for (int i=1,x;i<=n;++i){
        fin>>x;
        A[i]=A[i-1]+x;
    }
    build(1,1,n);
    ++m;
    for(;--m;){
        fin>>op>>st>>dr;
        if (!op){
            poz=st;
            sum=dr;
            update(1,1,n);
            continue;
        }
        tot=0;
        querry(1,1,n);
        fout<<tot<<"\n";
    }
    return 0;
}