PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000084. Cắt lưới thành ba phần (Check if Grid can be Cut into Sections)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cắt lưới thành ba phần (Check if Grid can be Cut into Sections)

Check if Grid can be Cut into Sections

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An n×nn\times n grid contains mm non-overlapping rectangles. Determine whether two parallel horizontal or vertical cuts can divide the grid into three sections, with at least one rectangle in each section and no rectangle split through its interior. Cuts may lie on rectangle boundaries.

Input

The first line contains n,mn,m, followed by mm lines x1,y1,x2,y2x_1,y_1,x_2,y_2. This is the stdin/stdout adaptation.

Output

Print YES if two valid cuts exist; otherwise print NO.

Subtasks

  • Subtask 1 (20 points): 3≤m≤203\le m\le 20; 3≤n≤1093\le n\le10^9; 0≤x1<x2≤n0\le x_1<x_2\le n; 0≤y1<y2≤n0\le y_1<y_2\le n; rectangles have pairwise disjoint interiors.
  • Subtask 2 (30 points): 3≤m≤20003\le m\le 2000; 3≤n≤1093\le n\le10^9; 0≤x1<x2≤n0\le x_1<x_2\le n; 0≤y1<y2≤n0\le y_1<y_2\le n; rectangles have pairwise disjoint interiors.
  • Subtask 3 (50 points): 3≤m≤1000003\le m\le 100000; 3≤n≤1093\le n\le10^9; 0≤x1<x2≤n0\le x_1<x_2\le n; 0≤y1<y2≤n0\le y_1<y_2\le n; rectangles have pairwise disjoint interiors.

Examples

Input

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

Output

YES

Explanation

Horizontal cuts at y=2y=2 and y=4y=4 create three non-empty sections. Every rectangle lies entirely inside one section.