Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя _spartan

Автор _spartan, 10 лет назад, По-английски

I have been learning 2-D BIT lately.

But I am having difficulty implementing range updates in it.

Eg. Suppose we have a matrix M[][].There are 2 types of queries:

1.ADD x1 y1 x2 y2 val

This adds val to all matrix elements which have their x-coordinate between x1 and x2 and y-coordinate between y1 and y2.

2.SUM x1 y1 x2 y2

This query returns the sum of all matrix elements which have their x-coordinate between x1 and x2 and y-coordinate between y1 and y2.

I am having trouble implementing the first query using 2-D BIT.

Had point(x1, y1) and point(x2, y2) been same, following code would work :

void update(int x1, int y1, ll val)

{

int x, y;

x=x1;

while(x<=n){

    y=y1;

    while(y<=m){                  //Update y coordinate

       ft[x][y]+=val;        //ft stores BIT

       y+=(y&-y);

    }

    x+=(x&-x);

}

return;

}

How to go about it if I have to update the whole range?

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
10 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I'm not sure you can do this in a simple way using BIT

You need to use 2D segment Trees

»
10 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

This kind of problems cannot be solve using regular BIT (using only one BIT) since regular BIT doesn't support range update.

This problem is similar with USUBQSUB on SPOJ, solution using Range Update BIT explained here http://codeforces.me/blog/entry/8874#comment-146017

  • »
    »
    8 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    is not really true, BIT does not support range update and range sum at the same time, but we can simply implement range update and single element query

»
10 лет назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

I suppose this paper would be useful for you.