E. Connected Component on a Chessboard
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two integers b and w. You have a chessboard of size 109×109 with the top left cell at (1;1), the cell (1;1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x,y) there are at most four connected cells: (x−1,y),(x+1,y),(x,y−1),(x,y+1)). A set of cells is called a connected component if for every pair of cells C1 and C2 from this set, there exists a sequence of cells c1, c2, ..., ck such that c1=C1, ck=C2, all ci from 1 to k are belong to this set of cells and for every i∈[1,k−1], cells ci and ci+1 are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1≤q≤105) — the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1≤b,w≤105) — the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2⋅105 (∑w+∑b≤2⋅105).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b+w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1;109].
Example
inputCopy
3
1 1
1 4
2 5
outputCopy
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
题意:
给你一个1e9*1e9的黑白棋盘,让你构造一个联通块,联通块中黑色个数为b,白色为w
思路:
直接构造一个横着的联通块,显然满足数据范围。
对黑色个数多还是白色个数多分开讨论,
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d
",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ' ', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int main()
{
//freopen("D:\common_text\code_stream\in.txt","r",stdin);
// freopen("D:\common_text\code_stream\out.txt","w",stdout);
int q;
gg(q);
while (q--)
{
int b, w;
gg(b); gg(w);
if (b < w)
{
int m = b * 3 + 1;
if (w <= m)
{
printf("YES
");
cout<<2<<" "<<2<<endl;
w--;
int x=3;
int y=2;
while(1)
{
cout<<y<<" "<<x<<endl;
if(w>b)
{
cout<<y-1<<" "<<x<<endl;
w--;
}
if(w>b)
{
cout<<y+1<<" "<<x<<endl;
w--;
}
if(w)
{
cout<<y<<" "<<x+1<<endl;
w--;
}
b--;
if(!b&&!w)
{
break;
}
x+=2;
}
} else
{
printf("NO
");
}
} else
{
int m = w * 3 + 1;
if (b <= m)
{
printf("YES
");
cout<<2<<" "<<3<<endl;
b--;
int x=4;
int y=2;
while(1)
{
cout<<y<<" "<<x<<endl;
if(b>w)
{
cout<<y-1<<" "<<x<<endl;
b--;
}
if(b>w)
{
cout<<y+1<<" "<<x<<endl;
b--;
}
if(b)
{
cout<<y<<" "<<x+1<<endl;
b--;
}
w--;
if(!b&&!w)
{
break;
}
x+=2;
}
} else
{
printf("NO
");
}
}
cout<<endl;
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '
');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}