codeforces-scrape

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

1722E.md (2675B)


      1 Source: https://codeforces.com/problemset/problem/1722/E
      2 
      3 E. Counting Rectangles
      4 
      5 time limit per test
      6 
      7 6 seconds
      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 have $n$ rectangles, the $i$-th rectangle has height $h_i$ and width $w_i$.
     22 
     23 You are asked $q$ queries of the form $h_s \ w_s \ h_b \ w_b$.
     24 
     25 For each query output, the total area of rectangles you own that can fit a rectangle of height $h_s$ and width $w_s$ while also fitting in a rectangle of height $h_b$ and width $w_b$. In other words, print $\sum h_i \cdot w_i$ for $i$ such that $h_s \lt h_i \lt h_b$ and $w_s \lt w_i \lt w_b$.
     26 
     27 Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.
     28 
     29 Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
     30 
     31 Input
     32 
     33 The first line of the input contains an integer $t$ ($1 \leq t \leq 100$) — the number of test cases.
     34 
     35 The first line of each test case two integers $n, q$ ($1 \leq n \leq 10^5$; $1 \leq q \leq 10^5$) — the number of rectangles you own and the number of queries.
     36 
     37 Then $n$ lines follow, each containing two integers $h_i, w_i$ ($1 \leq h_i, w_i \leq 1000$) — the height and width of the $i$-th rectangle.
     38 
     39 Then $q$ lines follow, each containing four integers $h_s, w_s, h_b, w_b$ ($1 \leq h_s \lt h_b,\ w_s \lt w_b \leq 1000$) — the description of each query.
     40 
     41 The sum of $q$ over all test cases does not exceed $10^5$, and the sum of $n$ over all test cases does not exceed $10^5$.
     42 
     43 Output
     44 
     45 For each test case, output $q$ lines, the $i$-th line containing the answer to the $i$-th query.
     46 
     47 Example
     48 
     49 Input
     50 
     51 ```
     52 3
     53 
     54 2 1
     55 
     56 2 3
     57 
     58 3 2
     59 
     60 1 1 3 4
     61 
     62 5 5
     63 
     64 1 1
     65 
     66 2 2
     67 
     68 3 3
     69 
     70 4 4
     71 
     72 5 5
     73 
     74 3 3 6 6
     75 
     76 2 1 4 5
     77 
     78 1 1 2 10
     79 
     80 1 1 100 100
     81 
     82 1 1 3 3
     83 
     84 3 1
     85 
     86 999 999
     87 
     88 999 999
     89 
     90 999 998
     91 
     92 1 1 1000 1000
     93 ```
     94 
     95 Output
     96 
     97 ```
     98 6
     99 41
    100 9
    101 0
    102 54
    103 4
    104 2993004
    105 ```
    106 
    107 Note
    108 
    109  ![](https://espresso.codeforces.com/aa51b7dfff92458931d37d604395e88d39f0df91.png) 
    110 
    111 In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $1 \times 1$ rectangle inside of it and fit into a $3 \times 4$ rectangle.
    112 
    113 Only the $2 \times 3$ rectangle works, because $1 \lt 2$ (comparing heights) and $1 \lt 3$ (comparing widths), so the $1 \times 1$ rectangle fits inside, and $2 \lt 3$ (comparing heights) and $3 \lt 4$ (comparing widths), so it fits inside the $3 \times 4$ rectangle. The $3 \times 2$ rectangle is too tall to fit in a $3 \times 4$ rectangle. The total area is $2 \cdot 3 = 6$.