Объясните и предоставьте, когда мы запускаем весь метод обновления. как он добавляет только особую точку, когда он прохоJAVA

Программисты JAVA общаются здесь
Anonymous
Объясните и предоставьте, когда мы запускаем весь метод обновления. как он добавляет только особую точку, когда он прохо

Сообщение Anonymous »

Код: Выделить всё

// Java program implementing the above queries
import java.util.*;
class GFG
{
static int maxn = 2005;

// 2D Binary Indexed Tree. Note: global variable
// will have initially all elements zero
static int [][]bit = new int[maxn][maxn];

// function to add a point at (x, y)
static void update(int x, int y)
{
int y1;
while (x < maxn)
{
// x is the xth BIT that will be updated
// while y is the indices where an update
// will be made in xth BIT
y1 = y;
while ( y1 < maxn )
{
bit[x][y1]++;
y1 += (y1 & -y1);
}

// next BIT that should be updated
x += x & -x;
}
}

// Function to return number of points in the
// rectangle (1, 1), (x, y)
static int query(int x, int y)
{
int res = 0, y1;
while (x > 0)
{
// xth BIT's yth node
// must be added to the result
y1 = y;
while (y1 > 0)
{
res += bit[x][y1];
y1 -= y1 & -y1;
}

// next BIT that will contribute to the result
x -= x & -x;
}
return res;
}

// (x1, y1) is the lower left and (x2, y2) is the
// upper right corner of the rectangle
static int pointsInRectangle(int x1, int y1,
int x2, int y2)
{
// Returns number of points in the rectangle
// (x1, y1), (x2, y2) as described in text above
return query(x2, y2) - query(x1 - 1, y2) -
query(x2, y1 - 1) +
query(x1 - 1, y1 - 1);
}

// Returns count of triangles with n points, i.e.,
// it returns nC3
static int findTriangles(int n)
{
// returns pts choose 3
return (n * (n - 1) * (n - 2)) / 6;
}

// Driver Code
public static void main(String[] args)
{
// inserting points
update(2, 2);
update(3, 5);
update(4, 2);
update(4, 5);
update(5, 4);

System.out.print("No. of triangles in the " +
"rectangle (1, 1) (6, 6) are: " +
findTriangles(pointsInRectangle(1, 1, 6, 6)));

update(3, 3);

System.out.print("\nNo. of triangles in the " +
"rectangle (1, 1) (6, 6) are: " +
findTriangles( pointsInRectangle(1, 1, 6, 6)));
}
}
из приведенного выше кода. что происходит, когда мы вызываем метод update(2,2). Можете ли вы предоставить подробное объяснение того, что происходит при вызове каждого метода обновления, и можете ли вы предоставить двумерную двоичную индексированную матрицу?
Я понял другие методы, но не понимаю, почему они там в матрицу добавляется только одна точка, когда while ( y1 < maxn )

Код: Выделить всё

  {
bit[x][y1]++;
y1 += (y1 & -y1);
}
означает, что будет точка в (2,2),(2,3) и...

Подробнее здесь: https://stackoverflow.com/questions/785 ... dding-a-si

Вернуться в «JAVA»