codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1027B.md (2147B)


      1 Source: https://codeforces.com/problemset/problem/1027/B
      2 
      3 B. Numbers on the Chessboard
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given a chessboard of size $n \times n$. It is filled with numbers from $1$ to $n^2$ in the following way: the first $\lceil \frac{n^2}{2} \rceil$ numbers from $1$ to $\lceil \frac{n^2}{2} \rceil$ are written in the cells with even sum of coordinates from left to right from top to bottom. The rest $n^2 - \lceil \frac{n^2}{2} \rceil$ numbers from $\lceil \frac{n^2}{2} \rceil + 1$ to $n^2$ are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation $\lceil\frac{x}{y}\rceil$ means division $x$ by $y$ rounded up.
     22 
     23 For example, the left board on the following picture is the chessboard which is given for $n=4$ and the right board is the chessboard which is given for $n=5$.
     24 
     25  ![](https://espresso.codeforces.com/7759c6c9459ee924858c300b3920de9f90fdc14a.png) 
     26 
     27 You are given $q$ queries. The $i$-th query is described as a pair $x_i, y_i$. The answer to the $i$-th query is the number written in the cell $x_i, y_i$ ($x_i$ is the row, $y_i$ is the column). Rows and columns are numbered from $1$ to $n$.
     28 
     29 Input
     30 
     31 The first line contains two integers $n$ and $q$ ($1 \le n \le 10^9$, $1 \le q \le 10^5$) — the size of the board and the number of queries.
     32 
     33 The next $q$ lines contain two integers each. The $i$-th line contains two integers $x_i, y_i$ ($1 \le x_i, y_i \le n$) — description of the $i$-th query.
     34 
     35 Output
     36 
     37 For each query from $1$ to $q$ print the answer to this query. The answer to the $i$-th query is the number written in the cell $x_i, y_i$ ($x_i$ is the row, $y_i$ is the column). Rows and columns are numbered from $1$ to $n$. Queries are numbered from $1$ to $q$ in order of the input.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 4 5  
     45 1 1  
     46 4 4  
     47 4 3  
     48 3 2  
     49 2 4
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 1  
     56 8  
     57 16  
     58 13  
     59 4
     60 ```
     61 
     62 Input
     63 
     64 ```
     65 5 4  
     66 2 1  
     67 4 2  
     68 3 3  
     69 3 4
     70 ```
     71 
     72 Output
     73 
     74 ```
     75 16  
     76 9  
     77 7  
     78 20
     79 ```
     80 
     81 Note
     82 
     83 Answers to the queries from examples are on the board in the picture from the problem statement.