Algorithm


C. Masha and two friends
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Masha was presented with a chessboard with a height of n and a width of m.

The rows on the chessboard are numbered from 11 to n from bottom to top. The columns are numbered from 11 to m from left to right. Therefore, each cell can be specified with the coordinates (x,y)(�,�), where x is the column number, and y is the row number (do not mix up).

Let us call a rectangle with coordinates (a,b,c,d)(�,�,�,�) a rectangle lower left point of which has coordinates (a,b)(�,�), and the upper right one — (c,d)(�,�).

The chessboard is painted black and white as follows:

An example of a chessboard.

Masha was very happy with the gift and, therefore, invited her friends Maxim and Denis to show off. The guys decided to make her a treat — they bought her a can of white and a can of black paint, so that if the old board deteriorates, it can be repainted. When they came to Masha, something unpleasant happened: first, Maxim went over the threshold and spilled white paint on the rectangle (x1,y1,x2,y2)(�1,�1,�2,�2). Then after him Denis spilled black paint on the rectangle (x3,y3,x4,y4)(�3,�3,�4,�4).

To spill paint of color color����� onto a certain rectangle means that all the cells that belong to the given rectangle become color�����. The cell dyeing is superimposed on each other (if at first some cell is spilled with white paint and then with black one, then its color will be black).

Masha was shocked! She drove away from the guests and decided to find out how spoiled the gift was. For this, she needs to know the number of cells of white and black color. Help her find these numbers!

Input

The first line contains a single integer t (1t1031≤�≤103) — the number of test cases.

Each of them is described in the following format:

The first line contains two integers n and m (1n,m1091≤�,�≤109) — the size of the board.

The second line contains four integers x1�1y1�1x2�2y2�2 (1x1x2m,1y1y2n1≤�1≤�2≤�,1≤�1≤�2≤�) — the coordinates of the rectangle, the white paint was spilled on.

The third line contains four integers x3�3y3�3x4�4y4�4 (1x3x4m,1y3y4n1≤�3≤�4≤�,1≤�3≤�4≤�) — the coordinates of the rectangle, the black paint was spilled on.

Output

Output t lines, each of which contains two numbers — the number of white and black cells after spilling paint, respectively.

Example
input
Copy
5
2 2
1 1 2 2
1 1 2 2
3 4
2 2 3 2
3 1 4 3
1 5
1 1 5 1
3 1 5 1
4 4
1 1 4 2
1 3 4 4
3 4
1 2 4 2
2 1 3 3
output
Copy
0 4
3 9
2 3
8 8
4 8
Note

Explanation for examples:

The first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illustration shows how the field looked after Denis spoiled black dye (the rectangle on which the dye was spilled is highlighted with red).

In the first test, the paint on the field changed as follows:

In the second test, the paint on the field changed as follows:

In the third test, the paint on the field changed as follows:

In the fourth test, the paint on the field changed as follows:

In the fifth test, the paint on the field changed as follows:

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int t, n, m;
int x, y, x2, y2;
int xx, yy, xx2, yy2;
int xxx, yyy, xxx2, yyy2;

int FindPoints() { 
  xxx = max(x, xx);
  yyy = max(y, yy);

  xxx2 = min(x2, xx2);
  yyy2 = min(y2, yy2);

  if(xxx > xxx2 || yyy > yyy2)
    return 0;

  return 1;
}

void calcWB(int x, int y, int x2, int y2, long long &w, long long &b) {
  int dx = abs(x2 - x + 1);
  int dy = abs(y2 - y + 1);

  if(x % 2 == 1 && y % 2 == 1 || x % 2 == 0 && y % 2 == 0) {
    if(dx % 2 == 0) {
      w = 1ll * dx / 2 * dy;
      b = 1ll * dx * dy - w;
    } else {
      w = 1ll * ((dx + 1) / 2) * ((dy + 1) / 2) + 1ll * (dx / 2) * (dy / 2);
      b = 1ll * dx * dy - w;
    }
  } else {
    if(dx % 2 == 0) {
      b = 1ll * dx / 2.0 * dy;
      w = 1ll * dx * dy - b;
    } else {
      b = 1ll * ((dx + 1) / 2) * ((dy + 1) / 2) + 1ll * (dx / 2) * (dy / 2);
      w = 1ll * dx * dy - b;
    }
  }
}

int main() {
  scanf("%d", &t);
  for(int i = 0; i < t; ++i) {
    scanf("%d %d", &n, &m);
    scanf("%d %d %d %d", &x, &y, &x2, &y2);
    scanf("%d %d %d %d", &xx, &yy, &xx2, &yy2);

    // calculate all
    long long w, b;
    calcWB(1, 1, m, n, w, b);

    // first query
    long long w1, b1;
    calcWB(x, y, x2, y2, w1, b1);

    // update
    b -= b1;
    w += b1;

    // second query
    long long w2, b2;
    calcWB(xx, yy, xx2, yy2, w2, b2);

    // check intersection
    if(FindPoints() == 1) {
      long long ww, bb;
      calcWB(xxx, yyy, xxx2, yyy2, ww, bb);

      b2 -= bb;
      w2 += bb;
    }

    // update
    b += w2;
    w -= w2;

    printf("%lld %lld\n", w, b);
  }

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
2 2
1 1 2 2
1 1 2 2
3 4
2 2 3 2
3 1 4 3
1 5
1 1 5 1
3 1 5 1
4 4
1 1 4 2
1 3 4 4
3 4
1 2 4 2
2 1 3 3

Output

x
+
cmd
0 4
3 9
2 3
8 8
4 8
Advertisements

Demonstration


Codeforcess Solution-C. Masha and two friends--Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+