The Tower is Going Home【数学】

版权声明:版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/YT201758501112/article/details/83901690

The Tower is Going Home

 CodeForces - 1044A 

On a chessboard with a width of 109109 and a height of 109109, the rows are numbered from bottom to top from 11 to 109109, and the columns are numbered from left to right from 11to 109109. Therefore, for each cell of the chessboard you can assign the coordinates (x,y)(x,y), where xx is the column number and yy is the row number.

Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1)(1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 109109).

Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:

  • Vertical. Each of these is defined by one number xx. Such spells create an infinite blocking line between the columns xx and x+1x+1.
  • Horizontal. Each of these is defined by three numbers x1x1, x2x2, yy. Such spells create a blocking segment that passes through the top side of the cells, which are in the row yy and in columns from x1x1 to x2x2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.

An example of a chessboard.

Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r0,c0)(r0,c0) into the cell (r1,c1)(r1,c1) only under the condition that r1=r0r1=r0 or c1=c0c1=c0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).

Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!

Input

The first line contains two integers nn and mm (0≤n,m≤1050≤n,m≤105) — the number of vertical and horizontal spells.

Each of the following nn lines contains one integer xx (1≤x<1091≤x<109) — the description of the vertical spell. It will create a blocking line between the columns of xx and x+1x+1.

Each of the following mm lines contains three integers x1x1, x2x2 and yy (1≤x1≤x2≤1091≤x1≤x2≤109, 1≤y<1091≤y<109) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number yy, in columns from x1x1 to x2x2 inclusive.

It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.

Output

In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1)(1,1) to at least one cell in the row with the number 109109

Examples

Input

2 3
6
8
1 5 6
1 9 4
2 4 2

Output

1

Input

1 3
4
1 5 3
1 9 4
4 6 6

Output

1

Input

0 2
1 1000000000 4
1 1000000000 2

Output

2

Input

0 0

Output

0

Input

2 3
4
6
1 4 3
1 5 2
1 6 5

Output

2

Note

In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.

Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.

In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.

Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.

In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.

 Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.

In the fourth sample, we have no spells, which means that we do not need to remove anything.

In the fifth example, we can remove the first vertical and third horizontal spells.

Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.

题目大意:第一行输入两个整数n,m,代表有n个竖着的墙,其下n行代表竖着的魔法墙的位置,m代表有m个横着的魔法墙,n行行的m行每行有三个数x1,x2,y,代表在(x1,y)到 (x2,y)之间有一道横着的魔法墙,问从(1,1)出发到达(x,1e9)至少要消除多少道魔法墙。

解决方法:若要消除魔法墙,则证明当前其处于一个封闭房间内,从(1,1)出发,就证明我们需要找到有多少个不共边的封闭矩形,因此对于横着的魔法墙,我们仅需要考虑从1出发的,将其终点存入一个数组a[]中,将竖着的魔法墙的位置存入数组b[]中,将两个数组从大到小排序,以数组a为基础遍历,在数组b中找到第一个小于它的值(即这两条边相交),两数组继续向下遍历,找到有多少相交的矩形,此题还需考虑一特殊情况即若横着的魔法墙为(1,1000000000),则其必须消去,因此在输入时及时统计这种情况的数量,若数量会大于矩形的个数,则输出数量,否则直接输出遍历得到的答案。

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int len=1e6+7;
const int MOD= 1e9;
int a[len];
int b[len];
int main()
{
   int n,m;
   scanf("%d%d",&n,&m);
   rep(i,1,n) scanf("%d",&a[i]);
   sort(a+1,a+n+1,greater<int> ());
   int m1=0;
   int cnt=0;
   rep(i,1,m)
   {
       int x1,x2,y;
       scanf("%d%d%d",&x1,&x2,&y);
       if(x1>1) continue;
       b[++m1]=x2;
       if(x2==1e9) cnt++;
   }
   if(n==0) { n=m1;
    rep(i,1,n) a[i]=1e9;
   }
    sort(b+1,b+1+m1,greater<int> ());
   int ans=0;
   int j=1,i=1;
   for(;i<=n&&j<=m1;){
       if(b[j]<a[i]){
        i++;
       }
       else if(b[j]>=a[i]){
        ans++;j++;i++;
       }
   }
   printf("%d\n",max(ans,cnt));
 return 0;
}

猜你喜欢

转载自blog.csdn.net/YT201758501112/article/details/83901690