## 程序设计：蒜头君的数轴

x1,x2,…, xn (−109≤xi≤109)，表示这些点的坐标，点坐标乱序排列。

4
1 3 7 15

1

### CODE

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
const int INF= 0x3f3f3f3f;
int val[N],gcd1[N],gcd2[N],dis[N];
int main()
{
int n,sum=0;
cin>>n;
for(int i=1;i<=n;i++) cin>>val[i];
sort(val+1,val+1+n); //因为输入是乱序，所以要排一下序
for(int i=1;i<n;i++){
dis[i]=val[i+1]-val[i];
sum+=dis[i];
}
gcd1[0]=0;//注意初始化
gcd2[n]=0;
for(int i=1;i<=n-1;i++) gcd1[i]=__gcd(gcd1[i-1],dis[i]);
for(int i=n-1;i>=1;i--) gcd2[i]=__gcd(gcd2[i+1],dis[i]);
int ans=INF;
for(int i=1;i<=n-1;i++){
int len=sum-dis[i];
int temp;
if(i==1) temp=(len/gcd2[i+1])-n+2;
else if(i==n-1) temp=(len/gcd1[n-2])-n+2;
else temp=(len/__gcd(gcd1[i-1],gcd2[i+1]))-n+2;
ans=min(ans,temp);
}
cout<<ans<<'\n';
return 0;
}


## Monitor

Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of $n \times m$.

But recently Xiaoteng found that his crops were often stolen by a group of people, so he decided to install some monitors to find all the people and then negotiate with them.

However, Xiao Teng bought bad monitors, each monitor can only monitor the crops inside a rectangle. There are $p$ monitors installed by Xiaoteng, and the rectangle monitored by each monitor is known.

Xiao Teng guess that the thieves would also steal $q$ times of crops. he also guessed the range they were going to steal, which was also a rectangle. Xiao Teng wants to know if his monitors can see all the thieves at a time.

Input

There are mutiple test cases.

Each case starts with a line containing two integers $n,m (1 <= n,1 <= m , n \times m <= 10^7)$ which represent the area of the land.

And the secend line contain a integer $p(1 <=p <= 10^6)$ which represent the number of the monitor Xiaoteng has installed. This is followed by p lines each describing a rectangle. Each of these lines contains four intergers $x_1,y_1,x_2andy_2(1<=x_1 <=x_2 <= n,1<= y_1 <= y_2 <= m)$ ,meaning the lower left corner and upper right corner of the rectangle.

Next line contain a integer $q(1 <= q<= 10^6)$ which represent the number of times that thieves will steal the crops. This is followed by q lines each describing a rectangle. Each of these lines contains four intergers $x_1,y_1,x_2andy_2(1<= x_1 <= x_2 <= n,1<= y_1 <= y_2 <= m)$,meaning the lower left corner and upper right corner of the rectangle.

Output

For each case you should print $q$ lines.

Each line containing YES or NO mean the all thieves whether can be seen.

Sample Input

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


Sample Output

YES
NO


### CODE

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <vector>
#include <set>
#define PI acos(-1)
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define debug freopen("in.txt","r",stdin); freopen("out.txt","w",stdout)
const int N=1e7+100;
using namespace std;
int n,m,v[N];
int get(int x,int y){
if(x==0||y==0||x>n||y>m) return 0;
return (x-1)*m+y;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
memset(v,0,sizeof v);
int t; scanf("%d",&t);
while(t--){
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
v[get(x1,y1)]++;
v[get(x2+1,y2+1)]++;
v[get(x1,y2+1)]--;
v[get(x2+1,y1)]--;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
v[get(i,j)]+=v[get(i-1,j)]+v[get(i,j-1)]-v[get(i-1,j-1)];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(v[get(i,j)]>0) v[get(i,j)]=1;
v[get(i,j)]+=v[get(i-1,j)]+v[get(i,j-1)]-v[get(i-1,j-1)];
}
}
int q; scanf("%d",&q);
while(q--){
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans=v[get(x2,y2)]+v[get(x1-1,y1-1)]-v[get(x2,y1-1)]-v[get(x1-1,y2)];
if(ans==(x2-x1+1)*(y2-y1+1))printf("YES\n");
else printf("NO\n");
}
}
return 0;
}


### 做法二

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int n,m,p,q;
int main(){
while(~scanf("%d%d",&n,&m)){
vector<vector<int> > ve(n+10,vector<int>(m+10,0));
scanf("%d",&p);
int x1,y1,x2,y2;
while(p--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
// cout<<x1<<y1<<x2<<y2<<endl;
ve[x1][y1]++;
ve[x2+1][y1]--;
ve[x1][y2+1]--;
ve[x2+1][y2+1]++;
}
//二维差分求前缀和
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
ve[i][j]+=ve[i-1][j]+ve[i][j-1]-ve[i-1][j-1];
}
//把每个格子都置为1
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
ve[i][j]=min(1,ve[i][j]);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
ve[i][j]+=ve[i-1][j]+ve[i][j-1]-ve[i-1][j-1];
}
scanf("%d",&q);
while(q--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int t=ve[x2][y2]-ve[x2][y1-1]-ve[x1-1][y2]+ve[x1-1][y1-1];
if(t==(x2-x1+1)*(y2-y1+1)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}