Cod sursa(job #1507988)

Utilizator c0rn1Goran Cornel c0rn1 Data 22 octombrie 2015 10:33:31
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <queue>
#define NMAX 100008
#define inclus(a, b, c, d) ((a) >= (c) && (b) <= (d))

using namespace std;

int n, k;
int arb[4*NMAX];
int index, x; /// for type 1
int st, dr; /// for type 0
int sum;

void update(int pos, int l, int r)
{
   int mid = (l+r)/2;

   if (l == r){
      arb[pos] -= x;
      return;
   }

   if (index <= mid)
      update(pos*2, l, mid);
   else
      update(pos*2+1, mid+1, r);

   arb[pos] = arb[pos*2] + arb[pos*2+1];
}

void determineMax(int pos, int l, int r)
{
   if (st <= l && r <= dr){
      sum += arb[pos];
      return;
   }
   int mid = (l + r) / 2;
   if (st <= mid)
      determineMax(pos*2, l, mid);
   if (dr > mid)
      determineMax(pos*2+1, mid+1, r);
}

void readData()
{
   scanf("%d %d\n", &n, &k);
   for (index = 1; index <= n; ++index){
      scanf("%d ", &x);
      x = -x;
      update(1, 1, n);
   }
}

void readQueries()
{
   int type, arg1, arg2;
   for (int i = 0; i < k; ++i){
      scanf("%d %d %d\n", &type, &arg1, &arg2);
      if (type == 1){
         st = arg1;
         dr = arg2;
         sum = 0;
         determineMax(1, 1, n);
         printf("%d\n", sum);
      }
      else if (type == 0){
         index = arg1;
         x = arg2;
         update(1, 1, n);
      }
   }
}

int main()
{
   freopen("datorii.in", "r", stdin);
   freopen("datorii.out", "w", stdout);
   readData();
   readQueries();

   return 0;
}