P1003 [NOIP2011 improvement group] carpet laying

Keywords: Dynamic Programming

P1003 [NOIP2011 improvement group] carpet laying

Title Description

In order to prepare for a unique award ceremony, the organizers laid some rectangular carpets on a rectangular area of the venue (which can be regarded as the first quadrant of the plane rectangular coordinate system). There are nn carpets, numbered from 11 to nn. Now lay these carpets in order from small to large parallel to the coordinate axis, and the later carpets cover the carpets already laid in front.

After the carpet is laid, the organizer wants to know the number of the top carpet covering a certain point on the ground. Note: points on the boundary and four vertices of the rectangular carpet are also covered by the carpet.

Input format

Enter a total of n + 2n+2 lines.

The first line, an integer nn, represents a total of nn carpets.

In the next nn line, line i+1i+1 represents the information of the carpet numbered ii, including four integers a, B, G, Ka, B, G and K, separated by a space between each two integers, representing the coordinates (a, b)(a,b) of the lower left corner of the carpet and the length of the carpet in the xx axis and yy axis respectively.

Line n + 2n+2 contains two integers xx and yy, representing the coordinates (x, y)(x,y) of the point on the ground.

Output format

Output a total of 11 lines, an integer representing the number of the carpet; If it is not covered by carpet, output - 1.

Input and output samples

Enter #1 copy

1 0 2 3
0 2 3 3
2 1 3 3
2 2

Output #1 copy


Enter #2 copy

1 0 2 3
0 2 3 3
2 1 3 3
4 5

Output #2 copy


Description / tips

[example explanation 1]

As shown in the figure below, carpet 11 is represented by a solid line, carpet 22 is represented by a dotted line, and carpet 33 is represented by a double solid line. The uppermost carpet covering point (2,2) (2,2) is carpet 33.

[data range]

For 30% to 30% of the data, there is n \le 2n ≤ 2.
For 50% 50% data, 0 \ Le a,b,g,k \ Le 1000 ≤ a,b,g,k ≤ 100.
For 100% 100% data, there are 0 \le n \le 10^40 ≤ n ≤ 104, 0 \le a, b, g, k \le {10}50 ≤ * a*,*b*,*g*,*k * ≤ 105.

noip2011 improve group day1 question 11.

Problem solving ideas

1. The first is the coverage problem. The back carpet is covered on the front carpet, and the result is the top carpet label. Therefore, the idea is to compare from the back. When the conditions are met, return the carpet label, and if not, return - 1

2. What are the conditions?

The condition is to see whether the input (x,y) coordinates are within the area of the uppermost carpet

‚Äč (x >= a[i] && x <= (a[i] + g[i]) && y >= b[i] && y <= (b[i] + k[i]))

The advantage of searching from the end is that it is fast and avoids too many repeated searches for the front carpet

In addition, there is a small detail worthy of attention, that is, when applying for array space during input, it should be noted that it is generally more than the maximum array length given by the title + 1

#include <iostream>
#include <sstream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
using namespace std;
#define ll long long
const ll mod = 1e9 + 9;
const int maxn = 2e6+5;
#define inf 0x3f3f3f3f
template <class T> void gmin(T& a, T b) {
    if(a > b) a = b;
template<class T> void gmax(T& a, T b) {
    if(a < b) a = b;
int n, a[10001], b[10001],g[10001],k[10001], x, y;
int main(){
    cin >> n;
    for (int i = 1; i <= n;i++){
        cin >> a[i] >> b[i] >> g[i] >> k[i];
    cin >> x >> y;
    for (int i = n; i >= 1; i--){
        if (x >= a[i] && x <= (a[i] + g[i]) && y >= b[i] && y <= (b[i] + k[i])){
            cout << i << endl;
            return 0;
    cout << -1 << endl;
    return 0;

[i] + k[i])){
cout << i << endl;
return 0;
cout << -1 << endl;
return 0;

Posted by lobster on Mon, 08 Nov 2021 00:47:25 -0800