output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
|---|---|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int num = 0;
char c = getchar();
if (c == '-') return -read();
while (c >= '0' && c <= '9') {
num = (num << 3) + (num << 1) + c - '0';
c = getchar();
}
return num;
}
inline long long READ() {
long long num = 0;
char c = getchar();
if (c == '-') return -READ();
while (c >= '0' && c <= '9') {
num = (num << 3) + (num << 1) + c - '0';
c = getchar();
}
return num;
}
long long n;
int m;
int cnt[10];
long long p10[19], p10m[19];
int wei;
unordered_map<long long, long long> mm;
long long dfs(long long djg, long long num, long long md) {
if (!djg) return md == 0;
long long xz = num * 100 + md, bb = num, ans = 0;
if (mm.count(xz)) return mm[xz];
for (int i = 0; i < 10; i++) {
if (djg == wei && i == 0) {
bb /= 10;
continue;
}
if (bb % 10 < cnt[i]) {
long long xxx = (md - i * p10[djg - 1]) % m;
while (xxx < 0) xxx += m;
ans += dfs(djg - 1, num + p10m[i], xxx);
}
bb /= 10;
}
return mm[xz] = ans;
}
int main() {
n = READ();
m = read();
p10[0] = p10m[0] = 1;
for (int i = 1; i < 19; i++) {
p10[i] = p10[i - 1] * 10 % m;
p10m[i] = p10m[i - 1] * 10;
}
while (n) {
cnt[n % 10]++;
wei++;
n /= 10;
}
cout << dfs(wei, 0, 0) << endl;
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int num = 0;
char c = getchar();
if (c == '-') return -read();
while (c >= '0' && c <= '9') {
num = (num << 3) + (num << 1) + c - '0';
c = getchar();
}
return num;
}
inline long long READ() {
long long num = 0;
char c = getchar();
if (c == '-') return -READ();
while (c >= '0' && c <= '9') {
num = (num << 3) + (num << 1) + c - '0';
c = getchar();
}
return num;
}
long long n;
int m;
int cnt[10];
long long p10[19], p10m[19];
int wei;
unordered_map<long long, long long> mm;
long long dfs(long long djg, long long num, long long md) {
if (!djg) return md == 0;
long long xz = num * 100 + md, bb = num, ans = 0;
if (mm.count(xz)) return mm[xz];
for (int i = 0; i < 10; i++) {
if (djg == wei && i == 0) {
bb /= 10;
continue;
}
if (bb % 10 < cnt[i]) {
long long xxx = (md - i * p10[djg - 1]) % m;
while (xxx < 0) xxx += m;
ans += dfs(djg - 1, num + p10m[i], xxx);
}
bb /= 10;
}
return mm[xz] = ans;
}
int main() {
n = READ();
m = read();
p10[0] = p10m[0] = 1;
for (int i = 1; i < 19; i++) {
p10[i] = p10[i - 1] * 10 % m;
p10m[i] = p10m[i - 1] * 10;
}
while (n) {
cnt[n % 10]++;
wei++;
n /= 10;
}
cout << dfs(wei, 0, 0) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MASK = (1 << 18) + 15, M = 128;
long long dp[MASK][M];
long long fact[M], cnt[M];
int m;
string a;
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cerr << "HELLO WORLD :)\n";
cin >> a >> m;
fact[0] = 1;
for (auto u : a) cnt[u - '0']++;
for (int i = 1; i <= 18; i++) fact[i] = i * fact[i - 1];
int len = (int)a.size();
dp[0][0] = 1;
for (int mask = 0; mask < (1 << len); mask++)
for (int j = 0; j < m; j++) {
bool flag = 0;
for (int i = 0; i < len; i++)
if ((mask >> i & 1) && (a[i] - '0')) flag = 1;
if (!flag && mask) continue;
for (int i = 0; i < len; i++)
if (!(mask >> i & 1))
dp[mask ^ (1 << i)][(10 * j + a[i] - '0') % m] += dp[mask][j];
}
long long ans = dp[(1 << len) - 1][0];
for (int i = 0; i < 10; i++) ans /= fact[cnt[i]];
return cout << ans << '\n', false;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MASK = (1 << 18) + 15, M = 128;
long long dp[MASK][M];
long long fact[M], cnt[M];
int m;
string a;
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cerr << "HELLO WORLD :)\n";
cin >> a >> m;
fact[0] = 1;
for (auto u : a) cnt[u - '0']++;
for (int i = 1; i <= 18; i++) fact[i] = i * fact[i - 1];
int len = (int)a.size();
dp[0][0] = 1;
for (int mask = 0; mask < (1 << len); mask++)
for (int j = 0; j < m; j++) {
bool flag = 0;
for (int i = 0; i < len; i++)
if ((mask >> i & 1) && (a[i] - '0')) flag = 1;
if (!flag && mask) continue;
for (int i = 0; i < len; i++)
if (!(mask >> i & 1))
dp[mask ^ (1 << i)][(10 * j + a[i] - '0') % m] += dp[mask][j];
}
long long ans = dp[(1 << len) - 1][0];
for (int i = 0; i < 10; i++) ans /= fact[cnt[i]];
return cout << ans << '\n', false;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 10;
const int M = 3e5 + 100;
const long long int mod = 1e9 + 7;
const long long int inf = 1e16;
long long int pw[22], dp[M][N], a[22];
bool mark[10];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long int k;
int m;
cin >> k >> m;
int n = 0;
while (k != 0) {
a[n++] = k % 10;
k /= 10;
}
pw[0] = 1;
for (int i = 1; i < 20; i++) pw[i] = (pw[i - 1] * 10) % m;
dp[0][0] = 1;
for (int mask = 1; mask < (1 << n); mask++) {
for (int r = 0; r < m; r++) {
int bi = __builtin_popcount(mask);
for (int i = 0; i < 10; i++) mark[i] = false;
for (int i = 0; i < n; i++) {
if ((mask >> i) & 1 && (mask != (1 << n) - 1 || a[i] != 0) &&
!mark[a[i]]) {
mark[a[i]] = true;
int mask2 = mask ^ (1 << i);
int r2 = (r - (pw[bi - 1] * a[i]) % m + m) % m;
dp[mask][r] += dp[mask2][r2];
}
}
}
}
cout << dp[(1 << n) - 1][0] << '\n';
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 10;
const int M = 3e5 + 100;
const long long int mod = 1e9 + 7;
const long long int inf = 1e16;
long long int pw[22], dp[M][N], a[22];
bool mark[10];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long int k;
int m;
cin >> k >> m;
int n = 0;
while (k != 0) {
a[n++] = k % 10;
k /= 10;
}
pw[0] = 1;
for (int i = 1; i < 20; i++) pw[i] = (pw[i - 1] * 10) % m;
dp[0][0] = 1;
for (int mask = 1; mask < (1 << n); mask++) {
for (int r = 0; r < m; r++) {
int bi = __builtin_popcount(mask);
for (int i = 0; i < 10; i++) mark[i] = false;
for (int i = 0; i < n; i++) {
if ((mask >> i) & 1 && (mask != (1 << n) - 1 || a[i] != 0) &&
!mark[a[i]]) {
mark[a[i]] = true;
int mask2 = mask ^ (1 << i);
int r2 = (r - (pw[bi - 1] * a[i]) % m + m) % m;
dp[mask][r] += dp[mask2][r2];
}
}
}
}
cout << dp[(1 << n) - 1][0] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int len;
int mo;
string n;
long long dp[1 << 18][100];
long long coun[10];
int fastmod[10004];
void pre() {
for (int i = (1); i <= (10004 - 1); ++i) {
fastmod[i] = i % mo;
}
}
long long solve(int mask, int m) {
if (mask == (1 << len) - 1) return m == 0;
if (dp[mask][m] != -1) return dp[mask][m];
long long ans = 0;
for (int j = (0); j <= (len - 1); ++j) {
if ((mask & (1 << j))) continue;
ans += solve(mask | (1 << j), fastmod[m * 10 + (n[j] - '0')]);
}
return dp[mask][m] = ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
;
cin >> n >> mo;
len = n.size();
pre();
memset(dp, -1, sizeof(dp));
long long ans = 0;
for (int i = (0); i <= (len - 1); ++i) {
coun[n[i] - '0']++;
if (n[i] - '0' > 0) ans += solve(1 << i, (n[i] - '0') % mo);
}
for (int i = (0); i <= (9); ++i) {
for (int j = (1); j <= (coun[i]); ++j) ans /= j;
}
cout << ans;
cout.flush();
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int len;
int mo;
string n;
long long dp[1 << 18][100];
long long coun[10];
int fastmod[10004];
void pre() {
for (int i = (1); i <= (10004 - 1); ++i) {
fastmod[i] = i % mo;
}
}
long long solve(int mask, int m) {
if (mask == (1 << len) - 1) return m == 0;
if (dp[mask][m] != -1) return dp[mask][m];
long long ans = 0;
for (int j = (0); j <= (len - 1); ++j) {
if ((mask & (1 << j))) continue;
ans += solve(mask | (1 << j), fastmod[m * 10 + (n[j] - '0')]);
}
return dp[mask][m] = ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
;
cin >> n >> mo;
len = n.size();
pre();
memset(dp, -1, sizeof(dp));
long long ans = 0;
for (int i = (0); i <= (len - 1); ++i) {
coun[n[i] - '0']++;
if (n[i] - '0' > 0) ans += solve(1 << i, (n[i] - '0') % mo);
}
for (int i = (0); i <= (9); ++i) {
for (int j = (1); j <= (coun[i]); ++j) ans /= j;
}
cout << ans;
cout.flush();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
string s;
int n, m;
long long dp[1 << 18][105];
long long power(int x, int p) {
if (p == 0) return 1;
return x * power(x, p - 1);
}
long long getCount(int mask, int mod, int c) {
if (mask == (1 << n) - 1)
if (mod == 0)
return 1;
else
return 0;
long long &ret = dp[mask][mod];
if (ret != -1) return ret;
ret = 0;
int f[10] = {0};
for (int i = 0; i < n; i++) {
if (mask == 0 && s[i] == '0') continue;
if (((mask >> i) & 1) == 0 && !f[s[i] - '0'])
ret += getCount(mask | (1 << i), (mod + (s[i] - '0') * power(10, c)) % m,
c - 1),
f[s[i] - '0'] = 1;
}
return ret;
}
int main() {
cin >> s >> m;
n = s.length();
memset(dp, -1, sizeof dp);
cout << getCount(0, 0, n - 1) << endl;
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string s;
int n, m;
long long dp[1 << 18][105];
long long power(int x, int p) {
if (p == 0) return 1;
return x * power(x, p - 1);
}
long long getCount(int mask, int mod, int c) {
if (mask == (1 << n) - 1)
if (mod == 0)
return 1;
else
return 0;
long long &ret = dp[mask][mod];
if (ret != -1) return ret;
ret = 0;
int f[10] = {0};
for (int i = 0; i < n; i++) {
if (mask == 0 && s[i] == '0') continue;
if (((mask >> i) & 1) == 0 && !f[s[i] - '0'])
ret += getCount(mask | (1 << i), (mod + (s[i] - '0') * power(10, c)) % m,
c - 1),
f[s[i] - '0'] = 1;
}
return ret;
}
int main() {
cin >> s >> m;
n = s.length();
memset(dp, -1, sizeof dp);
cout << getCount(0, 0, n - 1) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long dp[1 << 18][100];
int co[1 << 18];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
long long m;
cin >> m;
long long n = s.size();
long long c[10];
memset(c, 0, sizeof c);
for (long long i = 0; i < n; i++) c[s[i] - '0']++;
vector<long long> v[1 << n];
for (long long i = 0; i < (1 << n); i++) {
int r = 0;
for (long long j = 0; j < n; j++) {
if (i & (1 << j)) {
r++;
} else
v[i].push_back(j);
}
co[i] = r;
}
dp[(1 << n) - 1][0] = 1;
long long pw[20];
pw[0] = 1 % m;
int e[20];
int r = -1;
for (int i = 0; i < n; i++) {
int r = 0;
for (int j = 0; j <= 9; j++) {
r += c[j];
if (i < r) {
e[i] = j;
break;
}
}
}
for (long long i = 1; i < 20; i++) {
pw[i] = (pw[i - 1] * 10) % m;
}
for (long long mask = (1 << n) - 1; mask >= 0; mask--) {
for (long long mod = 0; mod < m; mod++) {
int now = e[co[mask]];
for (int j = 0; j < v[mask].size(); j++) {
if (now == 0 && v[mask][j] == n - 1) continue;
int notun = (mod + pw[v[mask][j]] * now) % m;
dp[mask][mod] += dp[mask | (1 << v[mask][j])][notun];
}
}
}
for (long long i = 0; i < 10; i++) {
long long r = 1;
for (long long j = 1; j <= c[i]; j++) r *= j;
dp[0][0] /= r;
}
cout << dp[0][0];
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long dp[1 << 18][100];
int co[1 << 18];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
long long m;
cin >> m;
long long n = s.size();
long long c[10];
memset(c, 0, sizeof c);
for (long long i = 0; i < n; i++) c[s[i] - '0']++;
vector<long long> v[1 << n];
for (long long i = 0; i < (1 << n); i++) {
int r = 0;
for (long long j = 0; j < n; j++) {
if (i & (1 << j)) {
r++;
} else
v[i].push_back(j);
}
co[i] = r;
}
dp[(1 << n) - 1][0] = 1;
long long pw[20];
pw[0] = 1 % m;
int e[20];
int r = -1;
for (int i = 0; i < n; i++) {
int r = 0;
for (int j = 0; j <= 9; j++) {
r += c[j];
if (i < r) {
e[i] = j;
break;
}
}
}
for (long long i = 1; i < 20; i++) {
pw[i] = (pw[i - 1] * 10) % m;
}
for (long long mask = (1 << n) - 1; mask >= 0; mask--) {
for (long long mod = 0; mod < m; mod++) {
int now = e[co[mask]];
for (int j = 0; j < v[mask].size(); j++) {
if (now == 0 && v[mask][j] == n - 1) continue;
int notun = (mod + pw[v[mask][j]] * now) % m;
dp[mask][mod] += dp[mask | (1 << v[mask][j])][notun];
}
}
}
for (long long i = 0; i < 10; i++) {
long long r = 1;
for (long long j = 1; j <= c[i]; j++) r *= j;
dp[0][0] /= r;
}
cout << dp[0][0];
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long setBit(long long num, long long i) { return num | (1 << i); }
long long unsetBit(long long num, long long i) {
num ^= 1 << i;
return num;
}
bool checkBit(long long num, long long i) {
if (num & 1 << i)
return true;
else
return false;
}
long long bp(long long num) { return __builtin_popcount(num); }
long long giaithua(long long m) {
if (m == 1) return 1;
return m * giaithua(m - 1);
}
long long k, n, m, a[20], count1 = 0, dp[300000][105], used[105];
int main() {
cin >> n >> m;
k = n;
memset(used, 0, sizeof(used));
while (k != 0) {
a[count1] = k % 10;
used[a[count1]]++;
k = k / 10;
count1++;
}
memset(dp, 0, sizeof(dp));
for (int i = 0; i < count1; i++) {
if (a[i] != 0) {
dp[setBit(0, i)][a[i] % m] = 1;
} else {
dp[setBit(0, i)][a[i] % m] = 0;
}
}
int k = 1 << count1;
for (int i = 0; i < k; i++) {
for (int ij = 0; ij < m; ij++) {
if (dp[i][ij]) {
for (int j = 0; j < count1; j++) {
if (checkBit(i, j) == 0) {
dp[setBit(i, j)][(ij * 10 + a[j]) % m] += dp[i][ij];
}
}
}
}
}
for (int i = 0; i <= 9; i++) {
if (used[i] != 0) dp[k - 1][0] /= giaithua(used[i]);
}
cout << dp[k - 1][0];
}
|
### Prompt
Generate a CPP solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long setBit(long long num, long long i) { return num | (1 << i); }
long long unsetBit(long long num, long long i) {
num ^= 1 << i;
return num;
}
bool checkBit(long long num, long long i) {
if (num & 1 << i)
return true;
else
return false;
}
long long bp(long long num) { return __builtin_popcount(num); }
long long giaithua(long long m) {
if (m == 1) return 1;
return m * giaithua(m - 1);
}
long long k, n, m, a[20], count1 = 0, dp[300000][105], used[105];
int main() {
cin >> n >> m;
k = n;
memset(used, 0, sizeof(used));
while (k != 0) {
a[count1] = k % 10;
used[a[count1]]++;
k = k / 10;
count1++;
}
memset(dp, 0, sizeof(dp));
for (int i = 0; i < count1; i++) {
if (a[i] != 0) {
dp[setBit(0, i)][a[i] % m] = 1;
} else {
dp[setBit(0, i)][a[i] % m] = 0;
}
}
int k = 1 << count1;
for (int i = 0; i < k; i++) {
for (int ij = 0; ij < m; ij++) {
if (dp[i][ij]) {
for (int j = 0; j < count1; j++) {
if (checkBit(i, j) == 0) {
dp[setBit(i, j)][(ij * 10 + a[j]) % m] += dp[i][ij];
}
}
}
}
}
for (int i = 0; i <= 9; i++) {
if (used[i] != 0) dp[k - 1][0] /= giaithua(used[i]);
}
cout << dp[k - 1][0];
}
```
|
#include <bits/stdc++.h>
using namespace std;
char s[20];
int m;
long long dp[1 << 18][110];
int cnt[20];
int main() {
ios::sync_with_stdio(false);
cin >> s >> m;
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
dp[0][0] = 1;
int l = strlen(s);
long long x = 1 << l, d = 1;
for (int i = 0; i < l; i++) d *= ++cnt[s[i] - '0'];
for (int i = 0; i < x; i++) {
for (int j = 0; j < l; j++) {
if (!(i & (1 << j)) && (i || s[j] - '0')) {
for (int k = 0; k < m; k++) {
if (dp[i][k])
dp[i | (1 << j)][(k * 10 + (s[j] - '0')) % m] += dp[i][k];
}
}
}
}
cout << dp[x - 1][0] / d << endl;
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[20];
int m;
long long dp[1 << 18][110];
int cnt[20];
int main() {
ios::sync_with_stdio(false);
cin >> s >> m;
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
dp[0][0] = 1;
int l = strlen(s);
long long x = 1 << l, d = 1;
for (int i = 0; i < l; i++) d *= ++cnt[s[i] - '0'];
for (int i = 0; i < x; i++) {
for (int j = 0; j < l; j++) {
if (!(i & (1 << j)) && (i || s[j] - '0')) {
for (int k = 0; k < m; k++) {
if (dp[i][k])
dp[i | (1 << j)][(k * 10 + (s[j] - '0')) % m] += dp[i][k];
}
}
}
}
cout << dp[x - 1][0] / d << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long v;
int n, m;
long long dp[1 << 18][100];
int num[100], cnt[100];
int N;
void solve() {
N = 1 << n;
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
if (num[i] > 0) dp[1 << i][num[i] % m] += 1;
}
for (int i = 1; i < N; i++) {
for (int j = 0; j < m; j++) {
if (dp[i][j] == 0) continue;
for (int k = 0; k < n; k++) {
if ((i & (1 << k)) > 0) continue;
dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j];
}
}
}
long long ans = dp[N - 1][0];
for (int i = 0; i <= 9; i++) {
for (int j = 1; j <= cnt[i]; j++) {
ans /= j;
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin >> v >> m;
n = 0;
while (v) {
num[n++] = v % 10;
cnt[v % 10]++;
v /= 10;
}
solve();
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long v;
int n, m;
long long dp[1 << 18][100];
int num[100], cnt[100];
int N;
void solve() {
N = 1 << n;
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
if (num[i] > 0) dp[1 << i][num[i] % m] += 1;
}
for (int i = 1; i < N; i++) {
for (int j = 0; j < m; j++) {
if (dp[i][j] == 0) continue;
for (int k = 0; k < n; k++) {
if ((i & (1 << k)) > 0) continue;
dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j];
}
}
}
long long ans = dp[N - 1][0];
for (int i = 0; i <= 9; i++) {
for (int j = 1; j <= cnt[i]; j++) {
ans /= j;
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin >> v >> m;
n = 0;
while (v) {
num[n++] = v % 10;
cnt[v % 10]++;
v /= 10;
}
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 15;
const int inf = 1e9 + 7;
const long long MOD = 1e9 + 7;
double eps = 1e-9;
long long f[20];
int digit[20];
int a[20];
long long dp[1 << 18][105];
int m;
void init() {
f[0] = 1;
for (int i = 1; i <= 18; i++) {
f[i] = f[i - 1] * i;
}
memset(dp, -1, sizeof(dp));
}
long long dfs(int cur, int mod, int len) {
if (dp[cur][mod] != -1) return dp[cur][mod];
if (cur == 0) {
if (mod == 0)
return 1;
else
return 0;
}
long long res = 0;
bool vis[10];
memset(vis, 0, sizeof(vis));
for (int i = 0; i < len; i++) {
if (cur & (1 << i)) {
if (vis[digit[len - i]]) continue;
vis[digit[len - i]] = true;
res +=
dfs(cur ^ (1 << i), ((10 * mod - digit[len - i]) % m + m) % m, len);
}
}
dp[cur][mod] = res;
return res;
}
int main() {
init();
long long n;
cin >> n >> m;
int len = 0;
while (n) {
len++;
digit[len] = n % 10;
a[n % 10]++;
n /= 10;
}
long long ans = 0;
dp[0][0] = 1;
bool vis[10];
memset(vis, 0, sizeof(vis));
for (int i = 0; i < len; i++) {
if (digit[len - i] && vis[digit[len - i]] == false) {
vis[digit[len - i]] = true;
ans += dfs(((1 << len) - 1) ^ (1 << i), ((-digit[len - i]) % m + m) % m,
len);
}
}
printf("%lld\n", ans);
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 15;
const int inf = 1e9 + 7;
const long long MOD = 1e9 + 7;
double eps = 1e-9;
long long f[20];
int digit[20];
int a[20];
long long dp[1 << 18][105];
int m;
void init() {
f[0] = 1;
for (int i = 1; i <= 18; i++) {
f[i] = f[i - 1] * i;
}
memset(dp, -1, sizeof(dp));
}
long long dfs(int cur, int mod, int len) {
if (dp[cur][mod] != -1) return dp[cur][mod];
if (cur == 0) {
if (mod == 0)
return 1;
else
return 0;
}
long long res = 0;
bool vis[10];
memset(vis, 0, sizeof(vis));
for (int i = 0; i < len; i++) {
if (cur & (1 << i)) {
if (vis[digit[len - i]]) continue;
vis[digit[len - i]] = true;
res +=
dfs(cur ^ (1 << i), ((10 * mod - digit[len - i]) % m + m) % m, len);
}
}
dp[cur][mod] = res;
return res;
}
int main() {
init();
long long n;
cin >> n >> m;
int len = 0;
while (n) {
len++;
digit[len] = n % 10;
a[n % 10]++;
n /= 10;
}
long long ans = 0;
dp[0][0] = 1;
bool vis[10];
memset(vis, 0, sizeof(vis));
for (int i = 0; i < len; i++) {
if (digit[len - i] && vis[digit[len - i]] == false) {
vis[digit[len - i]] = true;
ans += dfs(((1 << len) - 1) ^ (1 << i), ((-digit[len - i]) % m + m) % m,
len);
}
}
printf("%lld\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long n, x, dp[1 << 18][100];
int m, p[20], len;
long long dfs(int pos, int status, bool zero, int rev) {
if (!pos) return rev == 0;
if (!zero && dp[status][rev] != -1) return dp[status][rev];
long long ans = 0;
int f[10];
memset(f, 0, sizeof(f));
for (int i = 1; i <= len; i++)
if (!((status >> i - 1) & 1)) {
if (zero && p[i] == 0) continue;
if (f[p[i]]) continue;
ans += dfs(pos - 1, status | (1 << i - 1), zero && p[i] == 0,
(rev * 10 + p[i]) % m);
f[p[i]] = 1;
}
if (!zero) dp[status][rev] = ans;
return ans;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
memset(dp, -1, sizeof(dp));
cin >> n >> m;
x = n;
while (x) {
p[++len] = x % 10;
x /= 10;
}
long long ans = dfs(len, 0, true, 0);
cout << ans;
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long n, x, dp[1 << 18][100];
int m, p[20], len;
long long dfs(int pos, int status, bool zero, int rev) {
if (!pos) return rev == 0;
if (!zero && dp[status][rev] != -1) return dp[status][rev];
long long ans = 0;
int f[10];
memset(f, 0, sizeof(f));
for (int i = 1; i <= len; i++)
if (!((status >> i - 1) & 1)) {
if (zero && p[i] == 0) continue;
if (f[p[i]]) continue;
ans += dfs(pos - 1, status | (1 << i - 1), zero && p[i] == 0,
(rev * 10 + p[i]) % m);
f[p[i]] = 1;
}
if (!zero) dp[status][rev] = ans;
return ans;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
memset(dp, -1, sizeof(dp));
cin >> n >> m;
x = n;
while (x) {
p[++len] = x % 10;
x /= 10;
}
long long ans = dfs(len, 0, true, 0);
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
map<pair<vector<int>, int>, long long> ma;
long long dp[(1 << 18)][101];
int main() {
srand((unsigned int)time(NULL));
long long n, m;
int dig[20], id = 0, ten[20], cnt[10] = {};
ten[0] = 1;
cin >> n >> m;
for (int i = 1; i <= 19; i++) ten[i] = (ten[i - 1] * 10) % m;
while (n) {
cnt[n % 10]++;
dig[id++] = n % 10;
n /= 10;
}
dp[0][0] = 1LL;
for (int mask = 0; mask < (1 << id); mask++) {
for (int i = 0; i < id; i++) {
if (!mask && !dig[i]) continue;
if (((mask >> i) & 1)) continue;
for (int rem = 0; rem < m; rem++) {
if (!dp[mask][rem]) continue;
dp[mask ^ (1 << i)][(rem * 10 + dig[i]) % m] += dp[mask][rem];
}
}
}
for (int i = 0; i <= 9; i++)
for (int j = 1; j <= cnt[i]; j++) dp[(1 << id) - 1][0] /= j;
cout << dp[(1 << id) - 1][0] << endl;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
map<pair<vector<int>, int>, long long> ma;
long long dp[(1 << 18)][101];
int main() {
srand((unsigned int)time(NULL));
long long n, m;
int dig[20], id = 0, ten[20], cnt[10] = {};
ten[0] = 1;
cin >> n >> m;
for (int i = 1; i <= 19; i++) ten[i] = (ten[i - 1] * 10) % m;
while (n) {
cnt[n % 10]++;
dig[id++] = n % 10;
n /= 10;
}
dp[0][0] = 1LL;
for (int mask = 0; mask < (1 << id); mask++) {
for (int i = 0; i < id; i++) {
if (!mask && !dig[i]) continue;
if (((mask >> i) & 1)) continue;
for (int rem = 0; rem < m; rem++) {
if (!dp[mask][rem]) continue;
dp[mask ^ (1 << i)][(rem * 10 + dig[i]) % m] += dp[mask][rem];
}
}
}
for (int i = 0; i <= 9; i++)
for (int j = 1; j <= cnt[i]; j++) dp[(1 << id) - 1][0] /= j;
cout << dp[(1 << id) - 1][0] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long n;
int m;
long long dp[1 << 18][101];
int num[20];
int c[10];
int digitLen;
long long Fac(int x) { return x == 0 ? 1 : Fac(x - 1) * x; }
int main() {
cin >> n >> m;
while (n) {
int t = (int)(n % 10);
num[digitLen++] = t;
c[t]++;
n /= 10;
}
long long div = 1;
for (int i = 0; i < 10; ++i) div *= Fac(c[i]);
dp[0][0] = 1;
for (int i = 0; i < (1 << digitLen); ++i) {
for (int j = 0; j < m; ++j) {
if (dp[i][j] == 0) continue;
for (int k = 0; k < digitLen; ++k) {
if (1 & (i >> k)) continue;
if (i || num[k]) {
int mask = i | (1 << k), mod = (j * 10 + num[k]) % m;
dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j];
}
}
}
}
printf("%I64d\n", dp[(1 << digitLen) - 1][0] / div);
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n;
int m;
long long dp[1 << 18][101];
int num[20];
int c[10];
int digitLen;
long long Fac(int x) { return x == 0 ? 1 : Fac(x - 1) * x; }
int main() {
cin >> n >> m;
while (n) {
int t = (int)(n % 10);
num[digitLen++] = t;
c[t]++;
n /= 10;
}
long long div = 1;
for (int i = 0; i < 10; ++i) div *= Fac(c[i]);
dp[0][0] = 1;
for (int i = 0; i < (1 << digitLen); ++i) {
for (int j = 0; j < m; ++j) {
if (dp[i][j] == 0) continue;
for (int k = 0; k < digitLen; ++k) {
if (1 & (i >> k)) continue;
if (i || num[k]) {
int mask = i | (1 << k), mod = (j * 10 + num[k]) % m;
dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j];
}
}
}
}
printf("%I64d\n", dp[(1 << digitLen) - 1][0] / div);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
ifstream fin("A.in");
ofstream fout("A.out");
long long n;
int m, t;
int a[20];
bool upd[10];
long long dp[300000][101];
int main() {
cin >> n >> m;
while (n) {
a[t++] = n % 10;
n /= 10;
}
sort(a, a + t);
for (int i = 0; i < t; ++i) {
if (a[i] && (i == 0 || a[i] != a[i - 1])) dp[(1 << i)][a[i] % m]++;
}
int nn = (1 << t);
for (int i = 1; i < nn; ++i) {
for (int j = 0; j < m; ++j) {
if (dp[i][j] == 0) continue;
memset(upd, 0, sizeof(upd));
for (int k = 0; k < t; ++k) {
if (((1 << k) | i) != i && !upd[a[k]]) {
dp[(1 << k) | i][(j * 10 + a[k]) % m] += dp[i][j];
upd[a[k]] = 1;
}
}
}
}
cout << dp[nn - 1][0];
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
ifstream fin("A.in");
ofstream fout("A.out");
long long n;
int m, t;
int a[20];
bool upd[10];
long long dp[300000][101];
int main() {
cin >> n >> m;
while (n) {
a[t++] = n % 10;
n /= 10;
}
sort(a, a + t);
for (int i = 0; i < t; ++i) {
if (a[i] && (i == 0 || a[i] != a[i - 1])) dp[(1 << i)][a[i] % m]++;
}
int nn = (1 << t);
for (int i = 1; i < nn; ++i) {
for (int j = 0; j < m; ++j) {
if (dp[i][j] == 0) continue;
memset(upd, 0, sizeof(upd));
for (int k = 0; k < t; ++k) {
if (((1 << k) | i) != i && !upd[a[k]]) {
dp[(1 << k) | i][(j * 10 + a[k]) % m] += dp[i][j];
upd[a[k]] = 1;
}
}
}
}
cout << dp[nn - 1][0];
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s;
long long mem[(1 << 18)][105];
long long DP(int cur, int rem) {
if (cur == (1 << n) - 1) return rem == 0;
if (mem[cur][rem] == -1) {
long long ans = 0;
for (int i = 0; i < n; i++) {
if (!(cur & (1 << i))) {
ans += DP(cur | (1 << i), ((rem * 10) + (s[i] - '0')) % m);
}
}
mem[cur][rem] = ans;
}
return mem[cur][rem];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long tmp;
memset(mem, -1, sizeof mem);
cin >> s >> m;
n = s.length();
map<char, long long> mp;
for (char c : s) mp[c]++;
long long ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') continue;
ans += DP(1 << i, (s[i] - '0') % m);
}
for (auto it : mp) {
for (long long i = 1; i <= it.second; i++) ans /= i;
}
cout << ans;
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s;
long long mem[(1 << 18)][105];
long long DP(int cur, int rem) {
if (cur == (1 << n) - 1) return rem == 0;
if (mem[cur][rem] == -1) {
long long ans = 0;
for (int i = 0; i < n; i++) {
if (!(cur & (1 << i))) {
ans += DP(cur | (1 << i), ((rem * 10) + (s[i] - '0')) % m);
}
}
mem[cur][rem] = ans;
}
return mem[cur][rem];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long tmp;
memset(mem, -1, sizeof mem);
cin >> s >> m;
n = s.length();
map<char, long long> mp;
for (char c : s) mp[c]++;
long long ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') continue;
ans += DP(1 << i, (s[i] - '0') % m);
}
for (auto it : mp) {
for (long long i = 1; i <= it.second; i++) ans /= i;
}
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
string num;
long long dp[1 << 18][101];
long long mx;
int sz, m;
long long solve(long long mask, int md) {
if (mask == mx) {
if (md % m == 0) return 1;
return 0;
}
long long &ret = dp[mask][md];
if (ret != -1) return ret;
ret = 0;
bool vis[10];
memset(vis, 0, sizeof vis);
md = md * 10;
for (int i = 0; i < sz; i++) {
int cn = num[i] - '0';
if (mask == 0 && cn == 0) continue;
if (vis[cn]) continue;
if ((mask & (1 << i))) continue;
vis[cn] = true;
ret += solve((mask | (1 << i)), (md + cn) % m);
}
return ret;
}
int main() {
int i, j;
int a, b;
cin >> num >> m;
sz = num.size();
mx = (1 << sz) - 1;
memset(dp, -1, sizeof dp);
long long res = solve(0, 0);
cout << res << endl;
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string num;
long long dp[1 << 18][101];
long long mx;
int sz, m;
long long solve(long long mask, int md) {
if (mask == mx) {
if (md % m == 0) return 1;
return 0;
}
long long &ret = dp[mask][md];
if (ret != -1) return ret;
ret = 0;
bool vis[10];
memset(vis, 0, sizeof vis);
md = md * 10;
for (int i = 0; i < sz; i++) {
int cn = num[i] - '0';
if (mask == 0 && cn == 0) continue;
if (vis[cn]) continue;
if ((mask & (1 << i))) continue;
vis[cn] = true;
ret += solve((mask | (1 << i)), (md + cn) % m);
}
return ret;
}
int main() {
int i, j;
int a, b;
cin >> num >> m;
sz = num.size();
mx = (1 << sz) - 1;
memset(dp, -1, sizeof dp);
long long res = solve(0, 0);
cout << res << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool Check(long long n, long long pos) { return (n & (1 << pos)); }
long long Set(long long n, long long pos) { return (n | (1 << pos)); }
string s;
long long n, mod;
long long dp[(1 << 18) + 2][102];
long long call(long long mask, long long val) {
if (mask == (1 << n) - 1) {
if (val)
return 0;
else
return 1;
}
long long &ret = dp[mask][val];
if (ret != -1) return ret;
ret = 0;
int mp[11] = {0};
for (int i = 0; i < n; i++) {
int digit = s[i] - '0';
if (mp[digit] || (!digit && !mask) || Check(mask, i)) continue;
mp[digit] = 1;
ret += call(Set(mask, i), (val * 10 + digit) % mod);
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s >> mod;
n = s.size();
memset(dp, -1, sizeof dp);
cout << call(0, 0) << endl;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool Check(long long n, long long pos) { return (n & (1 << pos)); }
long long Set(long long n, long long pos) { return (n | (1 << pos)); }
string s;
long long n, mod;
long long dp[(1 << 18) + 2][102];
long long call(long long mask, long long val) {
if (mask == (1 << n) - 1) {
if (val)
return 0;
else
return 1;
}
long long &ret = dp[mask][val];
if (ret != -1) return ret;
ret = 0;
int mp[11] = {0};
for (int i = 0; i < n; i++) {
int digit = s[i] - '0';
if (mp[digit] || (!digit && !mask) || Check(mask, i)) continue;
mp[digit] = 1;
ret += call(Set(mask, i), (val * 10 + digit) % mod);
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s >> mod;
n = s.size();
memset(dp, -1, sizeof dp);
cout << call(0, 0) << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 20;
long long n, m;
long long f[11];
vector<long long> v;
long long dp[1 << 18][100];
long long fact[N];
void precompute() {
fact[0] = fact[1] = 1;
for (long long i = 2; i < N; i++) {
fact[i] = fact[i - 1] * i;
}
}
void work(long long k) {
while (k > 0) {
v.push_back(k % 10);
f[k % 10]++;
k /= 10;
}
n = v.size();
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
precompute();
cin >> n >> m;
work(n);
for (long long rem = 0; rem < m; rem++) dp[(1 << n) - 1][rem] = (rem == 0);
for (long long mask = (1 << n) - 2; mask >= 0; mask--) {
for (long long rem = 0; rem < m; rem++) {
long long &ans = dp[mask][rem];
if (mask == 0) {
for (long long i = 0; i < n; i++) {
if (v[i] == 0 || (mask >> i) & 1) continue;
long long cur = v[i];
ans += dp[mask | (1 << i)][(rem * 10 + cur) % m];
}
} else {
for (long long i = 0; i < n; i++) {
if ((mask >> i) & 1) continue;
long long cur = v[i];
ans += dp[mask | (1 << i)][(rem * 10 + cur) % m];
}
}
}
}
long long ans = dp[0][0];
for (long long i = 0; i <= 9; i++) ans /= fact[f[i]];
cout << ans;
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 20;
long long n, m;
long long f[11];
vector<long long> v;
long long dp[1 << 18][100];
long long fact[N];
void precompute() {
fact[0] = fact[1] = 1;
for (long long i = 2; i < N; i++) {
fact[i] = fact[i - 1] * i;
}
}
void work(long long k) {
while (k > 0) {
v.push_back(k % 10);
f[k % 10]++;
k /= 10;
}
n = v.size();
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
precompute();
cin >> n >> m;
work(n);
for (long long rem = 0; rem < m; rem++) dp[(1 << n) - 1][rem] = (rem == 0);
for (long long mask = (1 << n) - 2; mask >= 0; mask--) {
for (long long rem = 0; rem < m; rem++) {
long long &ans = dp[mask][rem];
if (mask == 0) {
for (long long i = 0; i < n; i++) {
if (v[i] == 0 || (mask >> i) & 1) continue;
long long cur = v[i];
ans += dp[mask | (1 << i)][(rem * 10 + cur) % m];
}
} else {
for (long long i = 0; i < n; i++) {
if ((mask >> i) & 1) continue;
long long cur = v[i];
ans += dp[mask | (1 << i)][(rem * 10 + cur) % m];
}
}
}
}
long long ans = dp[0][0];
for (long long i = 0; i <= 9; i++) ans /= fact[f[i]];
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> p, g;
long long n, f[2][27000][105];
int m, cnt[10];
int toInt(vector<int>& state) {
int res = 0;
for (int i = 0; i < int(p.size()); i++) {
if (state[i] == 0) continue;
res += state[i] * g[i + 1];
}
return res;
}
vector<int> toState(int s) {
vector<int> res(int(p.size()), 0);
for (int i = 0; i < int(p.size()); i++) {
res[i] = s / g[i + 1];
s %= g[i + 1];
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
long long _n = n, digits = 0;
while (_n) {
cnt[_n % 10]++;
_n /= 10;
digits++;
}
for (int i = 0; i < 10; i++) {
if (cnt[i] == 0) continue;
p.push_back(i);
}
g.resize(int(p.size()) + 1, 0);
g[int(g.size()) - 1] = 1;
for (int i = int(g.size()) - 2; i >= 0; i--) {
g[i] = g[i + 1] * (cnt[p[i]] + 1);
}
int final = g[0] - 1;
f[0][0][0] = 1;
for (int i = 0; i <= digits - 1; i++) {
int cur = i & 1, ne = 1 - cur;
memset(f[ne], 0, sizeof f[ne]);
for (int j = 0; j < 27000; j++)
for (int k = 0; k < m; k++) {
if (f[cur][j][k] == 0) continue;
vector<int> state = toState(j);
for (int x = 0; x < int(p.size()); x++) {
int num = p[x];
if (i == 0 && num == 0) continue;
if (cnt[num] == state[x]) continue;
state[x]++;
int nextJ = toInt(state);
state[x]--;
int nextK = (k * 10 + num) % m;
f[ne][nextJ][nextK] += f[cur][j][k];
}
}
}
cout << f[digits & 1][final][0] << "\n";
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> p, g;
long long n, f[2][27000][105];
int m, cnt[10];
int toInt(vector<int>& state) {
int res = 0;
for (int i = 0; i < int(p.size()); i++) {
if (state[i] == 0) continue;
res += state[i] * g[i + 1];
}
return res;
}
vector<int> toState(int s) {
vector<int> res(int(p.size()), 0);
for (int i = 0; i < int(p.size()); i++) {
res[i] = s / g[i + 1];
s %= g[i + 1];
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
long long _n = n, digits = 0;
while (_n) {
cnt[_n % 10]++;
_n /= 10;
digits++;
}
for (int i = 0; i < 10; i++) {
if (cnt[i] == 0) continue;
p.push_back(i);
}
g.resize(int(p.size()) + 1, 0);
g[int(g.size()) - 1] = 1;
for (int i = int(g.size()) - 2; i >= 0; i--) {
g[i] = g[i + 1] * (cnt[p[i]] + 1);
}
int final = g[0] - 1;
f[0][0][0] = 1;
for (int i = 0; i <= digits - 1; i++) {
int cur = i & 1, ne = 1 - cur;
memset(f[ne], 0, sizeof f[ne]);
for (int j = 0; j < 27000; j++)
for (int k = 0; k < m; k++) {
if (f[cur][j][k] == 0) continue;
vector<int> state = toState(j);
for (int x = 0; x < int(p.size()); x++) {
int num = p[x];
if (i == 0 && num == 0) continue;
if (cnt[num] == state[x]) continue;
state[x]++;
int nextJ = toInt(state);
state[x]--;
int nextK = (k * 10 + num) % m;
f[ne][nextJ][nextK] += f[cur][j][k];
}
}
}
cout << f[digits & 1][final][0] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int dX[] = {0, 0, 1, -1};
int dY[] = {1, -1, 0, 0};
string alpha = "abcdefghijklmnopqrstuvwxyz";
int MOD = 1000000007;
const int N = 200100;
string s;
int m, arr[20], n;
long long dp[(1 << 18) + 5][100];
long long solve(int mask, int rem, bool f) {
if (mask == (1 << n) - 1) return (rem == 0);
long long& ret = dp[mask][rem];
if (~ret) return ret;
ret = 0;
bool taken[10] = {0};
for (int i = 0; i < n; ++i) {
if (arr[i] == 0 && !f) continue;
if (taken[arr[i]]) continue;
if ((1 << i) & mask) continue;
ret += solve(mask | (1 << i), (rem * 10 + arr[i]) % m, f || (arr[i] > 0));
taken[arr[i]] = 1;
}
return ret;
}
int main() {
cin >> s;
scanf("%d", &m);
n = (s.size());
for (int i = 0; i < n; ++i) arr[i] = s[i] - '0';
memset(dp, -1, sizeof(dp));
cout << solve(0, 0, 0) << endl;
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int dX[] = {0, 0, 1, -1};
int dY[] = {1, -1, 0, 0};
string alpha = "abcdefghijklmnopqrstuvwxyz";
int MOD = 1000000007;
const int N = 200100;
string s;
int m, arr[20], n;
long long dp[(1 << 18) + 5][100];
long long solve(int mask, int rem, bool f) {
if (mask == (1 << n) - 1) return (rem == 0);
long long& ret = dp[mask][rem];
if (~ret) return ret;
ret = 0;
bool taken[10] = {0};
for (int i = 0; i < n; ++i) {
if (arr[i] == 0 && !f) continue;
if (taken[arr[i]]) continue;
if ((1 << i) & mask) continue;
ret += solve(mask | (1 << i), (rem * 10 + arr[i]) % m, f || (arr[i] > 0));
taken[arr[i]] = 1;
}
return ret;
}
int main() {
cin >> s;
scanf("%d", &m);
n = (s.size());
for (int i = 0; i < n; ++i) arr[i] = s[i] - '0';
memset(dp, -1, sizeof(dp));
cout << solve(0, 0, 0) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool Check(long long n, long long pos) { return (n & (1 << pos)); }
long long Set(long long n, long long pos) { return (n | (1 << pos)); }
string s;
long long n, mod;
long long dp[(1 << 18) + 2][102];
long long call(long long mask, long long val) {
if (mask == (1 << n) - 1) {
if (val)
return 0;
else
return 1;
}
long long &ret = dp[mask][val];
if (ret != -1) return ret;
ret = 0;
unordered_map<int, bool> mp;
for (int i = 0; i < n; i++) {
int digit = s[i] - '0';
if (mp[digit] || (!digit && !mask) || Check(mask, i)) continue;
mp[digit] = 1;
ret += call(Set(mask, i), (val * 10 + digit) % mod);
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s >> mod;
n = s.size();
memset(dp, -1, sizeof dp);
cout << call(0, 0) << endl;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool Check(long long n, long long pos) { return (n & (1 << pos)); }
long long Set(long long n, long long pos) { return (n | (1 << pos)); }
string s;
long long n, mod;
long long dp[(1 << 18) + 2][102];
long long call(long long mask, long long val) {
if (mask == (1 << n) - 1) {
if (val)
return 0;
else
return 1;
}
long long &ret = dp[mask][val];
if (ret != -1) return ret;
ret = 0;
unordered_map<int, bool> mp;
for (int i = 0; i < n; i++) {
int digit = s[i] - '0';
if (mp[digit] || (!digit && !mask) || Check(mask, i)) continue;
mp[digit] = 1;
ret += call(Set(mask, i), (val * 10 + digit) % mod);
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s >> mod;
n = s.size();
memset(dp, -1, sizeof dp);
cout << call(0, 0) << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
string s;
int n, m;
long long dp[1 << 18][105];
long long power(int x, int p) {
if (p == 0) return 1;
return x * power(x, p - 1);
}
long long getCount(int mask, int mod) {
if (mask == (1 << n) - 1)
if (mod == 0)
return 1;
else
return 0;
long long &ret = dp[mask][mod];
if (ret != -1) return ret;
ret = 0;
int f[10] = {0};
for (int i = 0; i < n; i++) {
if (mask == 0 && s[i] == '0') continue;
if (((mask >> i) & 1) == 0 && !f[s[i] - '0'])
ret += getCount(mask | (1 << i), (mod * 10 + s[i] - '0') % m),
f[s[i] - '0'] = 1;
}
return ret;
}
int main() {
cin >> s >> m;
n = s.length();
memset(dp, -1, sizeof dp);
cout << getCount(0, 0) << endl;
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string s;
int n, m;
long long dp[1 << 18][105];
long long power(int x, int p) {
if (p == 0) return 1;
return x * power(x, p - 1);
}
long long getCount(int mask, int mod) {
if (mask == (1 << n) - 1)
if (mod == 0)
return 1;
else
return 0;
long long &ret = dp[mask][mod];
if (ret != -1) return ret;
ret = 0;
int f[10] = {0};
for (int i = 0; i < n; i++) {
if (mask == 0 && s[i] == '0') continue;
if (((mask >> i) & 1) == 0 && !f[s[i] - '0'])
ret += getCount(mask | (1 << i), (mod * 10 + s[i] - '0') % m),
f[s[i] - '0'] = 1;
}
return ret;
}
int main() {
cin >> s >> m;
n = s.length();
memset(dp, -1, sizeof dp);
cout << getCount(0, 0) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long lp, numb[20], u;
long long dp[300000][105];
long long dmc(long long msk, long long mod) {
if (msk + 1 == (1 << u) && !(mod % lp)) return 1;
if (msk + 1 == (1 << u)) return 0;
if (dp[msk][mod] != -1) return dp[msk][mod];
long long i, j, res = 0, ty = 0;
for (i = 0; i < u; i++) {
if ((ty & (1 << numb[i]))) continue;
if (!msk && !numb[i]) continue;
if ((msk & (1 << i))) continue;
res += dmc(msk | (1 << i), (mod * 10 + numb[i]) % lp);
ty |= (1 << numb[i]);
}
return dp[msk][mod] = res;
}
int main() {
long long i, j, k;
string temp;
cin >> temp >> lp;
u = temp.size();
for (i = 0; i < u; i++) numb[i] = temp[i] - '0';
sort(numb, numb + u);
memset(dp, -1, sizeof dp);
cout << dmc(0, 0);
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long lp, numb[20], u;
long long dp[300000][105];
long long dmc(long long msk, long long mod) {
if (msk + 1 == (1 << u) && !(mod % lp)) return 1;
if (msk + 1 == (1 << u)) return 0;
if (dp[msk][mod] != -1) return dp[msk][mod];
long long i, j, res = 0, ty = 0;
for (i = 0; i < u; i++) {
if ((ty & (1 << numb[i]))) continue;
if (!msk && !numb[i]) continue;
if ((msk & (1 << i))) continue;
res += dmc(msk | (1 << i), (mod * 10 + numb[i]) % lp);
ty |= (1 << numb[i]);
}
return dp[msk][mod] = res;
}
int main() {
long long i, j, k;
string temp;
cin >> temp >> lp;
u = temp.size();
for (i = 0; i < u; i++) numb[i] = temp[i] - '0';
sort(numb, numb + u);
memset(dp, -1, sizeof dp);
cout << dmc(0, 0);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void _fill_int(int* p, int val, int rep) {
int i;
for (i = 0; i < rep; i++) p[i] = val;
}
signed long long GETi() {
signed long long i;
scanf("%lld", &i);
return i;
}
signed long long N, M;
vector<signed long long> V;
signed long long powd[10][20];
signed long long dpdp[1 << 18][101];
void solve() {
int f, i, j, k, l, x, y;
cin >> N >> M;
signed long long a = N;
while (a) V.push_back(a % 10), a /= 10;
for (i = 0; i < 10; i++) {
powd[i][0] = i;
for (j = 0; j < 18; j++) powd[i][j + 1] = powd[i][j] * 10;
for (j = 0; j < 19; j++) powd[i][j] %= M;
}
dpdp[0][0] = 1;
for (int mask = 0; mask < 1 << V.size(); mask++) {
int num = __builtin_popcount(mask);
for (i = 0; i < V.size(); i++) {
if (mask & (1 << i)) continue;
if (i == V.size() - 1 && V[num] == 0) continue;
l = powd[V[num]][i];
for (j = 0; j < M; j++) {
dpdp[mask | (1 << i)][(j + l) % M] += dpdp[mask][j];
}
}
}
signed long long ret = dpdp[(1 << V.size()) - 1][0];
int num[10];
memset(num, 0, sizeof(num));
for (__typeof(V.begin()) it = V.begin(); it != V.end(); it++) num[*it]++;
for (i = 0; i < 10; i++)
if (num[i]) {
for (j = 0; j < num[i]; j++) ret /= (j + 1);
}
(void)printf("%lld\n", ret);
}
int main(int argc, char** argv) {
string s;
if (argc == 1) ios::sync_with_stdio(false);
for (int i = 1; i < argc; i++) s += argv[i], s += '\n';
for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin);
solve();
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.
Number x is considered close to number n modulo m, if:
* it can be obtained by rearranging the digits of number n,
* it doesn't have any leading zeroes,
* the remainder after dividing number x by m equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
Input
The first line contains two integers: n (1 β€ n < 1018) and m (1 β€ m β€ 100).
Output
In a single line print a single integer β the number of numbers close to number n modulo m.
Examples
Input
104 2
Output
3
Input
223 4
Output
1
Input
7067678 8
Output
47
Note
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void _fill_int(int* p, int val, int rep) {
int i;
for (i = 0; i < rep; i++) p[i] = val;
}
signed long long GETi() {
signed long long i;
scanf("%lld", &i);
return i;
}
signed long long N, M;
vector<signed long long> V;
signed long long powd[10][20];
signed long long dpdp[1 << 18][101];
void solve() {
int f, i, j, k, l, x, y;
cin >> N >> M;
signed long long a = N;
while (a) V.push_back(a % 10), a /= 10;
for (i = 0; i < 10; i++) {
powd[i][0] = i;
for (j = 0; j < 18; j++) powd[i][j + 1] = powd[i][j] * 10;
for (j = 0; j < 19; j++) powd[i][j] %= M;
}
dpdp[0][0] = 1;
for (int mask = 0; mask < 1 << V.size(); mask++) {
int num = __builtin_popcount(mask);
for (i = 0; i < V.size(); i++) {
if (mask & (1 << i)) continue;
if (i == V.size() - 1 && V[num] == 0) continue;
l = powd[V[num]][i];
for (j = 0; j < M; j++) {
dpdp[mask | (1 << i)][(j + l) % M] += dpdp[mask][j];
}
}
}
signed long long ret = dpdp[(1 << V.size()) - 1][0];
int num[10];
memset(num, 0, sizeof(num));
for (__typeof(V.begin()) it = V.begin(); it != V.end(); it++) num[*it]++;
for (i = 0; i < 10; i++)
if (num[i]) {
for (j = 0; j < num[i]; j++) ret /= (j + 1);
}
(void)printf("%lld\n", ret);
}
int main(int argc, char** argv) {
string s;
if (argc == 1) ios::sync_with_stdio(false);
for (int i = 1; i < argc; i++) s += argv[i], s += '\n';
for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin);
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
const int N = 3000000 + 7;
int n, m;
int l[N], r[N];
int e[N];
std::vector<std::pair<int, int>> g[N];
int t[N];
int ans[N];
void dfs(int s);
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d%d", l + i, r + i), e[i] = l[i], e[i + n] = ++r[i];
std::sort(e + 1, e + n * 2 + 1);
m = std::unique(e + 1, e + n * 2 + 1) - e - 1;
for (int i = 1; i <= n; ++i)
l[i] = std::lower_bound(e + 1, e + m + 1, l[i]) - e,
r[i] = std::lower_bound(e + 1, e + m + 1, r[i]) - e;
for (int i = 1; i <= n; ++i)
g[l[i]].push_back(std::pair<int, int>(r[i], i)),
g[r[i]].push_back(std::pair<int, int>(l[i], i)), ++t[l[i]], --t[r[i]];
for (int i = 1; i <= m; ++i)
if ((t[i] += t[i - 1]) & 1)
g[i].push_back(std::pair<int, int>(i + 1, n + i)),
g[i + 1].push_back(std::pair<int, int>(i, n + i));
for (int i = 1; i <= m; ++i) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", ans[i] + 1 >> 1);
return 0;
}
void dfs(int s) {
while (g[s].size() && ans[g[s].back().second]) g[s].pop_back();
if (g[s].empty()) return;
std::pair<int, int> t = g[s].back();
ans[t.second] = t.first > s ? 1 : -1;
dfs(t.first);
}
|
### Prompt
Generate a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
const int N = 3000000 + 7;
int n, m;
int l[N], r[N];
int e[N];
std::vector<std::pair<int, int>> g[N];
int t[N];
int ans[N];
void dfs(int s);
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d%d", l + i, r + i), e[i] = l[i], e[i + n] = ++r[i];
std::sort(e + 1, e + n * 2 + 1);
m = std::unique(e + 1, e + n * 2 + 1) - e - 1;
for (int i = 1; i <= n; ++i)
l[i] = std::lower_bound(e + 1, e + m + 1, l[i]) - e,
r[i] = std::lower_bound(e + 1, e + m + 1, r[i]) - e;
for (int i = 1; i <= n; ++i)
g[l[i]].push_back(std::pair<int, int>(r[i], i)),
g[r[i]].push_back(std::pair<int, int>(l[i], i)), ++t[l[i]], --t[r[i]];
for (int i = 1; i <= m; ++i)
if ((t[i] += t[i - 1]) & 1)
g[i].push_back(std::pair<int, int>(i + 1, n + i)),
g[i + 1].push_back(std::pair<int, int>(i, n + i));
for (int i = 1; i <= m; ++i) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", ans[i] + 1 >> 1);
return 0;
}
void dfs(int s) {
while (g[s].size() && ans[g[s].back().second]) g[s].pop_back();
if (g[s].empty()) return;
std::pair<int, int> t = g[s].back();
ans[t.second] = t.first > s ? 1 : -1;
dfs(t.first);
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int get() {
char c;
while (!isdigit(c = getchar()))
;
int v = c - 48;
while (isdigit(c = getchar())) v = v * 10 + c - 48;
return v;
}
int n;
pair<int, int> a[200005];
bool vis[200005], c[200005];
vector<int> G[200005];
void dfs(int x, int col) {
vis[x] = 1;
c[x] = col;
for (int i = 0; i < G[x].size(); ++i)
if (!vis[G[x][i]]) dfs(G[x][i], col ^ 1);
}
int main() {
n = get();
for (int i = 1, l, r; i <= n; ++i) {
l = get();
r = get();
a[i * 2 - 1] = make_pair(2 * l, 2 * i - 1);
a[i * 2] = make_pair(2 * r + 1, 2 * i);
G[2 * i - 1].push_back(2 * i);
G[2 * i].push_back(2 * i - 1);
}
sort(a + 1, a + 2 * n + 1);
for (int i = 1, u, v; i <= n; ++i) {
u = a[2 * i - 1].second;
v = a[2 * i].second;
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= 2 * n; ++i)
if (!vis[i]) dfs(i, 0);
for (int i = 1; i <= n; ++i) printf("%d ", c[i * 2]);
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int get() {
char c;
while (!isdigit(c = getchar()))
;
int v = c - 48;
while (isdigit(c = getchar())) v = v * 10 + c - 48;
return v;
}
int n;
pair<int, int> a[200005];
bool vis[200005], c[200005];
vector<int> G[200005];
void dfs(int x, int col) {
vis[x] = 1;
c[x] = col;
for (int i = 0; i < G[x].size(); ++i)
if (!vis[G[x][i]]) dfs(G[x][i], col ^ 1);
}
int main() {
n = get();
for (int i = 1, l, r; i <= n; ++i) {
l = get();
r = get();
a[i * 2 - 1] = make_pair(2 * l, 2 * i - 1);
a[i * 2] = make_pair(2 * r + 1, 2 * i);
G[2 * i - 1].push_back(2 * i);
G[2 * i].push_back(2 * i - 1);
}
sort(a + 1, a + 2 * n + 1);
for (int i = 1, u, v; i <= n; ++i) {
u = a[2 * i - 1].second;
v = a[2 * i].second;
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= 2 * n; ++i)
if (!vis[i]) dfs(i, 0);
for (int i = 1; i <= n; ++i) printf("%d ", c[i * 2]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int head[maxn], deg[maxn], ans[maxn], tot = 1;
struct edge {
int v, nex, id;
bool f;
} e[maxn << 4];
void add(int u, int v, int id) {
e[++tot] = {v, head[u], id, 0}, head[u] = tot;
e[++tot] = {u, head[v], id, 0}, head[v] = tot;
}
struct Q {
int l, r;
} q[maxn];
vector<int> a;
void euler(int u) {
for (int &i = head[u]; i; i = e[i].nex) {
if (e[i].f) continue;
e[i].f = e[i ^ 1].f = 1;
int v = e[i].v;
ans[e[i].id] = (u < v);
euler(v);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> q[i].l >> q[i].r;
q[i].r++;
a.push_back(q[i].l);
a.push_back(q[i].r);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
for (int i = 0; i < n; i++) {
q[i].l = lower_bound(a.begin(), a.end(), q[i].l) - a.begin();
q[i].r = lower_bound(a.begin(), a.end(), q[i].r) - a.begin();
deg[q[i].l]++, deg[q[i].r]++;
add(q[i].l, q[i].r, i);
}
int len = a.size(), last = -1;
for (int i = 0; i < len; i++) {
if (deg[i] & 1) {
if (last == -1)
last = i;
else
add(last, i, n + 3), last = -1;
}
}
for (int i = 0; i < len; i++)
if (head[i]) euler(i);
for (int i = 0; i < n; i++) cout << ans[i] << ' ';
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int head[maxn], deg[maxn], ans[maxn], tot = 1;
struct edge {
int v, nex, id;
bool f;
} e[maxn << 4];
void add(int u, int v, int id) {
e[++tot] = {v, head[u], id, 0}, head[u] = tot;
e[++tot] = {u, head[v], id, 0}, head[v] = tot;
}
struct Q {
int l, r;
} q[maxn];
vector<int> a;
void euler(int u) {
for (int &i = head[u]; i; i = e[i].nex) {
if (e[i].f) continue;
e[i].f = e[i ^ 1].f = 1;
int v = e[i].v;
ans[e[i].id] = (u < v);
euler(v);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> q[i].l >> q[i].r;
q[i].r++;
a.push_back(q[i].l);
a.push_back(q[i].r);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
for (int i = 0; i < n; i++) {
q[i].l = lower_bound(a.begin(), a.end(), q[i].l) - a.begin();
q[i].r = lower_bound(a.begin(), a.end(), q[i].r) - a.begin();
deg[q[i].l]++, deg[q[i].r]++;
add(q[i].l, q[i].r, i);
}
int len = a.size(), last = -1;
for (int i = 0; i < len; i++) {
if (deg[i] & 1) {
if (last == -1)
last = i;
else
add(last, i, n + 3), last = -1;
}
}
for (int i = 0; i < len; i++)
if (head[i]) euler(i);
for (int i = 0; i < n; i++) cout << ans[i] << ' ';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, cl[N], l, r;
set<pair<pair<int, int>, int> > q;
vector<int> g[N];
void dfs(int v, int c = 0) {
cl[v] = c;
for (auto to : g[v]) {
if (cl[to] == -1) {
dfs(to, (c ^ 1));
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l >> r;
q.insert(make_pair(make_pair(l, r), i));
}
while (true) {
if (q.empty()) {
break;
}
auto it1 = *q.begin();
q.erase(q.begin());
if (q.empty()) {
break;
}
auto it2 = *q.begin();
q.erase(q.begin());
if (it1.first.second < it2.first.first) {
q.insert(it2);
continue;
}
g[it1.second].push_back(it2.second);
g[it2.second].push_back(it1.second);
if (it1.first.second > it2.first.second) {
q.insert(make_pair(make_pair(it2.first.second + 1, it1.first.second),
it1.second));
} else if (it1.first.second == it2.first.second) {
} else {
q.insert(make_pair(make_pair(it1.first.second + 1, it2.first.second),
it2.second));
}
}
memset(cl, -1, sizeof(cl));
for (int i = 1; i <= n; i++) {
if (cl[i] == -1) {
dfs(i);
}
}
for (int i = 1; i <= n; i++) {
cout << cl[i] << " ";
}
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, cl[N], l, r;
set<pair<pair<int, int>, int> > q;
vector<int> g[N];
void dfs(int v, int c = 0) {
cl[v] = c;
for (auto to : g[v]) {
if (cl[to] == -1) {
dfs(to, (c ^ 1));
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l >> r;
q.insert(make_pair(make_pair(l, r), i));
}
while (true) {
if (q.empty()) {
break;
}
auto it1 = *q.begin();
q.erase(q.begin());
if (q.empty()) {
break;
}
auto it2 = *q.begin();
q.erase(q.begin());
if (it1.first.second < it2.first.first) {
q.insert(it2);
continue;
}
g[it1.second].push_back(it2.second);
g[it2.second].push_back(it1.second);
if (it1.first.second > it2.first.second) {
q.insert(make_pair(make_pair(it2.first.second + 1, it1.first.second),
it1.second));
} else if (it1.first.second == it2.first.second) {
} else {
q.insert(make_pair(make_pair(it1.first.second + 1, it2.first.second),
it2.second));
}
}
memset(cl, -1, sizeof(cl));
for (int i = 1; i <= n; i++) {
if (cl[i] == -1) {
dfs(i);
}
}
for (int i = 1; i <= n; i++) {
cout << cl[i] << " ";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename _tp>
inline void read(_tp& x) {
char c11 = getchar(), ob = 0;
x = 0;
while (c11 != '-' && !isdigit(c11)) c11 = getchar();
if (c11 == '-') ob = 1, c11 = getchar();
while (isdigit(c11)) x = x * 10 + c11 - '0', c11 = getchar();
if (ob) x = -x;
}
const int N = 201000;
struct Edge {
int v, id, nxt;
} a[N + N];
int head[N], srt[N], L[N], R[N];
int deg[N], Ans[N];
bool vs[N], stop[N + N];
int n, _ = 1;
inline void add(int u, int v, int i) {
a[++_].v = v, a[_].id = i, a[_].nxt = head[u], head[u] = _, ++deg[u];
a[++_].v = u, a[_].id = i, a[_].nxt = head[v], head[v] = _, ++deg[v];
}
void dfs(int x) {
vs[x] = true;
for (int& i = head[x]; i; i = a[i].nxt)
if (!stop[i]) {
stop[i] = stop[i ^ 1] = true;
Ans[a[i].id] = x < a[i].v;
dfs(a[i].v);
}
}
int main() {
read(n);
for (int i = 1; i <= n; ++i) {
read(L[i]), srt[i + i - 1] = (L[i] = L[i] << 1);
read(R[i]), srt[i + i] = (R[i] = R[i] << 1 | 1);
}
sort(srt + 1, srt + n + n + 1);
int nn = unique(srt + 1, srt + n + n + 1) - srt - 1;
for (int i = 1, l, r; i <= n; ++i) {
l = lower_bound(srt + 1, srt + nn + 1, L[i]) - srt;
r = lower_bound(srt + 1, srt + nn + 1, R[i]) - srt;
add(l, r, i);
}
for (int i = 1, ls = 0; i <= nn; ++i)
if (deg[i] & 1)
if (ls)
add(ls, i, 0), ls = 0;
else
ls = i;
for (int i = 1; i <= nn; ++i)
if (!vs[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", Ans[i]);
putchar(10);
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename _tp>
inline void read(_tp& x) {
char c11 = getchar(), ob = 0;
x = 0;
while (c11 != '-' && !isdigit(c11)) c11 = getchar();
if (c11 == '-') ob = 1, c11 = getchar();
while (isdigit(c11)) x = x * 10 + c11 - '0', c11 = getchar();
if (ob) x = -x;
}
const int N = 201000;
struct Edge {
int v, id, nxt;
} a[N + N];
int head[N], srt[N], L[N], R[N];
int deg[N], Ans[N];
bool vs[N], stop[N + N];
int n, _ = 1;
inline void add(int u, int v, int i) {
a[++_].v = v, a[_].id = i, a[_].nxt = head[u], head[u] = _, ++deg[u];
a[++_].v = u, a[_].id = i, a[_].nxt = head[v], head[v] = _, ++deg[v];
}
void dfs(int x) {
vs[x] = true;
for (int& i = head[x]; i; i = a[i].nxt)
if (!stop[i]) {
stop[i] = stop[i ^ 1] = true;
Ans[a[i].id] = x < a[i].v;
dfs(a[i].v);
}
}
int main() {
read(n);
for (int i = 1; i <= n; ++i) {
read(L[i]), srt[i + i - 1] = (L[i] = L[i] << 1);
read(R[i]), srt[i + i] = (R[i] = R[i] << 1 | 1);
}
sort(srt + 1, srt + n + n + 1);
int nn = unique(srt + 1, srt + n + n + 1) - srt - 1;
for (int i = 1, l, r; i <= n; ++i) {
l = lower_bound(srt + 1, srt + nn + 1, L[i]) - srt;
r = lower_bound(srt + 1, srt + nn + 1, R[i]) - srt;
add(l, r, i);
}
for (int i = 1, ls = 0; i <= nn; ++i)
if (deg[i] & 1)
if (ls)
add(ls, i, 0), ls = 0;
else
ls = i;
for (int i = 1; i <= nn; ++i)
if (!vs[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", Ans[i]);
putchar(10);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
bool cmp(int *a, int *b) { return *a < *b; }
int n;
int l[MAXN], r[MAXN];
int *ts[MAXN * 2];
vector<pair<int, int> > ls[MAXN * 2];
bool u[MAXN * 2];
bool sol[MAXN];
void tour(int a) {
for (pair<int, int> &e : ls[a])
if (!u[e.second]) {
if (e.second < n) sol[e.second] = a < e.first;
u[e.second] = 1;
tour(e.first);
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
l[i] *= 2;
r[i] = r[i] * 2 + 1;
ts[2 * i + 0] = l + i;
ts[2 * i + 1] = r + i;
}
sort(ts, ts + 2 * n, cmp);
int c = -1;
int o = -1;
for (int i = 0; i < 2 * n; ++i) {
c += *ts[i] != o;
o = *ts[i];
*ts[i] = c;
}
++c;
for (int i = 0; i < n; ++i) {
ls[l[i]].push_back(pair<int, int>(r[i], i));
ls[r[i]].push_back(pair<int, int>(l[i], i));
}
for (int i = 0; i < c; ++i)
if (ls[i].size() & 1)
for (int j = i + 1; j < c; ++j)
if (ls[j].size() & 1) {
ls[i].push_back(pair<int, int>(j, n + i));
ls[j].push_back(pair<int, int>(i, n + i));
i = j;
break;
}
for (int i = 0; i < c; ++i) tour(i);
for (int i = 0; i < n; ++i) cout << sol[i] << ' ';
cout << endl;
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
bool cmp(int *a, int *b) { return *a < *b; }
int n;
int l[MAXN], r[MAXN];
int *ts[MAXN * 2];
vector<pair<int, int> > ls[MAXN * 2];
bool u[MAXN * 2];
bool sol[MAXN];
void tour(int a) {
for (pair<int, int> &e : ls[a])
if (!u[e.second]) {
if (e.second < n) sol[e.second] = a < e.first;
u[e.second] = 1;
tour(e.first);
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
l[i] *= 2;
r[i] = r[i] * 2 + 1;
ts[2 * i + 0] = l + i;
ts[2 * i + 1] = r + i;
}
sort(ts, ts + 2 * n, cmp);
int c = -1;
int o = -1;
for (int i = 0; i < 2 * n; ++i) {
c += *ts[i] != o;
o = *ts[i];
*ts[i] = c;
}
++c;
for (int i = 0; i < n; ++i) {
ls[l[i]].push_back(pair<int, int>(r[i], i));
ls[r[i]].push_back(pair<int, int>(l[i], i));
}
for (int i = 0; i < c; ++i)
if (ls[i].size() & 1)
for (int j = i + 1; j < c; ++j)
if (ls[j].size() & 1) {
ls[i].push_back(pair<int, int>(j, n + i));
ls[j].push_back(pair<int, int>(i, n + i));
i = j;
break;
}
for (int i = 0; i < c; ++i) tour(i);
for (int i = 0; i < n; ++i) cout << sol[i] << ' ';
cout << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int M = 1e5;
const int N = 2 * 1e6 + 5;
struct node {
int ver, next;
} e[N];
int tot, head[N];
int v[N];
int n, m;
multimap<int, int> h;
void add(int x, int y) {
e[++tot].ver = y;
e[tot].next = head[x];
head[x] = tot;
}
void dfs(int x, int color) {
v[x] = color;
for (int i = head[x]; i; i = e[i].next) {
int y = e[i].ver;
if (!v[y]) dfs(y, 3 - color);
}
}
signed main() {
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(i << 1, i << 1 | 1);
add(i << 1 | 1, i << 1);
h.insert(make_pair(x << 1, i << 1));
h.insert(make_pair(y << 1 | 1, i << 1 | 1));
}
multimap<int, int>::iterator it = h.begin();
for (int i = 0; i < n; i++) {
int l = it->second;
it++;
int r = it->second;
it++;
add(l, r);
add(r, l);
}
for (int i = 0; i < n; i++)
if (!v[i << 1]) dfs(i << 1, 1);
for (int i = 0; i < n; i++) printf("%d ", v[i << 1] - 1);
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int M = 1e5;
const int N = 2 * 1e6 + 5;
struct node {
int ver, next;
} e[N];
int tot, head[N];
int v[N];
int n, m;
multimap<int, int> h;
void add(int x, int y) {
e[++tot].ver = y;
e[tot].next = head[x];
head[x] = tot;
}
void dfs(int x, int color) {
v[x] = color;
for (int i = head[x]; i; i = e[i].next) {
int y = e[i].ver;
if (!v[y]) dfs(y, 3 - color);
}
}
signed main() {
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(i << 1, i << 1 | 1);
add(i << 1 | 1, i << 1);
h.insert(make_pair(x << 1, i << 1));
h.insert(make_pair(y << 1 | 1, i << 1 | 1));
}
multimap<int, int>::iterator it = h.begin();
for (int i = 0; i < n; i++) {
int l = it->second;
it++;
int r = it->second;
it++;
add(l, r);
add(r, l);
}
for (int i = 0; i < n; i++)
if (!v[i << 1]) dfs(i << 1, 1);
for (int i = 0; i < n; i++) printf("%d ", v[i << 1] - 1);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = int(2e5) + 10;
int n;
vector<int> E[MAX_N];
void addEdge(int u, int v) { E[u].push_back(v), E[v].push_back(u); }
int col[MAX_N];
void dfs(int u, int c) {
if (col[u] != -1) return;
col[u] = c;
for (vector<int>::iterator e = E[u].begin(); e != E[u].end(); ++e) {
dfs(*e, 1 - c);
}
}
int main() {
cin >> n;
vector<pair<int, int> > events;
for (int i = 0; i < n; ++i) {
int l, r;
scanf("%d%d", &l, &r);
l *= 2, r = r * 2 + 1;
addEdge(i * 2, i * 2 + 1);
events.push_back(make_pair(l, 2 * i));
events.push_back(make_pair(r, 2 * i + 1));
}
sort(events.begin(), events.end());
for (int i = 0; i < n; ++i) {
addEdge(events[i * 2].second, events[i * 2 + 1].second);
}
memset(col, -1, sizeof col);
for (int i = 0; i < n * 2; ++i) {
if (col[i] == -1) dfs(i, 0);
}
for (int i = 0; i < n; ++i) {
printf("%d ", col[i * 2]);
}
puts("");
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = int(2e5) + 10;
int n;
vector<int> E[MAX_N];
void addEdge(int u, int v) { E[u].push_back(v), E[v].push_back(u); }
int col[MAX_N];
void dfs(int u, int c) {
if (col[u] != -1) return;
col[u] = c;
for (vector<int>::iterator e = E[u].begin(); e != E[u].end(); ++e) {
dfs(*e, 1 - c);
}
}
int main() {
cin >> n;
vector<pair<int, int> > events;
for (int i = 0; i < n; ++i) {
int l, r;
scanf("%d%d", &l, &r);
l *= 2, r = r * 2 + 1;
addEdge(i * 2, i * 2 + 1);
events.push_back(make_pair(l, 2 * i));
events.push_back(make_pair(r, 2 * i + 1));
}
sort(events.begin(), events.end());
for (int i = 0; i < n; ++i) {
addEdge(events[i * 2].second, events[i * 2 + 1].second);
}
memset(col, -1, sizeof col);
for (int i = 0; i < n * 2; ++i) {
if (col[i] == -1) dfs(i, 0);
}
for (int i = 0; i < n; ++i) {
printf("%d ", col[i * 2]);
}
puts("");
}
```
|
#include <bits/stdc++.h>
long long gi() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f ? x : -x;
}
int fir[200010], dis[1000010], nxt[1000010], w[1000010], dir[1000010], id = 1;
void link(int a, int b) {
nxt[++id] = fir[a], fir[a] = id, dis[id] = b, w[id] = 1;
nxt[++id] = fir[b], fir[b] = id, dis[id] = a, w[id] = 1;
}
int l[100010], r[100010], u[200010], U, E[100010], c[200010];
void dfs(int x) {
for (int& i = fir[x]; i; i = nxt[i])
if (w[i]) w[i] = w[i ^ 1] = 0, dir[i] = 1, dir[i ^ 1] = 2, dfs(dis[i]);
}
int main() {
int n = gi();
for (int i = 1; i <= n; ++i)
l[i] = gi() - 1, r[i] = gi(), u[++U] = l[i], u[++U] = r[i];
std::sort(u + 1, u + U + 1);
U = std::unique(u + 1, u + U + 1) - u - 1;
for (int i = 1; i <= n; ++i)
l[i] = std::lower_bound(u + 1, u + U + 1, l[i]) - u;
for (int i = 1; i <= n; ++i)
r[i] = std::lower_bound(u + 1, u + U + 1, r[i]) - u;
for (int i = 1; i <= n; ++i)
link(l[i], r[i]), ++c[l[i]], ++c[r[i]], E[i] = id;
for (int i = 1; i <= U; ++i)
if (c[i] & 1) link(i, i + 1), ++c[i + 1];
for (int i = 1; i <= U; ++i) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", dir[E[i]] & 1);
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
long long gi() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f ? x : -x;
}
int fir[200010], dis[1000010], nxt[1000010], w[1000010], dir[1000010], id = 1;
void link(int a, int b) {
nxt[++id] = fir[a], fir[a] = id, dis[id] = b, w[id] = 1;
nxt[++id] = fir[b], fir[b] = id, dis[id] = a, w[id] = 1;
}
int l[100010], r[100010], u[200010], U, E[100010], c[200010];
void dfs(int x) {
for (int& i = fir[x]; i; i = nxt[i])
if (w[i]) w[i] = w[i ^ 1] = 0, dir[i] = 1, dir[i ^ 1] = 2, dfs(dis[i]);
}
int main() {
int n = gi();
for (int i = 1; i <= n; ++i)
l[i] = gi() - 1, r[i] = gi(), u[++U] = l[i], u[++U] = r[i];
std::sort(u + 1, u + U + 1);
U = std::unique(u + 1, u + U + 1) - u - 1;
for (int i = 1; i <= n; ++i)
l[i] = std::lower_bound(u + 1, u + U + 1, l[i]) - u;
for (int i = 1; i <= n; ++i)
r[i] = std::lower_bound(u + 1, u + U + 1, r[i]) - u;
for (int i = 1; i <= n; ++i)
link(l[i], r[i]), ++c[l[i]], ++c[r[i]], E[i] = id;
for (int i = 1; i <= U; ++i)
if (c[i] & 1) link(i, i + 1), ++c[i + 1];
for (int i = 1; i <= U; ++i) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", dir[E[i]] & 1);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100007;
const int maxm = 200007;
const int maxe = 400007;
struct node {
int from, to, nxt, id;
} e[maxe];
int l[maxn], r[maxn], a[maxm], head[maxm], vis[maxm], used[maxm], d[maxm],
c[maxm], top, tot;
void add(int u, int v, int id) {
e[tot].nxt = head[u];
e[tot].from = u;
e[tot].to = v;
e[tot].id = id;
head[u] = tot++;
}
void add_edge(int u, int v, int id) {
add(u, v, id);
add(v, u, id);
}
void dfs(int now) {
vis[now] = 1;
for (int i = head[now]; ~i; i = e[i].nxt) {
if (used[i / 2]) continue;
used[i / 2] = 1;
if (e[i].from < e[i].to) c[e[i].id] = 1;
dfs(e[i].to);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &l[i], &r[i]);
r[i]++;
a[i * 2] = l[i];
a[i * 2 + 1] = r[i];
}
sort(a, a + n + n);
top = (int)(unique(a, a + n + n) - a);
for (int i = 0; i < top; i++) head[i] = -1;
for (int i = 0; i < n; i++) {
l[i] = (int)(lower_bound(a, a + top, l[i]) - a);
r[i] = (int)(lower_bound(a, a + top, r[i]) - a);
add_edge(l[i], r[i], i);
d[l[i]]++;
d[r[i]]++;
}
int lst = -1, tmp = n;
for (int i = 0; i < top; i++) {
if (d[i] % 2) {
if (lst == -1)
lst = i;
else {
add_edge(i, lst, tmp++);
lst = -1;
}
}
}
for (int i = 0; i < top; i++) {
if (!vis[i]) dfs(i);
}
for (int i = 0; i < n; i++) printf("%d%c", c[i], (i == n - 1) ? '\n' : ' ');
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100007;
const int maxm = 200007;
const int maxe = 400007;
struct node {
int from, to, nxt, id;
} e[maxe];
int l[maxn], r[maxn], a[maxm], head[maxm], vis[maxm], used[maxm], d[maxm],
c[maxm], top, tot;
void add(int u, int v, int id) {
e[tot].nxt = head[u];
e[tot].from = u;
e[tot].to = v;
e[tot].id = id;
head[u] = tot++;
}
void add_edge(int u, int v, int id) {
add(u, v, id);
add(v, u, id);
}
void dfs(int now) {
vis[now] = 1;
for (int i = head[now]; ~i; i = e[i].nxt) {
if (used[i / 2]) continue;
used[i / 2] = 1;
if (e[i].from < e[i].to) c[e[i].id] = 1;
dfs(e[i].to);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &l[i], &r[i]);
r[i]++;
a[i * 2] = l[i];
a[i * 2 + 1] = r[i];
}
sort(a, a + n + n);
top = (int)(unique(a, a + n + n) - a);
for (int i = 0; i < top; i++) head[i] = -1;
for (int i = 0; i < n; i++) {
l[i] = (int)(lower_bound(a, a + top, l[i]) - a);
r[i] = (int)(lower_bound(a, a + top, r[i]) - a);
add_edge(l[i], r[i], i);
d[l[i]]++;
d[r[i]]++;
}
int lst = -1, tmp = n;
for (int i = 0; i < top; i++) {
if (d[i] % 2) {
if (lst == -1)
lst = i;
else {
add_edge(i, lst, tmp++);
lst = -1;
}
}
}
for (int i = 0; i < top; i++) {
if (!vis[i]) dfs(i);
}
for (int i = 0; i < n; i++) printf("%d%c", c[i], (i == n - 1) ? '\n' : ' ');
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int ans = 0, fh = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') fh = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar();
return ans * fh;
}
const int maxn = 4e5;
int n, ql[maxn], qr[maxn], b[maxn], mx, du[maxn];
int head[maxn], nex[maxn], v[maxn], num = 1, vis[maxn], ans[maxn];
inline void lsh() {
for (int i = 1; i <= n; i++) b[++mx] = ql[i], b[++mx] = qr[i];
sort(b + 1, b + mx + 1), mx = unique(b + 1, b + mx + 1) - b - 1;
for (int i = 1; i <= n; i++) {
ql[i] = lower_bound(b + 1, b + mx + 1, ql[i]) - b;
qr[i] = lower_bound(b + 1, b + mx + 1, qr[i]) - b;
}
}
inline void add(int x, int y) {
v[++num] = y, du[x]++;
nex[num] = head[x], head[x] = num;
v[++num] = x, du[y]++;
nex[num] = head[y], head[y] = num;
}
void dfs(int x) {
for (int &i = head[x]; i; i = nex[i]) {
if (vis[i >> 1]) continue;
int y = v[i];
vis[i >> 1] = 1;
du[x]--, du[y]--;
ans[i >> 1] = y > x, dfs(y);
}
}
int main() {
n = read();
for (int i = 1; i <= n; i++) ql[i] = read(), qr[i] = read() + 1;
lsh();
for (int i = 1; i <= n; i++) add(ql[i], qr[i]);
for (int i = 1, las = 0; i <= mx; i++)
if (du[i] & 1) {
if (!las)
las = i;
else
add(las, i), las = 0;
}
for (int i = 1; i <= mx; i++)
if (du[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int ans = 0, fh = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') fh = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar();
return ans * fh;
}
const int maxn = 4e5;
int n, ql[maxn], qr[maxn], b[maxn], mx, du[maxn];
int head[maxn], nex[maxn], v[maxn], num = 1, vis[maxn], ans[maxn];
inline void lsh() {
for (int i = 1; i <= n; i++) b[++mx] = ql[i], b[++mx] = qr[i];
sort(b + 1, b + mx + 1), mx = unique(b + 1, b + mx + 1) - b - 1;
for (int i = 1; i <= n; i++) {
ql[i] = lower_bound(b + 1, b + mx + 1, ql[i]) - b;
qr[i] = lower_bound(b + 1, b + mx + 1, qr[i]) - b;
}
}
inline void add(int x, int y) {
v[++num] = y, du[x]++;
nex[num] = head[x], head[x] = num;
v[++num] = x, du[y]++;
nex[num] = head[y], head[y] = num;
}
void dfs(int x) {
for (int &i = head[x]; i; i = nex[i]) {
if (vis[i >> 1]) continue;
int y = v[i];
vis[i >> 1] = 1;
du[x]--, du[y]--;
ans[i >> 1] = y > x, dfs(y);
}
}
int main() {
n = read();
for (int i = 1; i <= n; i++) ql[i] = read(), qr[i] = read() + 1;
lsh();
for (int i = 1; i <= n; i++) add(ql[i], qr[i]);
for (int i = 1, las = 0; i <= mx; i++)
if (du[i] & 1) {
if (!las)
las = i;
else
add(las, i), las = 0;
}
for (int i = 1; i <= mx; i++)
if (du[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 510000;
struct node {
int to, next, id;
} a[N * 2];
int n, l[N], r[N], x[N], ls[N], tot = 1, ans[N], in[N], cnt;
bool V[N], v[N];
void addl(int x, int y, int id) {
a[++tot].to = y;
a[tot].next = ls[x];
a[tot].id = id;
ls[x] = tot;
in[y]++;
}
void dfs(int x) {
V[x] = 1;
for (int i = ls[x]; i; i = a[i].next) {
if (v[i]) continue;
int y = a[i].to;
v[i] = v[i ^ 1] = 1;
dfs(y);
ans[a[i].id] = (x < a[i].to);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &l[i], &r[i]);
r[i]++;
x[++cnt] = l[i];
x[++cnt] = r[i];
}
sort(x + 1, x + 1 + cnt);
cnt = unique(x + 1, x + 1 + cnt) - x - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(x + 1, x + 1 + cnt, l[i]) - x;
r[i] = lower_bound(x + 1, x + 1 + cnt, r[i]) - x;
addl(l[i], r[i], i);
addl(r[i], l[i], i);
}
int last = 0;
for (int i = 1; i <= cnt; i++)
if (in[i] & 1) {
if (last)
addl(last, i, 0), addl(i, last, 0), last = 0;
else
last = i;
}
for (int i = 1; i <= cnt; i++)
if (!V[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 510000;
struct node {
int to, next, id;
} a[N * 2];
int n, l[N], r[N], x[N], ls[N], tot = 1, ans[N], in[N], cnt;
bool V[N], v[N];
void addl(int x, int y, int id) {
a[++tot].to = y;
a[tot].next = ls[x];
a[tot].id = id;
ls[x] = tot;
in[y]++;
}
void dfs(int x) {
V[x] = 1;
for (int i = ls[x]; i; i = a[i].next) {
if (v[i]) continue;
int y = a[i].to;
v[i] = v[i ^ 1] = 1;
dfs(y);
ans[a[i].id] = (x < a[i].to);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &l[i], &r[i]);
r[i]++;
x[++cnt] = l[i];
x[++cnt] = r[i];
}
sort(x + 1, x + 1 + cnt);
cnt = unique(x + 1, x + 1 + cnt) - x - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(x + 1, x + 1 + cnt, l[i]) - x;
r[i] = lower_bound(x + 1, x + 1 + cnt, r[i]) - x;
addl(l[i], r[i], i);
addl(r[i], l[i], i);
}
int last = 0;
for (int i = 1; i <= cnt; i++)
if (in[i] & 1) {
if (last)
addl(last, i, 0), addl(i, last, 0), last = 0;
else
last = i;
}
for (int i = 1; i <= cnt; i++)
if (!V[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long c[300100], l[300100], r[300100], acc[300100];
set<long long> gr[300100];
void dfs(long long v) {
if (!gr[v].empty()) {
long long e = *gr[v].begin();
long long u = r[e] ^ l[e] ^ v;
gr[v].erase(e);
gr[u].erase(e);
if (v < u) c[e] = 1;
dfs(u);
}
}
int main() {
long long n;
scanf("%I64d", &n);
long long on = n;
vector<long long> points;
for (long long i = 0; i < n; i++) {
scanf("%I64d %I64d", &l[i], &r[i]);
points.push_back(l[i]);
points.push_back(++r[i]);
}
sort(points.begin(), points.end());
unique(points.begin(), points.end());
for (long long i = 0; i < n; i++) {
l[i] = lower_bound(points.begin(), points.end(), l[i]) - points.begin();
r[i] = lower_bound(points.begin(), points.end(), r[i]) - points.begin();
acc[l[i]]++;
acc[r[i]]--;
}
for (long long i = 1; i < points.size(); i++) acc[i] += acc[i - 1];
for (long long i = 0; i < points.size(); i++)
if (acc[i] % 2) {
l[n] = i;
r[n] = i + 1;
n++;
}
for (long long i = 0; i < n; i++) {
gr[l[i]].insert(i);
gr[r[i]].insert(i);
}
for (long long i = 0; i < points.size(); i++) dfs(i);
for (long long i = 0; i < on; i++) printf("%I64d ", c[i]);
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long c[300100], l[300100], r[300100], acc[300100];
set<long long> gr[300100];
void dfs(long long v) {
if (!gr[v].empty()) {
long long e = *gr[v].begin();
long long u = r[e] ^ l[e] ^ v;
gr[v].erase(e);
gr[u].erase(e);
if (v < u) c[e] = 1;
dfs(u);
}
}
int main() {
long long n;
scanf("%I64d", &n);
long long on = n;
vector<long long> points;
for (long long i = 0; i < n; i++) {
scanf("%I64d %I64d", &l[i], &r[i]);
points.push_back(l[i]);
points.push_back(++r[i]);
}
sort(points.begin(), points.end());
unique(points.begin(), points.end());
for (long long i = 0; i < n; i++) {
l[i] = lower_bound(points.begin(), points.end(), l[i]) - points.begin();
r[i] = lower_bound(points.begin(), points.end(), r[i]) - points.begin();
acc[l[i]]++;
acc[r[i]]--;
}
for (long long i = 1; i < points.size(); i++) acc[i] += acc[i - 1];
for (long long i = 0; i < points.size(); i++)
if (acc[i] % 2) {
l[n] = i;
r[n] = i + 1;
n++;
}
for (long long i = 0; i < n; i++) {
gr[l[i]].insert(i);
gr[r[i]].insert(i);
}
for (long long i = 0; i < points.size(); i++) dfs(i);
for (long long i = 0; i < on; i++) printf("%I64d ", c[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int first = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) {
if (ch == '-') f = -1;
}
for (; isdigit(ch); ch = getchar()) {
first = first * 10 + ch - 48;
}
return first * f;
}
const int mxN = 2e5;
int n, en;
vector<int> disc;
pair<int, int> a[mxN + 3];
int cnt[mxN + 3];
struct Edge {
int v, nxt;
} e[mxN * 2 + 3];
int fe[mxN + 3];
int vis[mxN + 3];
int ans[mxN + 3];
void addedge(int u, int v) {
en++;
e[en].v = v;
e[en].nxt = fe[u];
fe[u] = en;
}
void dfs(int u) {
for (int &i = fe[u]; i; i = e[i].nxt)
if (!vis[i >> 1]) {
vis[i >> 1] = 1;
int v = e[i].v, o = i;
dfs(v);
ans[o >> 1] = o & 1;
}
}
int main() {
n = read();
for (int i = 1; i <= n; i++) {
a[i].first = read(), a[i].second = read();
disc.push_back(a[i].first - 1), disc.push_back(a[i].second);
}
sort(disc.begin(), disc.end());
disc.erase(unique(disc.begin(), disc.end()), disc.end());
en = 1;
for (int i = 1; i <= n; i++) {
a[i].first =
lower_bound(disc.begin(), disc.end(), a[i].first - 1) - disc.begin(),
a[i].second =
lower_bound(disc.begin(), disc.end(), a[i].second) - disc.begin();
cnt[a[i].first]++, cnt[a[i].second]++;
addedge(a[i].first, a[i].second);
addedge(a[i].second, a[i].first);
}
for (int i = 0, first = -1; i <= disc.size(); i++) {
if (cnt[i] & 1) {
if (first == -1) {
first = i;
} else {
addedge(first, i), addedge(i, first);
first = -1;
}
}
}
for (int i = 0; i <= disc.size(); i++) {
dfs(i);
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int first = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) {
if (ch == '-') f = -1;
}
for (; isdigit(ch); ch = getchar()) {
first = first * 10 + ch - 48;
}
return first * f;
}
const int mxN = 2e5;
int n, en;
vector<int> disc;
pair<int, int> a[mxN + 3];
int cnt[mxN + 3];
struct Edge {
int v, nxt;
} e[mxN * 2 + 3];
int fe[mxN + 3];
int vis[mxN + 3];
int ans[mxN + 3];
void addedge(int u, int v) {
en++;
e[en].v = v;
e[en].nxt = fe[u];
fe[u] = en;
}
void dfs(int u) {
for (int &i = fe[u]; i; i = e[i].nxt)
if (!vis[i >> 1]) {
vis[i >> 1] = 1;
int v = e[i].v, o = i;
dfs(v);
ans[o >> 1] = o & 1;
}
}
int main() {
n = read();
for (int i = 1; i <= n; i++) {
a[i].first = read(), a[i].second = read();
disc.push_back(a[i].first - 1), disc.push_back(a[i].second);
}
sort(disc.begin(), disc.end());
disc.erase(unique(disc.begin(), disc.end()), disc.end());
en = 1;
for (int i = 1; i <= n; i++) {
a[i].first =
lower_bound(disc.begin(), disc.end(), a[i].first - 1) - disc.begin(),
a[i].second =
lower_bound(disc.begin(), disc.end(), a[i].second) - disc.begin();
cnt[a[i].first]++, cnt[a[i].second]++;
addedge(a[i].first, a[i].second);
addedge(a[i].second, a[i].first);
}
for (int i = 0, first = -1; i <= disc.size(); i++) {
if (cnt[i] & 1) {
if (first == -1) {
first = i;
} else {
addedge(first, i), addedge(i, first);
first = -1;
}
}
}
for (int i = 0; i <= disc.size(); i++) {
dfs(i);
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 190010;
bool cmp(int *a, int *b) { return *a < *b; }
int n;
int l[MAXN], r[MAXN];
int *ts[MAXN * 2];
vector<pair<int, int> > ls[MAXN * 2];
bool u[MAXN * 2];
bool sol[MAXN];
void tour(int a) {
for (pair<int, int> &e : ls[a])
if (!u[e.second]) {
if (e.second < n) sol[e.second] = a < e.first;
u[e.second] = 1;
tour(e.first);
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
l[i] *= 2;
r[i] = r[i] * 2 + 1;
ts[2 * i + 0] = l + i;
ts[2 * i + 1] = r + i;
}
sort(ts, ts + 2 * n, cmp);
int c = -1;
int o = -1;
for (int i = 0; i < 2 * n; ++i) {
c += *ts[i] != o;
o = *ts[i];
*ts[i] = c;
}
++c;
for (int i = 0; i < n; ++i) {
ls[l[i]].push_back(pair<int, int>(r[i], i));
ls[r[i]].push_back(pair<int, int>(l[i], i));
}
for (int i = 0; i < c; ++i)
if (ls[i].size() & 1)
for (int j = i + 1; j < c; ++j)
if (ls[j].size() & 1) {
ls[i].push_back(pair<int, int>(j, n + i));
ls[j].push_back(pair<int, int>(i, n + i));
i = j;
break;
}
for (int i = 0; i < c; ++i) tour(i);
for (int i = 0; i < n; ++i) cout << sol[i] << ' ';
cout << endl;
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 190010;
bool cmp(int *a, int *b) { return *a < *b; }
int n;
int l[MAXN], r[MAXN];
int *ts[MAXN * 2];
vector<pair<int, int> > ls[MAXN * 2];
bool u[MAXN * 2];
bool sol[MAXN];
void tour(int a) {
for (pair<int, int> &e : ls[a])
if (!u[e.second]) {
if (e.second < n) sol[e.second] = a < e.first;
u[e.second] = 1;
tour(e.first);
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
l[i] *= 2;
r[i] = r[i] * 2 + 1;
ts[2 * i + 0] = l + i;
ts[2 * i + 1] = r + i;
}
sort(ts, ts + 2 * n, cmp);
int c = -1;
int o = -1;
for (int i = 0; i < 2 * n; ++i) {
c += *ts[i] != o;
o = *ts[i];
*ts[i] = c;
}
++c;
for (int i = 0; i < n; ++i) {
ls[l[i]].push_back(pair<int, int>(r[i], i));
ls[r[i]].push_back(pair<int, int>(l[i], i));
}
for (int i = 0; i < c; ++i)
if (ls[i].size() & 1)
for (int j = i + 1; j < c; ++j)
if (ls[j].size() & 1) {
ls[i].push_back(pair<int, int>(j, n + i));
ls[j].push_back(pair<int, int>(i, n + i));
i = j;
break;
}
for (int i = 0; i < c; ++i) tour(i);
for (int i = 0; i < n; ++i) cout << sol[i] << ' ';
cout << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct inter {
int l, r;
} I[600050];
struct point {
int next, to, w;
} ar[600050];
bool is[600050];
int a[600050], at, head[600050], art(1);
inline int lower(int, int, int, int*);
inline void read(int&), link(int, int, int), dfs(int);
int main() {
int n;
read(n);
for (int i(1); i <= n; ++i)
read(I[i].l), read(I[i].r), a[++at] = I[i].l, a[++at] = ++I[i].r;
sort(a + 1, a + at + 1), at = unique(a + 1, a + at + 1) - a - 1;
for (int i(1); i <= n; ++i)
link(I[i].l = lower(1, at, I[i].l, a), I[i].r = lower(1, at, I[i].r, a), i);
memset(a, 0, sizeof(a));
for (int i(1); i <= n; ++i) ++a[I[i].l], --a[I[i].r];
for (int i(1); i <= at; ++i) a[i] += a[i - 1];
for (int i(1); i <= at; ++i)
if (a[i] & 1) link(i, i + 1, 0);
for (int i(1); i <= at; ++i) dfs(i);
for (int i(1); i <= n; ++i) printf("%d ", a[i]);
return 0;
}
inline void dfs(int x) {
for (int i(head[x]); i; i = ar[i].next)
if (head[x] = ar[i].next, !is[i])
a[ar[i].w] = x < ar[i].to, is[i ^ 1] = 1, dfs(ar[i].to);
}
inline void link(int u, int v, int w) {
ar[++art] = {head[u], v, w}, head[u] = art;
ar[++art] = {head[v], u, w}, head[v] = art;
}
inline int lower(int l, int r, int x, int* a) {
int mid;
while (mid = l + r >> 1, l <= r) a[mid] < x ? l = mid + 1 : r = mid - 1;
return l;
}
inline void read(int& x) {
x ^= x;
register char c;
while (c = getchar(), c < '0' || c > '9')
;
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}
|
### Prompt
Please create a solution in CPP to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct inter {
int l, r;
} I[600050];
struct point {
int next, to, w;
} ar[600050];
bool is[600050];
int a[600050], at, head[600050], art(1);
inline int lower(int, int, int, int*);
inline void read(int&), link(int, int, int), dfs(int);
int main() {
int n;
read(n);
for (int i(1); i <= n; ++i)
read(I[i].l), read(I[i].r), a[++at] = I[i].l, a[++at] = ++I[i].r;
sort(a + 1, a + at + 1), at = unique(a + 1, a + at + 1) - a - 1;
for (int i(1); i <= n; ++i)
link(I[i].l = lower(1, at, I[i].l, a), I[i].r = lower(1, at, I[i].r, a), i);
memset(a, 0, sizeof(a));
for (int i(1); i <= n; ++i) ++a[I[i].l], --a[I[i].r];
for (int i(1); i <= at; ++i) a[i] += a[i - 1];
for (int i(1); i <= at; ++i)
if (a[i] & 1) link(i, i + 1, 0);
for (int i(1); i <= at; ++i) dfs(i);
for (int i(1); i <= n; ++i) printf("%d ", a[i]);
return 0;
}
inline void dfs(int x) {
for (int i(head[x]); i; i = ar[i].next)
if (head[x] = ar[i].next, !is[i])
a[ar[i].w] = x < ar[i].to, is[i ^ 1] = 1, dfs(ar[i].to);
}
inline void link(int u, int v, int w) {
ar[++art] = {head[u], v, w}, head[u] = art;
ar[++art] = {head[v], u, w}, head[v] = art;
}
inline int lower(int l, int r, int x, int* a) {
int mid;
while (mid = l + r >> 1, l <= r) a[mid] < x ? l = mid + 1 : r = mid - 1;
return l;
}
inline void read(int& x) {
x ^= x;
register char c;
while (c = getchar(), c < '0' || c > '9')
;
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 100005;
bool vis[maxn], par[maxn];
vector<int> edge[maxn];
pair<int, int> seg[maxn];
int n;
void add(int a, int b) { edge[a].push_back(b), edge[b].push_back(a); }
void dfs(int x, int cur) {
if (vis[x]) return;
vis[x] = true;
par[x] = cur;
for (int i = 0; i < edge[x].size(); i++) dfs(edge[x][i], cur ^ 1);
}
int main() {
scanf("%d", &n);
for (int i = 0, l, r; i < n; i++) {
scanf("%d%d", &l, &r);
add(i * 2, i * 2 + 1);
seg[i * 2] = make_pair(l, i * 2);
seg[i * 2 + 1] = make_pair(r + 1, i * 2 + 1);
}
sort(seg, seg + 2 * n);
for (int i = 0; i < n; i++) {
add(seg[i * 2].second, seg[i * 2 + 1].second);
}
memset(vis, false, sizeof(vis));
memset(par, 0, sizeof(par));
for (int i = 0; i < 2 * n; i++)
if (!vis[i]) dfs(i, 0);
for (int i = 0; i < n; i++) printf("%d ", par[i * 2]);
printf("\n");
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 100005;
bool vis[maxn], par[maxn];
vector<int> edge[maxn];
pair<int, int> seg[maxn];
int n;
void add(int a, int b) { edge[a].push_back(b), edge[b].push_back(a); }
void dfs(int x, int cur) {
if (vis[x]) return;
vis[x] = true;
par[x] = cur;
for (int i = 0; i < edge[x].size(); i++) dfs(edge[x][i], cur ^ 1);
}
int main() {
scanf("%d", &n);
for (int i = 0, l, r; i < n; i++) {
scanf("%d%d", &l, &r);
add(i * 2, i * 2 + 1);
seg[i * 2] = make_pair(l, i * 2);
seg[i * 2 + 1] = make_pair(r + 1, i * 2 + 1);
}
sort(seg, seg + 2 * n);
for (int i = 0; i < n; i++) {
add(seg[i * 2].second, seg[i * 2 + 1].second);
}
memset(vis, false, sizeof(vis));
memset(par, 0, sizeof(par));
for (int i = 0; i < 2 * n; i++)
if (!vis[i]) dfs(i, 0);
for (int i = 0; i < n; i++) printf("%d ", par[i * 2]);
printf("\n");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 55;
int V[maxn], U[maxn];
unordered_map<int, int> M;
vector<int> comp;
bitset<maxn> mark, emark, ans;
vector<int> adj[maxn];
void dfs(int v, int E = -1) {
mark[v] = 1;
for (auto e : adj[v]) {
if (!emark[e]) {
emark[e] = 1;
dfs(V[e] ^ U[e] ^ v, e);
}
}
if (~E && v == V[E]) ans[E] = 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> V[i] >> U[i];
U[i]++;
comp.push_back(V[i]);
comp.push_back(U[i]);
}
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
int n = comp.size();
for (int i = 0; i < n; i++) M[comp[i]] = i;
for (int i = 0; i < m; i++) {
V[i] = M[V[i]];
U[i] = M[U[i]];
adj[V[i]].push_back(i);
adj[U[i]].push_back(i);
}
vector<int> D;
for (int i = 0; i < n; i++) {
if (adj[i].size() % 2) D.push_back(i);
}
for (int i = 0; i < D.size() / 2; i++) {
V[m + i] = D[2 * i + 0];
U[m + i] = D[2 * i + 1];
adj[D[2 * i + 0]].push_back(m + i);
adj[D[2 * i + 1]].push_back(m + i);
}
for (int i = 0; i < n; i++) {
if (!mark[i]) dfs(i);
}
for (int i = 0; i < m; i++) cout << ans[i] << ' ';
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 55;
int V[maxn], U[maxn];
unordered_map<int, int> M;
vector<int> comp;
bitset<maxn> mark, emark, ans;
vector<int> adj[maxn];
void dfs(int v, int E = -1) {
mark[v] = 1;
for (auto e : adj[v]) {
if (!emark[e]) {
emark[e] = 1;
dfs(V[e] ^ U[e] ^ v, e);
}
}
if (~E && v == V[E]) ans[E] = 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> V[i] >> U[i];
U[i]++;
comp.push_back(V[i]);
comp.push_back(U[i]);
}
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
int n = comp.size();
for (int i = 0; i < n; i++) M[comp[i]] = i;
for (int i = 0; i < m; i++) {
V[i] = M[V[i]];
U[i] = M[U[i]];
adj[V[i]].push_back(i);
adj[U[i]].push_back(i);
}
vector<int> D;
for (int i = 0; i < n; i++) {
if (adj[i].size() % 2) D.push_back(i);
}
for (int i = 0; i < D.size() / 2; i++) {
V[m + i] = D[2 * i + 0];
U[m + i] = D[2 * i + 1];
adj[D[2 * i + 0]].push_back(m + i);
adj[D[2 * i + 1]].push_back(m + i);
}
for (int i = 0; i < n; i++) {
if (!mark[i]) dfs(i);
}
for (int i = 0; i < m; i++) cout << ans[i] << ' ';
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e60;
template <typename T>
inline void scf(T &x) {
bool f = 0;
x = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') {
f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (f) x = -x;
return;
}
template <typename T1, typename T2>
void scf(T1 &x, T2 &y) {
scf(x);
return scf(y);
}
template <typename T1, typename T2, typename T3>
void scf(T1 &x, T2 &y, T3 &z) {
scf(x);
scf(y);
return scf(z);
}
template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
scf(x);
scf(y);
scf(z);
return scf(w);
}
inline char mygetchar() {
char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
template <typename T>
void chkmax(T &x, const T &y) {
if (y > x) x = y;
return;
}
template <typename T>
void chkmin(T &x, const T &y) {
if (y < x) x = y;
return;
}
const int N = 1e5 + 100;
int n;
vector<int> g[N];
struct seg {
int l, r, id;
seg() {}
seg(int L, int R, int I) : l(L), r(R), id(I) {}
bool operator<(const seg &a) const {
return l == a.l ? (r == a.r ? id < a.id : r < a.r) : l < a.l;
}
};
set<seg> all;
int col[N];
void TZL() {
scf(n);
for (int i = (1); i <= (n); ++i) {
int l, r;
scf(l, r);
all.insert(seg(l, r, i));
col[i] = -1;
}
return;
}
void dfs(int u, int c = 0) {
col[u] = c;
for (int v : g[u])
if (!~col[v]) dfs(v, c ^ 1);
return;
}
void RANK1() {
while ((int)all.size() > 1) {
seg a = *all.begin();
all.erase(all.begin());
seg b = *all.begin();
all.erase(all.begin());
if (a.r < b.l) {
all.insert(b);
continue;
}
g[a.id].push_back(b.id);
g[b.id].push_back(a.id);
if (a.r > b.r) all.insert(seg(b.r + 1, a.r, a.id));
if (a.r < b.r) all.insert(seg(a.r + 1, b.r, b.id));
}
for (int i = (1); i <= (n); ++i) {
if (!~col[i]) dfs(i);
printf("%d ", col[i]);
}
return;
}
int main() {
TZL();
RANK1();
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e60;
template <typename T>
inline void scf(T &x) {
bool f = 0;
x = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') {
f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (f) x = -x;
return;
}
template <typename T1, typename T2>
void scf(T1 &x, T2 &y) {
scf(x);
return scf(y);
}
template <typename T1, typename T2, typename T3>
void scf(T1 &x, T2 &y, T3 &z) {
scf(x);
scf(y);
return scf(z);
}
template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
scf(x);
scf(y);
scf(z);
return scf(w);
}
inline char mygetchar() {
char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
template <typename T>
void chkmax(T &x, const T &y) {
if (y > x) x = y;
return;
}
template <typename T>
void chkmin(T &x, const T &y) {
if (y < x) x = y;
return;
}
const int N = 1e5 + 100;
int n;
vector<int> g[N];
struct seg {
int l, r, id;
seg() {}
seg(int L, int R, int I) : l(L), r(R), id(I) {}
bool operator<(const seg &a) const {
return l == a.l ? (r == a.r ? id < a.id : r < a.r) : l < a.l;
}
};
set<seg> all;
int col[N];
void TZL() {
scf(n);
for (int i = (1); i <= (n); ++i) {
int l, r;
scf(l, r);
all.insert(seg(l, r, i));
col[i] = -1;
}
return;
}
void dfs(int u, int c = 0) {
col[u] = c;
for (int v : g[u])
if (!~col[v]) dfs(v, c ^ 1);
return;
}
void RANK1() {
while ((int)all.size() > 1) {
seg a = *all.begin();
all.erase(all.begin());
seg b = *all.begin();
all.erase(all.begin());
if (a.r < b.l) {
all.insert(b);
continue;
}
g[a.id].push_back(b.id);
g[b.id].push_back(a.id);
if (a.r > b.r) all.insert(seg(b.r + 1, a.r, a.id));
if (a.r < b.r) all.insert(seg(a.r + 1, b.r, b.id));
}
for (int i = (1); i <= (n); ++i) {
if (!~col[i]) dfs(i);
printf("%d ", col[i]);
}
return;
}
int main() {
TZL();
RANK1();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 1e5 + 5;
const int IN = 1;
const int OUT = 0;
struct data {
int id, x, io;
data(int _id = 0, int _x = 0, int _io = 0) : id(_id), x(_x), io(_io) {}
bool operator<(const data& b) const { return x < b.x; }
} a[SIZE * 2];
int m = 0;
int to[SIZE * 2], an[SIZE];
int main() {
int(n);
scanf("%d", &n);
for (int i = 0; i < (n); ++i) {
int l, r;
scanf("%d%d", &l, &r);
a[m++] = data(i, l, IN);
a[m++] = data(i, r + 1, OUT);
}
sort(a, a + m);
for (int i = 0; i < m; i++)
to[a[i].id * 2 + a[i].io] = a[i ^ 1].id * 2 + a[i ^ 1].io;
for (int i = 0; i < n; i++) {
if (!an[i]) {
int now = i;
int me = IN;
an[i] = 1;
while (1) {
int tmp = to[now * 2 + me];
if (tmp / 2 == i) break;
if ((tmp & 1) == me) {
an[tmp >> 1] = -an[now];
} else {
an[tmp >> 1] = an[now];
}
now = tmp >> 1;
me = tmp & 1;
me ^= 1;
}
}
}
for (int i = 0; i < (n); ++i) {
if (i) putchar(' ');
if (an[i] == 1)
printf("1");
else
printf("0");
}
puts("");
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 1e5 + 5;
const int IN = 1;
const int OUT = 0;
struct data {
int id, x, io;
data(int _id = 0, int _x = 0, int _io = 0) : id(_id), x(_x), io(_io) {}
bool operator<(const data& b) const { return x < b.x; }
} a[SIZE * 2];
int m = 0;
int to[SIZE * 2], an[SIZE];
int main() {
int(n);
scanf("%d", &n);
for (int i = 0; i < (n); ++i) {
int l, r;
scanf("%d%d", &l, &r);
a[m++] = data(i, l, IN);
a[m++] = data(i, r + 1, OUT);
}
sort(a, a + m);
for (int i = 0; i < m; i++)
to[a[i].id * 2 + a[i].io] = a[i ^ 1].id * 2 + a[i ^ 1].io;
for (int i = 0; i < n; i++) {
if (!an[i]) {
int now = i;
int me = IN;
an[i] = 1;
while (1) {
int tmp = to[now * 2 + me];
if (tmp / 2 == i) break;
if ((tmp & 1) == me) {
an[tmp >> 1] = -an[now];
} else {
an[tmp >> 1] = an[now];
}
now = tmp >> 1;
me = tmp & 1;
me ^= 1;
}
}
}
for (int i = 0; i < (n); ++i) {
if (i) putchar(' ');
if (an[i] == 1)
printf("1");
else
printf("0");
}
puts("");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 600005;
int n, ct, cnt, e[Maxn], l[Maxn], r[Maxn], tmp[Maxn], deg[Maxn], head[Maxn];
struct edg {
int nxt, to;
bool dir;
} edge[2 * Maxn];
void add(int x, int y) {
deg[x]++;
edge[++cnt] = (edg){head[x], y};
head[x] = cnt;
}
void dfs(int u) {
for (int& i = head[u]; i; i = edge[i].nxt) {
int to = edge[i].to;
if (edge[i].dir || edge[((i - 1) ^ 1) + 1].dir) continue;
edge[i].dir = true;
dfs(to);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), r[i]++, tmp[++ct] = l[i], tmp[++ct] = r[i];
sort(tmp + 1, tmp + 1 + ct);
ct = unique(tmp + 1, tmp + 1 + ct) - tmp - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(tmp + 1, tmp + 1 + ct, l[i]) - tmp,
r[i] = lower_bound(tmp + 1, tmp + 1 + ct, r[i]) - tmp;
add(l[i], r[i]), add(r[i], l[i]);
e[i] = cnt - 1;
}
for (int i = ct - 1; i >= 1; i--) add(i + 1, i), add(i, i + 1);
for (int i = 1; i <= ct; i++)
if (deg[i] & 1) add(0, i), add(i, 0);
for (int i = 0; i <= ct; i++) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", (int)edge[e[i]].dir);
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 600005;
int n, ct, cnt, e[Maxn], l[Maxn], r[Maxn], tmp[Maxn], deg[Maxn], head[Maxn];
struct edg {
int nxt, to;
bool dir;
} edge[2 * Maxn];
void add(int x, int y) {
deg[x]++;
edge[++cnt] = (edg){head[x], y};
head[x] = cnt;
}
void dfs(int u) {
for (int& i = head[u]; i; i = edge[i].nxt) {
int to = edge[i].to;
if (edge[i].dir || edge[((i - 1) ^ 1) + 1].dir) continue;
edge[i].dir = true;
dfs(to);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), r[i]++, tmp[++ct] = l[i], tmp[++ct] = r[i];
sort(tmp + 1, tmp + 1 + ct);
ct = unique(tmp + 1, tmp + 1 + ct) - tmp - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(tmp + 1, tmp + 1 + ct, l[i]) - tmp,
r[i] = lower_bound(tmp + 1, tmp + 1 + ct, r[i]) - tmp;
add(l[i], r[i]), add(r[i], l[i]);
e[i] = cnt - 1;
}
for (int i = ct - 1; i >= 1; i--) add(i + 1, i), add(i, i + 1);
for (int i = 1; i <= ct; i++)
if (deg[i] & 1) add(0, i), add(i, 0);
for (int i = 0; i <= ct; i++) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", (int)edge[e[i]].dir);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 10;
struct edge {
int y;
list<edge>::iterator rev;
edge(int y) : y(y) {}
};
list<edge> g[MAXN];
int pa[MAXN];
int find(int x) { return pa[x] = pa[x] == x ? x : find(pa[x]); }
void join(int x, int y) { pa[find(x)] = find(y); }
void add_edge(int a, int b) {
join(a, b);
g[a].push_front(edge(b));
auto ia = g[a].begin();
g[b].push_front(edge(a));
auto ib = g[b].begin();
ia->rev = ib;
ib->rev = ia;
}
vector<int> p;
void go(int x) {
while (g[x].size()) {
int y = g[x].front().y;
g[y].erase(g[x].front().rev);
g[x].pop_front();
go(y);
}
p.push_back(x);
}
vector<int> get_path(int x) {
p.clear();
go(x);
reverse(p.begin(), p.end());
return p;
}
map<pair<int, int>, int> mp;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<int, int> > v(n);
vector<int> vs = {-1, 1000000001};
for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
int x, y;
cin >> x >> y;
y++;
v[i] = {x, y};
vs.push_back(x);
vs.push_back(y);
}
sort(vs.begin(), vs.end());
vs.erase(unique(vs.begin(), vs.end()), vs.end());
int k = int(vs.size());
for (int i = 0, ThxDem = k; i < ThxDem; ++i) pa[i] = i;
vector<int> deg(k);
for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
int x = lower_bound(vs.begin(), vs.end(), v[i].first) - vs.begin();
int y = lower_bound(vs.begin(), vs.end(), v[i].second) - vs.begin();
mp[{x, y}] = i;
add_edge(x, y);
deg[x]++;
deg[y]++;
}
int wh = -1;
for (int i = 0, ThxDem = k; i < ThxDem; ++i)
if (deg[i] & 1) {
if (wh < 0)
wh = i;
else {
add_edge(wh, i);
wh = -1;
}
}
assert(wh < 0);
vector<int> res(n);
vector<int> did(k);
for (int i = 0, ThxDem = k; i < ThxDem; ++i)
if (!did[find(i)]) {
did[find(i)] = 1;
auto ans = get_path(i);
for (int j = 0, ThxDem = int(ans.size()); j < ThxDem; ++j) {
int x = ans[j], y = ans[(j + 1) % int(ans.size())];
if (mp.count({x, y})) res[mp[{x, y}]] = 1;
}
}
for (auto x : res) cout << x << " ";
cout << "\n";
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 10;
struct edge {
int y;
list<edge>::iterator rev;
edge(int y) : y(y) {}
};
list<edge> g[MAXN];
int pa[MAXN];
int find(int x) { return pa[x] = pa[x] == x ? x : find(pa[x]); }
void join(int x, int y) { pa[find(x)] = find(y); }
void add_edge(int a, int b) {
join(a, b);
g[a].push_front(edge(b));
auto ia = g[a].begin();
g[b].push_front(edge(a));
auto ib = g[b].begin();
ia->rev = ib;
ib->rev = ia;
}
vector<int> p;
void go(int x) {
while (g[x].size()) {
int y = g[x].front().y;
g[y].erase(g[x].front().rev);
g[x].pop_front();
go(y);
}
p.push_back(x);
}
vector<int> get_path(int x) {
p.clear();
go(x);
reverse(p.begin(), p.end());
return p;
}
map<pair<int, int>, int> mp;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<int, int> > v(n);
vector<int> vs = {-1, 1000000001};
for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
int x, y;
cin >> x >> y;
y++;
v[i] = {x, y};
vs.push_back(x);
vs.push_back(y);
}
sort(vs.begin(), vs.end());
vs.erase(unique(vs.begin(), vs.end()), vs.end());
int k = int(vs.size());
for (int i = 0, ThxDem = k; i < ThxDem; ++i) pa[i] = i;
vector<int> deg(k);
for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
int x = lower_bound(vs.begin(), vs.end(), v[i].first) - vs.begin();
int y = lower_bound(vs.begin(), vs.end(), v[i].second) - vs.begin();
mp[{x, y}] = i;
add_edge(x, y);
deg[x]++;
deg[y]++;
}
int wh = -1;
for (int i = 0, ThxDem = k; i < ThxDem; ++i)
if (deg[i] & 1) {
if (wh < 0)
wh = i;
else {
add_edge(wh, i);
wh = -1;
}
}
assert(wh < 0);
vector<int> res(n);
vector<int> did(k);
for (int i = 0, ThxDem = k; i < ThxDem; ++i)
if (!did[find(i)]) {
did[find(i)] = 1;
auto ans = get_path(i);
for (int j = 0, ThxDem = int(ans.size()); j < ThxDem; ++j) {
int x = ans[j], y = ans[(j + 1) % int(ans.size())];
if (mp.count({x, y})) res[mp[{x, y}]] = 1;
}
}
for (auto x : res) cout << x << " ";
cout << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
int P[2 * 110000], X[110000], Y[110000], use[2 * 110000], n, tot,
vis[2 * 110000], du[2 * 110000];
vector<pair<int, int> > E[2 * 110000];
void dfs(int x) {
vis[x] = 1;
for (auto i : E[x])
if (!use[i.second]) {
if (i.first > x)
use[i.second] = 1;
else
use[i.second] = -1;
dfs(i.first);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &X[i], &Y[i]);
P[++tot] = X[i];
P[++tot] = Y[i] + 1;
}
sort(P + 1, P + tot + 1);
tot = unique(P + 1, P + tot + 1) - P - 1;
for (int i = 1; i <= n; ++i) {
auto Find = [](int x) { return lower_bound(P + 1, P + tot + 1, x) - P; };
int l = Find(X[i]), r = Find(Y[i] + 1);
E[l].push_back(make_pair(r, i));
E[r].push_back(make_pair(l, i));
du[l]++;
du[r]++;
}
int cnt = n, last = 0;
for (int i = 1; i <= tot; ++i)
if (du[i] & 1) {
if (last)
E[last].push_back(make_pair(i, ++cnt)),
E[i].push_back(make_pair(last, cnt)), last = 0;
else
last = i;
}
for (int i = 1; i <= tot; ++i)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", max(use[i], 0));
}
|
### Prompt
Create a solution in cpp for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
int P[2 * 110000], X[110000], Y[110000], use[2 * 110000], n, tot,
vis[2 * 110000], du[2 * 110000];
vector<pair<int, int> > E[2 * 110000];
void dfs(int x) {
vis[x] = 1;
for (auto i : E[x])
if (!use[i.second]) {
if (i.first > x)
use[i.second] = 1;
else
use[i.second] = -1;
dfs(i.first);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &X[i], &Y[i]);
P[++tot] = X[i];
P[++tot] = Y[i] + 1;
}
sort(P + 1, P + tot + 1);
tot = unique(P + 1, P + tot + 1) - P - 1;
for (int i = 1; i <= n; ++i) {
auto Find = [](int x) { return lower_bound(P + 1, P + tot + 1, x) - P; };
int l = Find(X[i]), r = Find(Y[i] + 1);
E[l].push_back(make_pair(r, i));
E[r].push_back(make_pair(l, i));
du[l]++;
du[r]++;
}
int cnt = n, last = 0;
for (int i = 1; i <= tot; ++i)
if (du[i] & 1) {
if (last)
E[last].push_back(make_pair(i, ++cnt)),
E[i].push_back(make_pair(last, cnt)), last = 0;
else
last = i;
}
for (int i = 1; i <= tot; ++i)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", max(use[i], 0));
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbs(string str, T t) {
cerr << str << " : " << t << "\n";
}
template <class T, class... S>
void dbs(string str, T t, S... s) {
int idx = str.find(',');
cerr << str.substr(0, idx) << " : " << t << ",";
dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const map<S, T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
void prc(T a, T b) {
cerr << "[";
for (T i = a; i != b; ++i) {
if (i != a) cerr << ", ";
cerr << *i;
}
cerr << "]\n";
}
vector<pair<pair<int, int>, int>> intervals;
vector<int> Adj[100010];
bool visited[100010];
int colorNode[100010];
bool poss = true;
void DFS(int u, int p, int color) {
visited[u] = true;
colorNode[u] = color;
for (auto v : Adj[u]) {
if (!visited[v]) {
DFS(v, u, 1 - color);
} else if (visited[v]) {
if (colorNode[v] != 1 - color) {
poss = false;
}
}
}
}
int main() {
int n;
cin >> n;
for (int i = (int)(1); i <= (int)(n); i++) {
int l1, r1;
cin >> l1 >> r1;
intervals.push_back(make_pair(make_pair(l1, r1), i));
}
auto comp = [](const pair<pair<int, int>, int>& a,
const pair<pair<int, int>, int>& b) {
if (a.first.first != b.first.first) return a.first.first > b.first.first;
return a.second > b.second;
};
priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>,
decltype(comp)>
Q1(comp);
for (auto interval : intervals) {
Q1.push(interval);
}
while (!Q1.empty()) {
pair<pair<int, int>, int> u = Q1.top();
Q1.pop();
if (Q1.empty()) break;
pair<pair<int, int>, int> v = Q1.top();
Q1.pop();
if (u.first.second < v.first.first) {
Q1.push(v);
continue;
}
if (u.first.second >= v.first.first) {
Adj[u.second].push_back(v.second), Adj[v.second].push_back(u.second);
}
if (u.first.second > v.first.second) {
Q1.push(
make_pair(make_pair(v.first.second + 1, u.first.second), u.second));
} else if (u.first.second < v.first.second) {
Q1.push(
make_pair(make_pair(u.first.second + 1, v.first.second), v.second));
}
}
for (int i = (int)(1); i <= (int)(n); i++) {
if (!visited[i]) {
DFS(i, 0, 0);
}
}
if (!poss) {
cout << "-1\n";
} else {
for (int i = (int)(1); i <= (int)(n); i++) {
cout << colorNode[i] << " ";
}
cout << "\n";
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbs(string str, T t) {
cerr << str << " : " << t << "\n";
}
template <class T, class... S>
void dbs(string str, T t, S... s) {
int idx = str.find(',');
cerr << str.substr(0, idx) << " : " << t << ",";
dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const map<S, T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
void prc(T a, T b) {
cerr << "[";
for (T i = a; i != b; ++i) {
if (i != a) cerr << ", ";
cerr << *i;
}
cerr << "]\n";
}
vector<pair<pair<int, int>, int>> intervals;
vector<int> Adj[100010];
bool visited[100010];
int colorNode[100010];
bool poss = true;
void DFS(int u, int p, int color) {
visited[u] = true;
colorNode[u] = color;
for (auto v : Adj[u]) {
if (!visited[v]) {
DFS(v, u, 1 - color);
} else if (visited[v]) {
if (colorNode[v] != 1 - color) {
poss = false;
}
}
}
}
int main() {
int n;
cin >> n;
for (int i = (int)(1); i <= (int)(n); i++) {
int l1, r1;
cin >> l1 >> r1;
intervals.push_back(make_pair(make_pair(l1, r1), i));
}
auto comp = [](const pair<pair<int, int>, int>& a,
const pair<pair<int, int>, int>& b) {
if (a.first.first != b.first.first) return a.first.first > b.first.first;
return a.second > b.second;
};
priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>,
decltype(comp)>
Q1(comp);
for (auto interval : intervals) {
Q1.push(interval);
}
while (!Q1.empty()) {
pair<pair<int, int>, int> u = Q1.top();
Q1.pop();
if (Q1.empty()) break;
pair<pair<int, int>, int> v = Q1.top();
Q1.pop();
if (u.first.second < v.first.first) {
Q1.push(v);
continue;
}
if (u.first.second >= v.first.first) {
Adj[u.second].push_back(v.second), Adj[v.second].push_back(u.second);
}
if (u.first.second > v.first.second) {
Q1.push(
make_pair(make_pair(v.first.second + 1, u.first.second), u.second));
} else if (u.first.second < v.first.second) {
Q1.push(
make_pair(make_pair(u.first.second + 1, v.first.second), v.second));
}
}
for (int i = (int)(1); i <= (int)(n); i++) {
if (!visited[i]) {
DFS(i, 0, 0);
}
}
if (!poss) {
cout << "-1\n";
} else {
for (int i = (int)(1); i <= (int)(n); i++) {
cout << colorNode[i] << " ";
}
cout << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, id, op;
} a[200002];
struct edge {
int to, nxxt;
} e[200002 << 3];
int n, tot, js, head[200002 << 2], cnt = 2, dfn[200002], low[200002], f[200002],
cn, an;
bool inq[200002];
stack<int> s;
inline void ins(int u, int v) {
e[cnt] = (edge){v, head[u]};
head[u] = cnt++;
}
inline bool cmp(node n1, node n2) {
if (n1.x ^ n2.x)
return n1.x < n2.x;
else
return n1.op > n2.op;
}
void tarjan(int x) {
dfn[x] = low[x] = ++cn;
inq[x] = 1;
s.push(x);
for (int i = head[x]; i; i = e[i].nxxt) {
int j = e[i].to;
if (!dfn[j])
tarjan(j), low[x] = min(low[x], low[j]);
else if (inq[j])
low[x] = min(low[x], dfn[j]);
}
if (dfn[x] == low[x]) {
an++;
while (!s.empty() && s.top() != x) {
inq[s.top()] = 0;
f[s.top()] = an;
s.pop();
}
inq[x] = 0;
f[x] = an;
s.pop();
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d%d", &x, &y);
a[++tot] = (node){x, i, 1};
a[++tot] = (node){y, i, -1};
}
sort(a + 1, a + 1 + tot, cmp);
int su = 0;
for (int i = 2; i <= tot; i += 2) {
if (a[i].op != a[i - 1].op) {
ins(a[i].id, a[i - 1].id);
ins(a[i - 1].id, a[i].id);
ins(a[i].id + n, a[i - 1].id + n);
ins(a[i - 1].id + n, a[i].id + n);
} else {
ins(a[i].id, a[i - 1].id + n);
ins(a[i].id + n, a[i - 1].id);
ins(a[i - 1].id, a[i].id + n);
ins(a[i - 1].id + n, a[i].id);
}
}
for (int i = 1; i <= 2 * n; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= n; i++) printf("%d ", f[i] < f[i + n]);
puts("");
}
|
### Prompt
Please create a solution in CPP to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, id, op;
} a[200002];
struct edge {
int to, nxxt;
} e[200002 << 3];
int n, tot, js, head[200002 << 2], cnt = 2, dfn[200002], low[200002], f[200002],
cn, an;
bool inq[200002];
stack<int> s;
inline void ins(int u, int v) {
e[cnt] = (edge){v, head[u]};
head[u] = cnt++;
}
inline bool cmp(node n1, node n2) {
if (n1.x ^ n2.x)
return n1.x < n2.x;
else
return n1.op > n2.op;
}
void tarjan(int x) {
dfn[x] = low[x] = ++cn;
inq[x] = 1;
s.push(x);
for (int i = head[x]; i; i = e[i].nxxt) {
int j = e[i].to;
if (!dfn[j])
tarjan(j), low[x] = min(low[x], low[j]);
else if (inq[j])
low[x] = min(low[x], dfn[j]);
}
if (dfn[x] == low[x]) {
an++;
while (!s.empty() && s.top() != x) {
inq[s.top()] = 0;
f[s.top()] = an;
s.pop();
}
inq[x] = 0;
f[x] = an;
s.pop();
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d%d", &x, &y);
a[++tot] = (node){x, i, 1};
a[++tot] = (node){y, i, -1};
}
sort(a + 1, a + 1 + tot, cmp);
int su = 0;
for (int i = 2; i <= tot; i += 2) {
if (a[i].op != a[i - 1].op) {
ins(a[i].id, a[i - 1].id);
ins(a[i - 1].id, a[i].id);
ins(a[i].id + n, a[i - 1].id + n);
ins(a[i - 1].id + n, a[i].id + n);
} else {
ins(a[i].id, a[i - 1].id + n);
ins(a[i].id + n, a[i - 1].id);
ins(a[i - 1].id, a[i].id + n);
ins(a[i - 1].id + n, a[i].id);
}
}
for (int i = 1; i <= 2 * n; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= n; i++) printf("%d ", f[i] < f[i + n]);
puts("");
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
const int maxn = 2e5 + 1e2;
struct node {
int l, r;
} s[maxn];
struct Edge {
int to, nxt, id;
} e[maxn << 1];
int n;
int tot, Cnt = 1;
int b[maxn << 1], head[maxn], cnt[maxn], Ans[maxn], vis[maxn], Vis[maxn << 1];
vector<int> S;
inline void file() {
freopen("CF429E.in", "r", stdin);
freopen("CF429E.out", "w", stdout);
}
inline int read() {
int x = 0, p = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') p = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c - '0');
c = getchar();
}
return x * p;
}
inline void add(int from, int to, int p) {
e[++Cnt] = (Edge){to, head[from], p};
head[from] = Cnt;
e[++Cnt] = (Edge){from, head[to], p};
head[to] = Cnt;
}
void dfs(int u) {
vis[u] = 1;
for (int i = head[u]; i != 0; i = e[i].nxt)
if (!Vis[i / 2]) {
Vis[i / 2] = 1;
if (i & 1)
Ans[e[i].id] = 1;
else
Ans[e[i].id] = 0;
dfs(e[i].to);
}
}
int main() {
n = read();
for (int i = (int)1; i <= (int)n; ++i) {
s[i].l = read();
s[i].r = read();
++s[i].r;
b[++tot] = s[i].l;
b[++tot] = s[i].r;
}
sort(b + 1, b + tot + 1);
tot = unique(b + 1, b + tot + 1) - (b + 1);
for (int i = (int)1; i <= (int)n; ++i) {
int x = lower_bound(b + 1, b + tot + 1, s[i].l) - b;
int y = lower_bound(b + 1, b + tot + 1, s[i].r) - b;
add(x, y, i);
++cnt[x];
++cnt[y];
}
for (int i = (int)1; i <= (int)tot; ++i)
if (cnt[i] & 1) S.push_back(i);
for (int i = 0; i < (int)S.size() - 1; i += 2) add(S[i], S[i + 1], 0);
for (int i = (int)1; i <= (int)tot; ++i)
if (!vis[i]) dfs(i);
for (int i = (int)1; i <= (int)n; ++i) printf("%d ", Ans[i]);
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
const int maxn = 2e5 + 1e2;
struct node {
int l, r;
} s[maxn];
struct Edge {
int to, nxt, id;
} e[maxn << 1];
int n;
int tot, Cnt = 1;
int b[maxn << 1], head[maxn], cnt[maxn], Ans[maxn], vis[maxn], Vis[maxn << 1];
vector<int> S;
inline void file() {
freopen("CF429E.in", "r", stdin);
freopen("CF429E.out", "w", stdout);
}
inline int read() {
int x = 0, p = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') p = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c - '0');
c = getchar();
}
return x * p;
}
inline void add(int from, int to, int p) {
e[++Cnt] = (Edge){to, head[from], p};
head[from] = Cnt;
e[++Cnt] = (Edge){from, head[to], p};
head[to] = Cnt;
}
void dfs(int u) {
vis[u] = 1;
for (int i = head[u]; i != 0; i = e[i].nxt)
if (!Vis[i / 2]) {
Vis[i / 2] = 1;
if (i & 1)
Ans[e[i].id] = 1;
else
Ans[e[i].id] = 0;
dfs(e[i].to);
}
}
int main() {
n = read();
for (int i = (int)1; i <= (int)n; ++i) {
s[i].l = read();
s[i].r = read();
++s[i].r;
b[++tot] = s[i].l;
b[++tot] = s[i].r;
}
sort(b + 1, b + tot + 1);
tot = unique(b + 1, b + tot + 1) - (b + 1);
for (int i = (int)1; i <= (int)n; ++i) {
int x = lower_bound(b + 1, b + tot + 1, s[i].l) - b;
int y = lower_bound(b + 1, b + tot + 1, s[i].r) - b;
add(x, y, i);
++cnt[x];
++cnt[y];
}
for (int i = (int)1; i <= (int)tot; ++i)
if (cnt[i] & 1) S.push_back(i);
for (int i = 0; i < (int)S.size() - 1; i += 2) add(S[i], S[i + 1], 0);
for (int i = (int)1; i <= (int)tot; ++i)
if (!vis[i]) dfs(i);
for (int i = (int)1; i <= (int)n; ++i) printf("%d ", Ans[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int head[200001], col[200001];
struct {
int v, link;
} e[2 * 200001];
int n, m;
void addEdge(int u, int v) {
e[m] = {v, head[u]};
head[u] = m++;
}
void dfs(int u, int c) {
if (col[u] != -1) return;
col[u] = c;
for (int i = head[u]; i != -1; i = e[i].link) dfs(e[i].v, 1 - c);
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
vector<pair<int, int> > list;
memset(head, -1, sizeof(head));
for (int i = 0; i < n; ++i) {
int l, r;
cin >> l >> r;
list.push_back(make_pair(l * 2, i * 2));
list.push_back(make_pair(r * 2 + 1, i * 2 + 1));
addEdge(i * 2, i * 2 + 1);
addEdge(i * 2 + 1, i * 2);
}
sort(list.begin(), list.end());
for (int i = 0; i < 2 * n; i += 2) {
addEdge(list[i].second, list[i + 1].second);
addEdge(list[i + 1].second, list[i].second);
}
memset(col, -1, sizeof(col));
for (int i = 0; i < n * 2; ++i)
if (col[i] < 0) dfs(i, 0);
for (int i = 0; i < n; ++i) cout << col[i * 2] << " ";
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int head[200001], col[200001];
struct {
int v, link;
} e[2 * 200001];
int n, m;
void addEdge(int u, int v) {
e[m] = {v, head[u]};
head[u] = m++;
}
void dfs(int u, int c) {
if (col[u] != -1) return;
col[u] = c;
for (int i = head[u]; i != -1; i = e[i].link) dfs(e[i].v, 1 - c);
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
vector<pair<int, int> > list;
memset(head, -1, sizeof(head));
for (int i = 0; i < n; ++i) {
int l, r;
cin >> l >> r;
list.push_back(make_pair(l * 2, i * 2));
list.push_back(make_pair(r * 2 + 1, i * 2 + 1));
addEdge(i * 2, i * 2 + 1);
addEdge(i * 2 + 1, i * 2);
}
sort(list.begin(), list.end());
for (int i = 0; i < 2 * n; i += 2) {
addEdge(list[i].second, list[i + 1].second);
addEdge(list[i + 1].second, list[i].second);
}
memset(col, -1, sizeof(col));
for (int i = 0; i < n * 2; ++i)
if (col[i] < 0) dfs(i, 0);
for (int i = 0; i < n; ++i) cout << col[i * 2] << " ";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int i, j, k, n, m;
struct Section {
int l, r;
} z[N];
int store[N * 2], tot;
bool cmp(int a, int b) { return a < b; }
int disva = 0;
map<int, int> s;
struct Edge {
int y, l, id;
bool enable;
} f[N * 4];
int g[N * 2], In[N * 2], T;
void Ins(int x, int y, int id) {
f[++T].y = y, f[T].l = g[x], g[x] = T;
In[x]++, In[y]++;
f[++T].y = x, f[T].l = g[y], g[y] = T;
f[T].enable = f[T - 1].enable = 1;
f[T].id = f[T - 1].id = id;
}
bool v[N * 2];
int a[N * 2], t, b[N * 2], tt, ans[N];
void Euler(int po) {
for (int k = g[po]; k; k = f[k].l)
if (f[k].enable) {
f[k].enable = f[k ^ 1].enable = 0;
ans[f[k].id] = (f[k].y > po);
Euler(f[k].y);
}
}
int main() {
T = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &z[i].l, &z[i].r);
z[i].r++;
store[++tot] = z[i].l, store[++tot] = z[i].r;
}
sort(store + 1, store + 1 + tot, cmp);
disva = 0;
store[0] = -2;
for (i = 1; i <= tot; i++)
if (store[i] != store[i - 1]) {
s[store[i]] = ++disva;
}
for (i = 1; i <= n; i++) z[i].l = s[z[i].l], z[i].r = s[z[i].r];
m = disva;
for (i = 1; i <= n; i++) {
Ins(z[i].l, z[i].r, i);
}
for (i = 1; i <= m; i++)
if (In[i] & 1) {
b[++tt] = i;
}
sort(b + 1, b + 1 + tt, cmp);
for (i = 1; i <= tt - 1; i += 2) Ins(b[i], b[i + 1], 0);
for (i = 1; i <= m; i++) {
Euler(i);
}
for (i = 1; i <= n; i++) printf("%d ", ans[i]);
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int i, j, k, n, m;
struct Section {
int l, r;
} z[N];
int store[N * 2], tot;
bool cmp(int a, int b) { return a < b; }
int disva = 0;
map<int, int> s;
struct Edge {
int y, l, id;
bool enable;
} f[N * 4];
int g[N * 2], In[N * 2], T;
void Ins(int x, int y, int id) {
f[++T].y = y, f[T].l = g[x], g[x] = T;
In[x]++, In[y]++;
f[++T].y = x, f[T].l = g[y], g[y] = T;
f[T].enable = f[T - 1].enable = 1;
f[T].id = f[T - 1].id = id;
}
bool v[N * 2];
int a[N * 2], t, b[N * 2], tt, ans[N];
void Euler(int po) {
for (int k = g[po]; k; k = f[k].l)
if (f[k].enable) {
f[k].enable = f[k ^ 1].enable = 0;
ans[f[k].id] = (f[k].y > po);
Euler(f[k].y);
}
}
int main() {
T = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &z[i].l, &z[i].r);
z[i].r++;
store[++tot] = z[i].l, store[++tot] = z[i].r;
}
sort(store + 1, store + 1 + tot, cmp);
disva = 0;
store[0] = -2;
for (i = 1; i <= tot; i++)
if (store[i] != store[i - 1]) {
s[store[i]] = ++disva;
}
for (i = 1; i <= n; i++) z[i].l = s[z[i].l], z[i].r = s[z[i].r];
m = disva;
for (i = 1; i <= n; i++) {
Ins(z[i].l, z[i].r, i);
}
for (i = 1; i <= m; i++)
if (In[i] & 1) {
b[++tt] = i;
}
sort(b + 1, b + 1 + tt, cmp);
for (i = 1; i <= tt - 1; i += 2) Ins(b[i], b[i + 1], 0);
for (i = 1; i <= m; i++) {
Euler(i);
}
for (i = 1; i <= n; i++) printf("%d ", ans[i]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, cnt, now, id;
int l[N + 10], r[N + 10], a[(N << 1) + 10], degree[(N << 1) + 10], ans[N + 10];
bool vis[(N << 1) + 10], vis2[(N << 1) + 10];
struct edge {
int v, id;
edge(int v_, int id_) : v(v_), id(id_) {}
};
vector<edge> g[(N << 1) + 10];
void dfs(int u) {
vis[u] = 1;
int siz = g[u].size();
for (int i = 0; i < siz; i++) {
int uid = g[u][i].id, v = g[u][i].v;
if (vis2[uid]) continue;
vis2[uid] = 1;
if (uid <= n) ans[uid] = (u < v);
dfs(v);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), r[i]++, a[(i << 1) - 1] = l[i],
a[i << 1] = r[i];
sort(a + 1, a + (n << 1 | 1));
cnt = unique(a + 1, a + (n << 1 | 1)) - a - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(a + 1, a + cnt + 1, l[i]) - a;
r[i] = lower_bound(a + 1, a + cnt + 1, r[i]) - a;
degree[l[i]]++;
degree[r[i]]++;
g[l[i]].push_back(edge(r[i], i));
g[r[i]].push_back(edge(l[i], i));
}
id = n;
for (int i = 1; i <= cnt; i++)
if (degree[i] & 1) {
if (now)
g[now].push_back(edge(i, ++id)), g[i].push_back(edge(now, id)),
degree[now]++, degree[i]++, now = 0;
else
now = i;
}
for (int i = 1; i <= cnt; i++)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, cnt, now, id;
int l[N + 10], r[N + 10], a[(N << 1) + 10], degree[(N << 1) + 10], ans[N + 10];
bool vis[(N << 1) + 10], vis2[(N << 1) + 10];
struct edge {
int v, id;
edge(int v_, int id_) : v(v_), id(id_) {}
};
vector<edge> g[(N << 1) + 10];
void dfs(int u) {
vis[u] = 1;
int siz = g[u].size();
for (int i = 0; i < siz; i++) {
int uid = g[u][i].id, v = g[u][i].v;
if (vis2[uid]) continue;
vis2[uid] = 1;
if (uid <= n) ans[uid] = (u < v);
dfs(v);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), r[i]++, a[(i << 1) - 1] = l[i],
a[i << 1] = r[i];
sort(a + 1, a + (n << 1 | 1));
cnt = unique(a + 1, a + (n << 1 | 1)) - a - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(a + 1, a + cnt + 1, l[i]) - a;
r[i] = lower_bound(a + 1, a + cnt + 1, r[i]) - a;
degree[l[i]]++;
degree[r[i]]++;
g[l[i]].push_back(edge(r[i], i));
g[r[i]].push_back(edge(l[i], i));
}
id = n;
for (int i = 1; i <= cnt; i++)
if (degree[i] & 1) {
if (now)
g[now].push_back(edge(i, ++id)), g[i].push_back(edge(now, id)),
degree[now]++, degree[i]++, now = 0;
else
now = i;
}
for (int i = 1; i <= cnt; i++)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)2e5 + 10;
int l[maxn], r[maxn], x[maxn];
bool used[maxn], edge[maxn];
int col[maxn];
vector<pair<int, int> > g[maxn];
void dfs(int v) {
for (int i = 0; i < (int)(g[v]).size(); i++) {
int to = g[v][i].first;
int id = g[v][i].second;
if (edge[id]) continue;
edge[id] = true;
col[id] = v < to;
dfs(to);
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d%d", &l[i], &r[i]);
r[i]++;
x[2 * i] = l[i];
x[2 * i + 1] = r[i];
}
sort(x, x + 2 * n);
int m = unique(x, x + 2 * n) - x;
for (int i = 0; i < n; i++) {
int L = lower_bound(x, x + m, l[i]) - x;
int R = lower_bound(x, x + m, r[i]) - x;
g[L].push_back(make_pair(R, i));
g[R].push_back(make_pair(L, i));
}
int prev = -1;
int last = n;
for (int i = 0; i < m; i++) {
if ((int)(g[i]).size() % 2 == 1) {
if (prev == -1) {
prev = i;
} else {
g[prev].push_back(make_pair(i, last));
g[i].push_back(make_pair(prev, last));
last++;
prev = -1;
}
}
}
for (int i = 0; i < m; i++) {
dfs(i);
}
for (int i = 0; i < n; i++) {
printf("%d ", col[i]);
}
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)2e5 + 10;
int l[maxn], r[maxn], x[maxn];
bool used[maxn], edge[maxn];
int col[maxn];
vector<pair<int, int> > g[maxn];
void dfs(int v) {
for (int i = 0; i < (int)(g[v]).size(); i++) {
int to = g[v][i].first;
int id = g[v][i].second;
if (edge[id]) continue;
edge[id] = true;
col[id] = v < to;
dfs(to);
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d%d", &l[i], &r[i]);
r[i]++;
x[2 * i] = l[i];
x[2 * i + 1] = r[i];
}
sort(x, x + 2 * n);
int m = unique(x, x + 2 * n) - x;
for (int i = 0; i < n; i++) {
int L = lower_bound(x, x + m, l[i]) - x;
int R = lower_bound(x, x + m, r[i]) - x;
g[L].push_back(make_pair(R, i));
g[R].push_back(make_pair(L, i));
}
int prev = -1;
int last = n;
for (int i = 0; i < m; i++) {
if ((int)(g[i]).size() % 2 == 1) {
if (prev == -1) {
prev = i;
} else {
g[prev].push_back(make_pair(i, last));
g[i].push_back(make_pair(prev, last));
last++;
prev = -1;
}
}
}
for (int i = 0; i < m; i++) {
dfs(i);
}
for (int i = 0; i < n; i++) {
printf("%d ", col[i]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,popcnt")
using namespace std;
struct data {
int x, id;
bool operator<(const data &rhs) const { return x < rhs.x; }
} ds[200010];
int val[200010], lef[200010], righ[200010], n;
bool vis[200010];
vector<int> nxt[200010];
map<pair<int, int>, int> ban, ans;
template <class T>
void read(T &x) {
char ch = x = 0;
bool fl = false;
while (!isdigit(ch)) fl |= ch == '-', ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x = fl ? -x : x;
}
void dfs(int u) {
for (auto &v : nxt[u]) {
if (!ban[{u, v}]) {
ban[{u, v}] = ban[{v, u}] = true;
ans[{u, v}] = 1, dfs(v);
}
}
}
int main() {
read(n);
for (int i = 1; i <= n; i++) {
read(ds[i].x), read(ds[n + i].x), ds[n + i].x++;
ds[i].id = i, ds[n + i].id = n + i;
}
sort(ds + 1, ds + 2 * n + 1), ds[0].x = -1;
for (int i = 1; i <= 2 * n; i++) {
val[ds[i].id] = val[ds[i - 1].id] + (ds[i].id != ds[i - 1].id);
(ds[i].id <= n ? lef[ds[i].id] : righ[ds[i].id - n]) = val[ds[i].id];
}
for (int i = 1; i <= n; i++) {
nxt[lef[i]].push_back(righ[i]), nxt[righ[i]].push_back(lef[i]);
}
for (int i = 1, last = 0; i <= val[ds[2 * n].id]; i++) {
if (nxt[i].size() & 1) {
if (last)
nxt[last].push_back(i), nxt[i].push_back(last), last = 0;
else
last = i;
}
}
for (int i = 1; i <= 2 * n; i++) {
if (!vis[i]) dfs(i);
}
for (int i = 1; i <= n; i++) {
printf("%d%c", ans[{lef[i], righ[i]}], i == n ? '\n' : ' ');
}
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,popcnt")
using namespace std;
struct data {
int x, id;
bool operator<(const data &rhs) const { return x < rhs.x; }
} ds[200010];
int val[200010], lef[200010], righ[200010], n;
bool vis[200010];
vector<int> nxt[200010];
map<pair<int, int>, int> ban, ans;
template <class T>
void read(T &x) {
char ch = x = 0;
bool fl = false;
while (!isdigit(ch)) fl |= ch == '-', ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x = fl ? -x : x;
}
void dfs(int u) {
for (auto &v : nxt[u]) {
if (!ban[{u, v}]) {
ban[{u, v}] = ban[{v, u}] = true;
ans[{u, v}] = 1, dfs(v);
}
}
}
int main() {
read(n);
for (int i = 1; i <= n; i++) {
read(ds[i].x), read(ds[n + i].x), ds[n + i].x++;
ds[i].id = i, ds[n + i].id = n + i;
}
sort(ds + 1, ds + 2 * n + 1), ds[0].x = -1;
for (int i = 1; i <= 2 * n; i++) {
val[ds[i].id] = val[ds[i - 1].id] + (ds[i].id != ds[i - 1].id);
(ds[i].id <= n ? lef[ds[i].id] : righ[ds[i].id - n]) = val[ds[i].id];
}
for (int i = 1; i <= n; i++) {
nxt[lef[i]].push_back(righ[i]), nxt[righ[i]].push_back(lef[i]);
}
for (int i = 1, last = 0; i <= val[ds[2 * n].id]; i++) {
if (nxt[i].size() & 1) {
if (last)
nxt[last].push_back(i), nxt[i].push_back(last), last = 0;
else
last = i;
}
}
for (int i = 1; i <= 2 * n; i++) {
if (!vis[i]) dfs(i);
}
for (int i = 1; i <= n; i++) {
printf("%d%c", ans[{lef[i], righ[i]}], i == n ? '\n' : ' ');
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, tot, sum[200010];
int b[200010], l[200010], r[200010];
struct Node {
int to, nxt, id;
} Edge[200010 << 1];
int Head[200010], cnt_Edge = 1;
void Add_Edge(int u, int v, int id) {
Edge[++cnt_Edge] = (Node){v, Head[u], id};
Head[u] = cnt_Edge;
}
int ans[200010], vis[200010 << 1], fl[200010];
void dfs(int u) {
fl[u] = 1;
for (int i = Head[u]; i; i = Edge[i].nxt) {
int v = Edge[i].to, w = Edge[i].id;
if (vis[i]) continue;
if (u < v)
ans[w] = 0;
else
ans[w] = 1;
vis[i] = vis[i ^ 1] = 1;
dfs(v);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &l[i], &r[i]);
r[i]++;
b[++tot] = l[i];
b[++tot] = r[i];
}
sort(b + 1, b + tot + 1);
tot = unique(b + 1, b + tot + 1) - b - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(b + 1, b + tot + 1, l[i]) - b;
r[i] = lower_bound(b + 1, b + tot + 1, r[i]) - b;
sum[l[i]]++;
sum[r[i]]--;
Add_Edge(l[i], r[i], i);
Add_Edge(r[i], l[i], i);
}
for (int i = 1; i <= tot; i++) sum[i] += sum[i - 1];
for (int i = 1; i <= tot; i++)
if (sum[i] & 1) Add_Edge(i, i + 1, n + 1), Add_Edge(i + 1, i, n + 1);
for (int i = 1; i <= tot; i++)
if (!fl[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, tot, sum[200010];
int b[200010], l[200010], r[200010];
struct Node {
int to, nxt, id;
} Edge[200010 << 1];
int Head[200010], cnt_Edge = 1;
void Add_Edge(int u, int v, int id) {
Edge[++cnt_Edge] = (Node){v, Head[u], id};
Head[u] = cnt_Edge;
}
int ans[200010], vis[200010 << 1], fl[200010];
void dfs(int u) {
fl[u] = 1;
for (int i = Head[u]; i; i = Edge[i].nxt) {
int v = Edge[i].to, w = Edge[i].id;
if (vis[i]) continue;
if (u < v)
ans[w] = 0;
else
ans[w] = 1;
vis[i] = vis[i ^ 1] = 1;
dfs(v);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &l[i], &r[i]);
r[i]++;
b[++tot] = l[i];
b[++tot] = r[i];
}
sort(b + 1, b + tot + 1);
tot = unique(b + 1, b + tot + 1) - b - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(b + 1, b + tot + 1, l[i]) - b;
r[i] = lower_bound(b + 1, b + tot + 1, r[i]) - b;
sum[l[i]]++;
sum[r[i]]--;
Add_Edge(l[i], r[i], i);
Add_Edge(r[i], l[i], i);
}
for (int i = 1; i <= tot; i++) sum[i] += sum[i - 1];
for (int i = 1; i <= tot; i++)
if (sum[i] & 1) Add_Edge(i, i + 1, n + 1), Add_Edge(i + 1, i, n + 1);
for (int i = 1; i <= tot; i++)
if (!fl[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int T = 1e5 + 5;
pair<pair<int, int>, int> d[T];
map<int, int> M;
int f[T], id[T], col[T];
int l;
vector<long long> g[T];
void DFS(int v, int c) {
col[v] = c;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (col[to] == col[v]) {
cout << "-1" << endl;
exit(0);
}
if (!col[to]) DFS(to, 3 - c);
}
}
void add_edge(int a, int b) {
g[a].push_back(b);
g[b].push_back(a);
}
void add(int x) {
if (M[x]) return;
f[l++] = x;
}
void doit(int &x) { x = M[x]; }
set<pair<pair<int, int>, int> > s;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> d[i].first.first >> d[i].first.second;
d[i].second = i;
s.insert(d[i]);
}
for (int i = 0; s.size() >= 2; i++) {
pair<pair<int, int>, int> A = *s.begin();
s.erase(s.begin());
pair<pair<int, int>, int> B = *s.begin();
s.erase(s.begin());
if (A.first.second < B.first.first) {
s.insert(B);
continue;
}
if (A.first.second == B.first.second) {
add_edge(A.second, B.second);
} else if (A.first.second < B.first.second) {
add_edge(A.second, B.second);
B.first.first = A.first.second + 1;
s.insert(B);
} else {
add_edge(A.second, B.second);
A.first.first = B.first.second + 1;
s.insert(A);
}
}
for (int i = 0; i < n; i++)
if (!col[i]) DFS(i, 1);
for (int i = 0; i < n; i++) cout << col[i] - 1 << " ";
cout << endl;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int T = 1e5 + 5;
pair<pair<int, int>, int> d[T];
map<int, int> M;
int f[T], id[T], col[T];
int l;
vector<long long> g[T];
void DFS(int v, int c) {
col[v] = c;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (col[to] == col[v]) {
cout << "-1" << endl;
exit(0);
}
if (!col[to]) DFS(to, 3 - c);
}
}
void add_edge(int a, int b) {
g[a].push_back(b);
g[b].push_back(a);
}
void add(int x) {
if (M[x]) return;
f[l++] = x;
}
void doit(int &x) { x = M[x]; }
set<pair<pair<int, int>, int> > s;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> d[i].first.first >> d[i].first.second;
d[i].second = i;
s.insert(d[i]);
}
for (int i = 0; s.size() >= 2; i++) {
pair<pair<int, int>, int> A = *s.begin();
s.erase(s.begin());
pair<pair<int, int>, int> B = *s.begin();
s.erase(s.begin());
if (A.first.second < B.first.first) {
s.insert(B);
continue;
}
if (A.first.second == B.first.second) {
add_edge(A.second, B.second);
} else if (A.first.second < B.first.second) {
add_edge(A.second, B.second);
B.first.first = A.first.second + 1;
s.insert(B);
} else {
add_edge(A.second, B.second);
A.first.first = B.first.second + 1;
s.insert(A);
}
}
for (int i = 0; i < n; i++)
if (!col[i]) DFS(i, 1);
for (int i = 0; i < n; i++) cout << col[i] - 1 << " ";
cout << endl;
}
```
|
#include <bits/stdc++.h>
int n, tot = 1, cnt;
int head[200001], ver[400001], next[400001], id[400001];
int l[100001], r[100001], deg[200001];
int vp[200001], ve[400001];
int x[200001];
int ans[100001];
void add(int u, int v) {
ver[++tot] = v;
next[tot] = head[u];
head[u] = tot;
}
void dfs(int p) {
vp[p] = 1;
for (int i = head[p]; i; i = next[i]) {
if (ve[i]) continue;
ve[i] = ve[i ^ 1] = 1;
ans[id[i]] = (p < ver[i]);
dfs(ver[i]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &l[i], &r[i]);
r[i]++;
x[++cnt] = l[i];
x[++cnt] = r[i];
}
std::sort(x + 1, x + cnt + 1);
cnt = std::unique(x + 1, x + cnt + 1) - x - 1;
for (int i = 1; i <= n; i++) {
l[i] = std::lower_bound(x + 1, x + cnt + 1, l[i]) - x;
r[i] = std::lower_bound(x + 1, x + cnt + 1, r[i]) - x;
add(l[i], r[i]);
id[tot] = i;
add(r[i], l[i]);
id[tot] = i;
deg[l[i]]++;
deg[r[i]]++;
}
for (int i = 1, last = 0; i <= cnt; i++)
if (deg[i] & 1) {
if (last)
add(last, i), add(i, last), last = 0;
else
last = i;
}
for (int i = 1; i <= cnt; i++)
if (!vp[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
int n, tot = 1, cnt;
int head[200001], ver[400001], next[400001], id[400001];
int l[100001], r[100001], deg[200001];
int vp[200001], ve[400001];
int x[200001];
int ans[100001];
void add(int u, int v) {
ver[++tot] = v;
next[tot] = head[u];
head[u] = tot;
}
void dfs(int p) {
vp[p] = 1;
for (int i = head[p]; i; i = next[i]) {
if (ve[i]) continue;
ve[i] = ve[i ^ 1] = 1;
ans[id[i]] = (p < ver[i]);
dfs(ver[i]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &l[i], &r[i]);
r[i]++;
x[++cnt] = l[i];
x[++cnt] = r[i];
}
std::sort(x + 1, x + cnt + 1);
cnt = std::unique(x + 1, x + cnt + 1) - x - 1;
for (int i = 1; i <= n; i++) {
l[i] = std::lower_bound(x + 1, x + cnt + 1, l[i]) - x;
r[i] = std::lower_bound(x + 1, x + cnt + 1, r[i]) - x;
add(l[i], r[i]);
id[tot] = i;
add(r[i], l[i]);
id[tot] = i;
deg[l[i]]++;
deg[r[i]]++;
}
for (int i = 1, last = 0; i <= cnt; i++)
if (deg[i] & 1) {
if (last)
add(last, i), add(i, last), last = 0;
else
last = i;
}
for (int i = 1; i <= cnt; i++)
if (!vp[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
int Read() {
char c;
while (c = getchar(), (c != '-') && (c < '0' || c > '9'))
;
bool neg = (c == '-');
int ret = (neg ? 0 : c - 48);
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + c - 48;
return neg ? -ret : ret;
}
const int MAXN = 200005;
pair<int, int> a[MAXN];
int N, n, cnt, m, st[MAXN], b[MAXN], deg[MAXN], odd[MAXN], nxt[MAXN << 2],
lnk[MAXN << 2], mark[MAXN << 1], vis[MAXN];
void AddEdge(int x, int y) {
lnk[++cnt] = y, nxt[cnt] = st[x], st[x] = cnt, ++deg[x];
lnk[++cnt] = x, nxt[cnt] = st[y], st[y] = cnt, ++deg[y];
}
void init() {
scanf("%d", &N);
for (int i = 1; i <= N; i++)
a[i].first = Read(), a[i].second = Read() + 1, b[++n] = a[i].first,
b[++n] = a[i].second;
sort(b + 1, b + n + 1), n = unique(b + 1, b + n + 1) - b - 1;
for (int i = 1; i <= N; i++)
a[i].first = lower_bound(b + 1, b + n + 1, a[i].first) - b,
a[i].second = lower_bound(b + 1, b + n + 1, a[i].second) - b;
for (int i = 1; i <= N; i++) AddEdge(a[i].first, a[i].second);
for (int i = 1; i <= n; i++)
if (deg[i] & 1) odd[++m] = i;
sort(odd + 1, odd + m + 1);
for (int i = 1; i <= m; i++)
if (i & 1) AddEdge(odd[i], odd[i + 1]);
}
void DFS(int x) {
vis[x] = 1;
for (int i = st[x], y = lnk[i]; i; i = nxt[i], y = lnk[i])
if (!mark[(i + 1) >> 1]) {
mark[(i + 1) >> 1] = i;
DFS(y);
}
}
void work() {
for (int i = 1; i <= n; i++)
if (!vis[i]) DFS(i);
for (int i = 1; i <= N; i++) printf("%d%c", mark[i] & 1, i < N ? ' ' : '\n');
}
int main() {
init();
work();
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int Read() {
char c;
while (c = getchar(), (c != '-') && (c < '0' || c > '9'))
;
bool neg = (c == '-');
int ret = (neg ? 0 : c - 48);
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + c - 48;
return neg ? -ret : ret;
}
const int MAXN = 200005;
pair<int, int> a[MAXN];
int N, n, cnt, m, st[MAXN], b[MAXN], deg[MAXN], odd[MAXN], nxt[MAXN << 2],
lnk[MAXN << 2], mark[MAXN << 1], vis[MAXN];
void AddEdge(int x, int y) {
lnk[++cnt] = y, nxt[cnt] = st[x], st[x] = cnt, ++deg[x];
lnk[++cnt] = x, nxt[cnt] = st[y], st[y] = cnt, ++deg[y];
}
void init() {
scanf("%d", &N);
for (int i = 1; i <= N; i++)
a[i].first = Read(), a[i].second = Read() + 1, b[++n] = a[i].first,
b[++n] = a[i].second;
sort(b + 1, b + n + 1), n = unique(b + 1, b + n + 1) - b - 1;
for (int i = 1; i <= N; i++)
a[i].first = lower_bound(b + 1, b + n + 1, a[i].first) - b,
a[i].second = lower_bound(b + 1, b + n + 1, a[i].second) - b;
for (int i = 1; i <= N; i++) AddEdge(a[i].first, a[i].second);
for (int i = 1; i <= n; i++)
if (deg[i] & 1) odd[++m] = i;
sort(odd + 1, odd + m + 1);
for (int i = 1; i <= m; i++)
if (i & 1) AddEdge(odd[i], odd[i + 1]);
}
void DFS(int x) {
vis[x] = 1;
for (int i = st[x], y = lnk[i]; i; i = nxt[i], y = lnk[i])
if (!mark[(i + 1) >> 1]) {
mark[(i + 1) >> 1] = i;
DFS(y);
}
}
void work() {
for (int i = 1; i <= n; i++)
if (!vis[i]) DFS(i);
for (int i = 1; i <= N; i++) printf("%d%c", mark[i] & 1, i < N ? ' ' : '\n');
}
int main() {
init();
work();
return 0;
}
```
|
#include <bits/stdc++.h>
struct Range {
int left, right;
} seg[100000];
struct Edge {
int to, idx;
Edge() {}
Edge(int a, int b) : to(a), idx(b) {}
};
std::vector<Edge> g[2 * 100000];
int color[2 * 100000] = {};
void draw(int now) {
for (int i = 0; i < g[now].size(); i++) {
Edge e = g[now][i];
if (color[e.idx] != 0) continue;
color[e.idx] = (now < e.to) ? 1 : 2;
draw(e.to);
}
return;
}
int main() {
int n;
scanf("%d", &n);
std::set<int> bd;
for (int i = 0; i < n; i++) {
scanf("%d%d", &seg[i].left, &seg[i].right);
seg[i].right++;
bd.insert(seg[i].left);
bd.insert(seg[i].right);
}
int m = bd.size();
std::map<int, int> mp;
int idx = 0;
for (auto iter = bd.begin(); iter != bd.end(); iter++, idx++) {
mp[*iter] = idx;
}
for (int i = 0; i < n; i++) {
seg[i].left = mp[seg[i].left];
seg[i].right = mp[seg[i].right];
g[seg[i].left].push_back(Edge(seg[i].right, i));
g[seg[i].right].push_back(Edge(seg[i].left, i));
}
std::vector<int> odd_p;
for (int i = 0; i < m; i++)
if (g[i].size() % 2 == 1) odd_p.push_back(i);
for (int i = 0, ex_n = 0; i < odd_p.size(); i += 2, ex_n++) {
int now = odd_p[i], nxt = odd_p[i + 1];
g[now].push_back(Edge(nxt, n + ex_n));
g[nxt].push_back(Edge(now, n + ex_n));
}
for (int i = 0; i < m; i++) draw(i);
for (int i = 0; i < n; i++) printf("%d ", (color[i] == 1) ? 0 : 1);
putchar('\n');
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
struct Range {
int left, right;
} seg[100000];
struct Edge {
int to, idx;
Edge() {}
Edge(int a, int b) : to(a), idx(b) {}
};
std::vector<Edge> g[2 * 100000];
int color[2 * 100000] = {};
void draw(int now) {
for (int i = 0; i < g[now].size(); i++) {
Edge e = g[now][i];
if (color[e.idx] != 0) continue;
color[e.idx] = (now < e.to) ? 1 : 2;
draw(e.to);
}
return;
}
int main() {
int n;
scanf("%d", &n);
std::set<int> bd;
for (int i = 0; i < n; i++) {
scanf("%d%d", &seg[i].left, &seg[i].right);
seg[i].right++;
bd.insert(seg[i].left);
bd.insert(seg[i].right);
}
int m = bd.size();
std::map<int, int> mp;
int idx = 0;
for (auto iter = bd.begin(); iter != bd.end(); iter++, idx++) {
mp[*iter] = idx;
}
for (int i = 0; i < n; i++) {
seg[i].left = mp[seg[i].left];
seg[i].right = mp[seg[i].right];
g[seg[i].left].push_back(Edge(seg[i].right, i));
g[seg[i].right].push_back(Edge(seg[i].left, i));
}
std::vector<int> odd_p;
for (int i = 0; i < m; i++)
if (g[i].size() % 2 == 1) odd_p.push_back(i);
for (int i = 0, ex_n = 0; i < odd_p.size(); i += 2, ex_n++) {
int now = odd_p[i], nxt = odd_p[i + 1];
g[now].push_back(Edge(nxt, n + ex_n));
g[nxt].push_back(Edge(now, n + ex_n));
}
for (int i = 0; i < m; i++) draw(i);
for (int i = 0; i < n; i++) printf("%d ", (color[i] == 1) ? 0 : 1);
putchar('\n');
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int L = 200100;
int n;
vector<int> a[L];
vector<pair<int, int> > b;
int col[L];
void ins(int x, int y) {
a[x].push_back(y);
a[y].push_back(x);
}
void dfs(int x, int c) {
if (col[x] != -1) return;
col[x] = c;
for (vector<int>::iterator e = a[x].begin(); e != a[x].end(); ++e)
dfs(*e, 1 - c);
}
void init(void) {
cin >> n;
int i, l, r;
for (i = 1; i <= n; i++) {
scanf("%d%d", &l, &r);
l *= 2;
r = r * 2 + 1;
ins(i * 2 - 1, 2 * i);
b.push_back(make_pair(l, 2 * i - 1));
b.push_back(make_pair(r, 2 * i));
}
sort(b.begin(), b.end());
}
void work(void) {
int i;
for (i = 0; i < n; i++) ins(b[i * 2].second, b[i * 2 + 1].second);
memset(col, -1, sizeof(col));
for (i = 1; i <= 2 * n; i++)
if (col[i] == -1) dfs(i, 0);
for (i = 1; i <= n; i++) printf("%d ", col[i * 2 - 1]);
printf("\n");
}
int main(void) {
init();
work();
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int L = 200100;
int n;
vector<int> a[L];
vector<pair<int, int> > b;
int col[L];
void ins(int x, int y) {
a[x].push_back(y);
a[y].push_back(x);
}
void dfs(int x, int c) {
if (col[x] != -1) return;
col[x] = c;
for (vector<int>::iterator e = a[x].begin(); e != a[x].end(); ++e)
dfs(*e, 1 - c);
}
void init(void) {
cin >> n;
int i, l, r;
for (i = 1; i <= n; i++) {
scanf("%d%d", &l, &r);
l *= 2;
r = r * 2 + 1;
ins(i * 2 - 1, 2 * i);
b.push_back(make_pair(l, 2 * i - 1));
b.push_back(make_pair(r, 2 * i));
}
sort(b.begin(), b.end());
}
void work(void) {
int i;
for (i = 0; i < n; i++) ins(b[i * 2].second, b[i * 2 + 1].second);
memset(col, -1, sizeof(col));
for (i = 1; i <= 2 * n; i++)
if (col[i] == -1) dfs(i, 0);
for (i = 1; i <= n; i++) printf("%d ", col[i * 2 - 1]);
printf("\n");
}
int main(void) {
init();
work();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
const int E = 2e5 + 100;
int n;
int l[N], r[N];
vector<int> x;
vector<int> g[N];
vector<int> tour;
int to[E], mark[E], ecnt;
int from[E], c[E];
void add(int u, int v) {
from[ecnt] = u, to[ecnt] = v;
g[u].push_back(ecnt);
g[v].push_back(ecnt);
ecnt++;
}
void dfs(int v) {
while (((int)g[v].size())) {
int id = g[v].back();
g[v].pop_back();
int u = from[id] + to[id] - v;
if (!mark[id]) {
mark[id] = true;
dfs(u);
if (u < v)
c[id] = 1;
else
c[id] = 0;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
r[i]++;
x.push_back(l[i]);
x.push_back(r[i]);
}
sort(x.begin(), x.end());
x.resize(unique(x.begin(), x.end()) - x.begin());
for (int i = 0; i < n; ++i) {
l[i] = lower_bound(x.begin(), x.end(), l[i]) - x.begin();
r[i] = lower_bound(x.begin(), x.end(), r[i]) - x.begin();
add(l[i], r[i]);
}
int lst = -1;
for (int i = 0; i < 2 * n; ++i) {
if (((int)g[i].size()) & 1) {
if (lst == -1)
lst = i;
else {
add(lst, i);
lst = -1;
}
}
}
for (int i = 0; i < 2 * n; i++)
if (!g[i].empty()) dfs(i);
for (int i = 0; i < n; i++) cout << c[i] << " ";
cout << endl;
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
const int E = 2e5 + 100;
int n;
int l[N], r[N];
vector<int> x;
vector<int> g[N];
vector<int> tour;
int to[E], mark[E], ecnt;
int from[E], c[E];
void add(int u, int v) {
from[ecnt] = u, to[ecnt] = v;
g[u].push_back(ecnt);
g[v].push_back(ecnt);
ecnt++;
}
void dfs(int v) {
while (((int)g[v].size())) {
int id = g[v].back();
g[v].pop_back();
int u = from[id] + to[id] - v;
if (!mark[id]) {
mark[id] = true;
dfs(u);
if (u < v)
c[id] = 1;
else
c[id] = 0;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
r[i]++;
x.push_back(l[i]);
x.push_back(r[i]);
}
sort(x.begin(), x.end());
x.resize(unique(x.begin(), x.end()) - x.begin());
for (int i = 0; i < n; ++i) {
l[i] = lower_bound(x.begin(), x.end(), l[i]) - x.begin();
r[i] = lower_bound(x.begin(), x.end(), r[i]) - x.begin();
add(l[i], r[i]);
}
int lst = -1;
for (int i = 0; i < 2 * n; ++i) {
if (((int)g[i].size()) & 1) {
if (lst == -1)
lst = i;
else {
add(lst, i);
lst = -1;
}
}
}
for (int i = 0; i < 2 * n; i++)
if (!g[i].empty()) dfs(i);
for (int i = 0; i < n; i++) cout << c[i] << " ";
cout << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> ed[401000], odd, cod[401000];
pair<int, int> e[401000];
int pathlen, x, y, n, m, tot, st[401000], pathedge[401000], b[401000],
path[401000], pr[401000];
bool don[401000], vis[402000];
void dfs(int x) {
don[x] = true;
for (int i = 0; i < (int)ed[x].size(); i++) {
if (vis[cod[x][i]]) continue;
vis[cod[x][i]] = true;
dfs(ed[x][i]);
pathlen++;
path[pathlen] = x;
pathedge[pathlen] = cod[x][i];
}
}
void EulerCircuit() {
for (int k = 1; k <= tot; k++) {
if (!don[k]) {
pathlen = 0;
dfs(k);
for (int i = pathlen; i > 1; i--) {
if (path[i] > path[i - 1])
pr[pathedge[i]] = 0;
else
pr[pathedge[i]] = 1;
}
if (path[1] > path[pathlen])
pr[pathedge[1]] = 0;
else
pr[pathedge[1]] = 1;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &x, &y);
y++;
e[i] = make_pair(x, y);
b[++tot] = x;
b[++tot] = y;
}
sort(&b[1], &b[tot + 1]);
tot = unique(&b[1], &b[tot + 1]) - &b[1];
for (int i = 1; i <= n; i++) {
e[i].first = lower_bound(&b[1], &b[tot + 1], e[i].first) - &b[0];
e[i].second = lower_bound(&b[1], &b[tot + 1], e[i].second) - &b[0];
}
for (int i = 1; i <= n; i++) {
int x = e[i].first, y = e[i].second;
ed[x].push_back(y);
ed[y].push_back(x);
cod[x].push_back(i);
cod[y].push_back(i);
}
for (int i = 1; i <= tot; i++) {
if (ed[i].size() % 2) odd.push_back(i);
}
int ccc = n;
for (int i = 1; i < odd.size(); i += 2) {
ed[odd[i - 1]].push_back(odd[i]);
ed[odd[i]].push_back(odd[i - 1]);
cod[odd[i - 1]].push_back(++ccc);
cod[odd[i]].push_back(ccc);
}
EulerCircuit();
for (int i = 1; i <= n; i++) printf("%d ", pr[i]);
printf("\n");
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> ed[401000], odd, cod[401000];
pair<int, int> e[401000];
int pathlen, x, y, n, m, tot, st[401000], pathedge[401000], b[401000],
path[401000], pr[401000];
bool don[401000], vis[402000];
void dfs(int x) {
don[x] = true;
for (int i = 0; i < (int)ed[x].size(); i++) {
if (vis[cod[x][i]]) continue;
vis[cod[x][i]] = true;
dfs(ed[x][i]);
pathlen++;
path[pathlen] = x;
pathedge[pathlen] = cod[x][i];
}
}
void EulerCircuit() {
for (int k = 1; k <= tot; k++) {
if (!don[k]) {
pathlen = 0;
dfs(k);
for (int i = pathlen; i > 1; i--) {
if (path[i] > path[i - 1])
pr[pathedge[i]] = 0;
else
pr[pathedge[i]] = 1;
}
if (path[1] > path[pathlen])
pr[pathedge[1]] = 0;
else
pr[pathedge[1]] = 1;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &x, &y);
y++;
e[i] = make_pair(x, y);
b[++tot] = x;
b[++tot] = y;
}
sort(&b[1], &b[tot + 1]);
tot = unique(&b[1], &b[tot + 1]) - &b[1];
for (int i = 1; i <= n; i++) {
e[i].first = lower_bound(&b[1], &b[tot + 1], e[i].first) - &b[0];
e[i].second = lower_bound(&b[1], &b[tot + 1], e[i].second) - &b[0];
}
for (int i = 1; i <= n; i++) {
int x = e[i].first, y = e[i].second;
ed[x].push_back(y);
ed[y].push_back(x);
cod[x].push_back(i);
cod[y].push_back(i);
}
for (int i = 1; i <= tot; i++) {
if (ed[i].size() % 2) odd.push_back(i);
}
int ccc = n;
for (int i = 1; i < odd.size(); i += 2) {
ed[odd[i - 1]].push_back(odd[i]);
ed[odd[i]].push_back(odd[i - 1]);
cod[odd[i - 1]].push_back(++ccc);
cod[odd[i]].push_back(ccc);
}
EulerCircuit();
for (int i = 1; i <= n; i++) printf("%d ", pr[i]);
printf("\n");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 10;
constexpr int LG = 20;
constexpr int MOD = 1e9 + 7;
constexpr int MOD2 = 1e9 + 9;
int n, l, r, cl[N];
set<pair<int, pair<int, int>>> st;
void solve() {
if ((int)st.size() <= 1) return;
pair<int, pair<int, int>> aval = *st.begin();
st.erase(st.begin());
pair<int, pair<int, int>> dovom = *st.begin();
st.erase(st.begin());
if (aval.second.first == dovom.second.first) {
cl[aval.second.second] = 1;
solve();
} else if (aval.second.first < dovom.second.first) {
st.insert(
{aval.second.first + 1, {dovom.second.first, dovom.second.second}});
solve();
cl[aval.second.second] = 1 - cl[dovom.second.second];
} else {
st.insert(
{dovom.second.first + 1, {aval.second.first, aval.second.second}});
solve();
cl[dovom.second.second] = 1 - cl[aval.second.second];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l >> r;
st.insert({l, {r, i}});
}
solve();
for (int i = 0; i < n; i++) cout << cl[i] << ' ';
cout << '\n';
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 10;
constexpr int LG = 20;
constexpr int MOD = 1e9 + 7;
constexpr int MOD2 = 1e9 + 9;
int n, l, r, cl[N];
set<pair<int, pair<int, int>>> st;
void solve() {
if ((int)st.size() <= 1) return;
pair<int, pair<int, int>> aval = *st.begin();
st.erase(st.begin());
pair<int, pair<int, int>> dovom = *st.begin();
st.erase(st.begin());
if (aval.second.first == dovom.second.first) {
cl[aval.second.second] = 1;
solve();
} else if (aval.second.first < dovom.second.first) {
st.insert(
{aval.second.first + 1, {dovom.second.first, dovom.second.second}});
solve();
cl[aval.second.second] = 1 - cl[dovom.second.second];
} else {
st.insert(
{dovom.second.first + 1, {aval.second.first, aval.second.second}});
solve();
cl[dovom.second.second] = 1 - cl[aval.second.second];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l >> r;
st.insert({l, {r, i}});
}
solve();
for (int i = 0; i < n; i++) cout << cl[i] << ' ';
cout << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double dInf = 1E90;
const long long lInf = (long long)1E16;
const int Inf = 0x23333333;
const int N = 300005;
struct edge {
int x, y, used, num, next;
} e[N << 1];
int a[N];
int cnt[N], ret[N];
bool vis[N];
int n, m, T = 2;
pair<int, int> A[N];
void mke(int x, int y, int num) {
e[T].y = y, e[T].next = a[x], e[T].num = num, a[x] = T++;
e[T].y = x, e[T].next = a[y], e[T].num = num, a[y] = T++;
}
void preprocessing() {
static pair<long long, int> que[N];
int tl = 0;
scanf("%d", &n);
for (int i = 0, LIM = n; i < LIM; ++i) {
scanf("%d%d", &A[i].first, &A[i].second);
if (A[i].first > A[i].second) swap(A[i].first, A[i].second);
que[tl++] = make_pair(A[i].first, i + 1),
que[tl++] = make_pair(++A[i].second, -i - 1);
}
sort(que, que + tl);
for (int i = 0, LIM = tl; i < LIM; ++i) {
if (i && que[i].first != que[i - 1].first) ++m;
if (que[i].second < 0)
A[-que[i].second - 1].second = m;
else
A[que[i].second - 1].first = m;
}
for (int i = 0, LIM = n; i < LIM; ++i) {
cnt[A[i].first] ^= 1, cnt[A[i].second] ^= 1;
mke(A[i].first, A[i].second, i);
}
int pre = -1;
for (int i = 0, LIM = m + 1; i < LIM; ++i) {
if (!cnt[i]) continue;
if (pre >= 0)
mke(pre, i, -1), pre = -1;
else
pre = i;
}
}
void euler(int x, int fr) {
vis[x] = 1;
for (int j = a[x]; j; j = e[j].next)
if (j != (fr ^ 1) && !e[j].used)
e[j].used = 1, e[j ^ 1].used = 2, euler(e[j].y, j);
}
int main() {
preprocessing();
for (int i = 0, LIM = m; i < LIM; ++i)
if (!vis[i]) euler(i, N + 5);
for (int i = 2, LIM = T; i < LIM; ++i) {
if (e[i].num < 0) continue;
ret[e[i].num] = (e[i].used == 1) ^ (i & 1);
}
for (int i = 0, LIM = n; i < LIM; ++i) printf("%d ", ret[i]);
printf("\n");
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double dInf = 1E90;
const long long lInf = (long long)1E16;
const int Inf = 0x23333333;
const int N = 300005;
struct edge {
int x, y, used, num, next;
} e[N << 1];
int a[N];
int cnt[N], ret[N];
bool vis[N];
int n, m, T = 2;
pair<int, int> A[N];
void mke(int x, int y, int num) {
e[T].y = y, e[T].next = a[x], e[T].num = num, a[x] = T++;
e[T].y = x, e[T].next = a[y], e[T].num = num, a[y] = T++;
}
void preprocessing() {
static pair<long long, int> que[N];
int tl = 0;
scanf("%d", &n);
for (int i = 0, LIM = n; i < LIM; ++i) {
scanf("%d%d", &A[i].first, &A[i].second);
if (A[i].first > A[i].second) swap(A[i].first, A[i].second);
que[tl++] = make_pair(A[i].first, i + 1),
que[tl++] = make_pair(++A[i].second, -i - 1);
}
sort(que, que + tl);
for (int i = 0, LIM = tl; i < LIM; ++i) {
if (i && que[i].first != que[i - 1].first) ++m;
if (que[i].second < 0)
A[-que[i].second - 1].second = m;
else
A[que[i].second - 1].first = m;
}
for (int i = 0, LIM = n; i < LIM; ++i) {
cnt[A[i].first] ^= 1, cnt[A[i].second] ^= 1;
mke(A[i].first, A[i].second, i);
}
int pre = -1;
for (int i = 0, LIM = m + 1; i < LIM; ++i) {
if (!cnt[i]) continue;
if (pre >= 0)
mke(pre, i, -1), pre = -1;
else
pre = i;
}
}
void euler(int x, int fr) {
vis[x] = 1;
for (int j = a[x]; j; j = e[j].next)
if (j != (fr ^ 1) && !e[j].used)
e[j].used = 1, e[j ^ 1].used = 2, euler(e[j].y, j);
}
int main() {
preprocessing();
for (int i = 0, LIM = m; i < LIM; ++i)
if (!vis[i]) euler(i, N + 5);
for (int i = 2, LIM = T; i < LIM; ++i) {
if (e[i].num < 0) continue;
ret[e[i].num] = (e[i].used == 1) ^ (i & 1);
}
for (int i = 0, LIM = n; i < LIM; ++i) printf("%d ", ret[i]);
printf("\n");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, c, B[N], T[N];
pair<int, int> A[N];
map<int, int> M;
vector<int> Odd;
vector<pair<int, int> > V[N];
void Add(int v, int u, int i) {
V[v].push_back({u, i});
V[u].push_back({v, i});
}
void DFS(int v) {
while (V[v].size()) {
auto X = V[v].back();
V[v].pop_back();
if (T[X.second]) continue;
T[X.second] = 1 + (v > X.first);
DFS(X.first);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d", &A[i].first, &A[i].second),
B[i * 2] = A[i].first, B[i * 2 + 1] = A[i].second + 1;
sort(B, B + 2 * n);
for (int i = 0; i < 2 * n; i++)
if (!M.count(B[i])) M[B[i]] = c++;
for (int i = 0; i < n; i++) Add(M[A[i].first], M[A[i].second + 1], i);
for (int i = 0; i < c; i++)
if (V[i].size() & 1) Odd.push_back(i);
for (int i = 0; i < Odd.size(); i += 2) Add(Odd[i], Odd[i + 1], n + i / 2);
for (int i = 0; i < c; i++) DFS(i);
for (int i = 0; i < n; i++, putchar(' ')) putchar(T[i] == 2 ? '1' : '0');
return (0);
}
|
### Prompt
Create a solution in CPP for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, c, B[N], T[N];
pair<int, int> A[N];
map<int, int> M;
vector<int> Odd;
vector<pair<int, int> > V[N];
void Add(int v, int u, int i) {
V[v].push_back({u, i});
V[u].push_back({v, i});
}
void DFS(int v) {
while (V[v].size()) {
auto X = V[v].back();
V[v].pop_back();
if (T[X.second]) continue;
T[X.second] = 1 + (v > X.first);
DFS(X.first);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d", &A[i].first, &A[i].second),
B[i * 2] = A[i].first, B[i * 2 + 1] = A[i].second + 1;
sort(B, B + 2 * n);
for (int i = 0; i < 2 * n; i++)
if (!M.count(B[i])) M[B[i]] = c++;
for (int i = 0; i < n; i++) Add(M[A[i].first], M[A[i].second + 1], i);
for (int i = 0; i < c; i++)
if (V[i].size() & 1) Odd.push_back(i);
for (int i = 0; i < Odd.size(); i += 2) Add(Odd[i], Odd[i + 1], n + i / 2);
for (int i = 0; i < c; i++) DFS(i);
for (int i = 0; i < n; i++, putchar(' ')) putchar(T[i] == 2 ? '1' : '0');
return (0);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> adj[N];
vector<int> cur;
int l[N], r[N];
int d[N];
vector<int> fard;
bool mark[N];
vector<int> tour;
unordered_map<int, int> nist[N];
unordered_map<int, bool> ans[N];
void dfs(int x) {
mark[x] = 1;
for (int p : adj[x]) {
if (nist[x][p]) {
nist[x][p] = nist[p][x] = nist[x][p] - 1;
dfs(p);
}
}
tour.push_back(x);
}
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
r[i]++;
cur.push_back(l[i]);
cur.push_back(r[i]);
}
sort(cur.begin(), cur.end());
cur.resize(unique(cur.begin(), cur.end()) - cur.begin());
for (int i = 1; i <= n; i++) {
int m1 = lower_bound(cur.begin(), cur.end(), l[i]) - cur.begin();
int m2 = lower_bound(cur.begin(), cur.end(), r[i]) - cur.begin();
adj[m1].push_back(m2);
adj[m2].push_back(m1);
d[m1]++;
d[m2]++;
}
for (int i = 0; i < cur.size(); i++)
if (d[i] % 2 == 1) fard.push_back(i);
for (int i = 0; i < fard.size(); i += 2) {
adj[fard[i]].push_back(fard[i + 1]);
adj[fard[i + 1]].push_back(fard[i]);
}
for (int i = 0; i < cur.size(); i++)
for (int p : adj[i]) nist[i][p]++;
for (int i = 0; i < cur.size(); i++) {
if (!mark[i]) {
tour.clear();
dfs(i);
tour.push_back(tour[0]);
for (int i = 0; i < tour.size() - 1; i++) {
if (tour[i] < tour[i + 1])
ans[tour[i]][tour[i + 1]] = ans[tour[i + 1]][tour[i]] = 1;
else
ans[tour[i]][tour[i + 1]] = ans[tour[i + 1]][tour[i]] = 0;
}
}
}
for (int i = 1; i <= n; i++) {
int m1 = lower_bound(cur.begin(), cur.end(), l[i]) - cur.begin();
int m2 = lower_bound(cur.begin(), cur.end(), r[i]) - cur.begin();
cout << ans[m1][m2] << "\n";
}
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> adj[N];
vector<int> cur;
int l[N], r[N];
int d[N];
vector<int> fard;
bool mark[N];
vector<int> tour;
unordered_map<int, int> nist[N];
unordered_map<int, bool> ans[N];
void dfs(int x) {
mark[x] = 1;
for (int p : adj[x]) {
if (nist[x][p]) {
nist[x][p] = nist[p][x] = nist[x][p] - 1;
dfs(p);
}
}
tour.push_back(x);
}
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
r[i]++;
cur.push_back(l[i]);
cur.push_back(r[i]);
}
sort(cur.begin(), cur.end());
cur.resize(unique(cur.begin(), cur.end()) - cur.begin());
for (int i = 1; i <= n; i++) {
int m1 = lower_bound(cur.begin(), cur.end(), l[i]) - cur.begin();
int m2 = lower_bound(cur.begin(), cur.end(), r[i]) - cur.begin();
adj[m1].push_back(m2);
adj[m2].push_back(m1);
d[m1]++;
d[m2]++;
}
for (int i = 0; i < cur.size(); i++)
if (d[i] % 2 == 1) fard.push_back(i);
for (int i = 0; i < fard.size(); i += 2) {
adj[fard[i]].push_back(fard[i + 1]);
adj[fard[i + 1]].push_back(fard[i]);
}
for (int i = 0; i < cur.size(); i++)
for (int p : adj[i]) nist[i][p]++;
for (int i = 0; i < cur.size(); i++) {
if (!mark[i]) {
tour.clear();
dfs(i);
tour.push_back(tour[0]);
for (int i = 0; i < tour.size() - 1; i++) {
if (tour[i] < tour[i + 1])
ans[tour[i]][tour[i + 1]] = ans[tour[i + 1]][tour[i]] = 1;
else
ans[tour[i]][tour[i + 1]] = ans[tour[i + 1]][tour[i]] = 0;
}
}
}
for (int i = 1; i <= n; i++) {
int m1 = lower_bound(cur.begin(), cur.end(), l[i]) - cur.begin();
int m2 = lower_bound(cur.begin(), cur.end(), r[i]) - cur.begin();
cout << ans[m1][m2] << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int p[maxn], w[maxn];
vector<pair<pair<int, int>, int>> a;
int find(int x) {
if (p[x] == x)
return x;
else {
int ret = find(p[x]);
w[x] ^= w[p[x]];
return p[x] = ret;
}
}
int main() {
int l, r, n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l >> r;
a.push_back(make_pair(make_pair(l, 0), i));
a.push_back(make_pair(make_pair(r, 1), i));
}
sort(a.begin(), a.end());
for (int i = 0; i < n; i++) p[i] = i;
for (int i = 0; i < a.size(); i += 2)
if (a[i].second != a[i + 1].second) {
int ret = a[i].first.second == a[i + 1].first.second;
int u = a[i].second, v = a[i + 1].second;
if (find(u) == find(v)) {
if (w[u] ^ w[v] ^ ret) {
cout << -1 << endl;
return 0;
}
} else {
w[p[v]] = ret ^ w[v];
p[p[v]] = u;
}
}
for (int i = 0; i < n; i++) {
find(i);
cout << w[i] << ' ';
}
cout << endl;
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int p[maxn], w[maxn];
vector<pair<pair<int, int>, int>> a;
int find(int x) {
if (p[x] == x)
return x;
else {
int ret = find(p[x]);
w[x] ^= w[p[x]];
return p[x] = ret;
}
}
int main() {
int l, r, n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l >> r;
a.push_back(make_pair(make_pair(l, 0), i));
a.push_back(make_pair(make_pair(r, 1), i));
}
sort(a.begin(), a.end());
for (int i = 0; i < n; i++) p[i] = i;
for (int i = 0; i < a.size(); i += 2)
if (a[i].second != a[i + 1].second) {
int ret = a[i].first.second == a[i + 1].first.second;
int u = a[i].second, v = a[i + 1].second;
if (find(u) == find(v)) {
if (w[u] ^ w[v] ^ ret) {
cout << -1 << endl;
return 0;
}
} else {
w[p[v]] = ret ^ w[v];
p[p[v]] = u;
}
}
for (int i = 0; i < n; i++) {
find(i);
cout << w[i] << ' ';
}
cout << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
bool t = false;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') t = true, ch = getchar();
while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
return t ? -x : x;
}
int n, L[400100], R[400100];
int S[400100], top;
struct Line {
int v, next;
} e[400100];
int h[400100], cnt = 2, dg[400100];
inline void Add(int u, int v) {
e[cnt] = (Line){v, h[u]};
h[u] = cnt++;
dg[v] += 1;
}
int vis[400100];
bool book[400100];
void dfs(int u) {
for (int &i = h[u]; i; i = e[i].next) {
int v = e[i].v, j = i;
if (book[i >> 1]) continue;
book[i >> 1] = true;
dfs(v);
vis[j >> 1] = u < v;
}
}
int main() {
n = read();
for (int i = 1; i <= n; ++i) L[i] = read(), R[i] = read();
for (int i = 1; i <= n; ++i) S[++top] = L[i], S[++top] = L[i] - 1;
for (int i = 1; i <= n; ++i) S[++top] = R[i], S[++top] = R[i] + 1;
sort(&S[1], &S[top + 1]);
top = unique(&S[1], &S[top + 1]) - S - 1;
for (int i = 1; i <= n; ++i) L[i] = lower_bound(&S[1], &S[top + 1], L[i]) - S;
for (int i = 1; i <= n; ++i) R[i] = lower_bound(&S[1], &S[top + 1], R[i]) - S;
for (int i = 1; i <= n; ++i) Add(L[i], R[i] + 1), Add(R[i] + 1, L[i]);
for (int i = 1, lst = 0; i <= top; ++i)
if (dg[i] & 1) lst ? Add(i, lst), Add(lst, i), lst = 0 : lst = i;
for (int i = 2; i < cnt; i += 2)
if (!book[i >> 1]) dfs(e[i].v);
for (int i = 1; i <= n; ++i) printf("%d ", vis[i]);
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
bool t = false;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') t = true, ch = getchar();
while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
return t ? -x : x;
}
int n, L[400100], R[400100];
int S[400100], top;
struct Line {
int v, next;
} e[400100];
int h[400100], cnt = 2, dg[400100];
inline void Add(int u, int v) {
e[cnt] = (Line){v, h[u]};
h[u] = cnt++;
dg[v] += 1;
}
int vis[400100];
bool book[400100];
void dfs(int u) {
for (int &i = h[u]; i; i = e[i].next) {
int v = e[i].v, j = i;
if (book[i >> 1]) continue;
book[i >> 1] = true;
dfs(v);
vis[j >> 1] = u < v;
}
}
int main() {
n = read();
for (int i = 1; i <= n; ++i) L[i] = read(), R[i] = read();
for (int i = 1; i <= n; ++i) S[++top] = L[i], S[++top] = L[i] - 1;
for (int i = 1; i <= n; ++i) S[++top] = R[i], S[++top] = R[i] + 1;
sort(&S[1], &S[top + 1]);
top = unique(&S[1], &S[top + 1]) - S - 1;
for (int i = 1; i <= n; ++i) L[i] = lower_bound(&S[1], &S[top + 1], L[i]) - S;
for (int i = 1; i <= n; ++i) R[i] = lower_bound(&S[1], &S[top + 1], R[i]) - S;
for (int i = 1; i <= n; ++i) Add(L[i], R[i] + 1), Add(R[i] + 1, L[i]);
for (int i = 1, lst = 0; i <= top; ++i)
if (dg[i] & 1) lst ? Add(i, lst), Add(lst, i), lst = 0 : lst = i;
for (int i = 2; i < cnt; i += 2)
if (!book[i >> 1]) dfs(e[i].v);
for (int i = 1; i <= n; ++i) printf("%d ", vis[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
vector<int> g[N];
int col[N], u[N], v[N], mark[N], vis[N];
void dfs(int x, int y) {
if (y != -1) vis[y] = true;
mark[x] = true;
for (auto i : g[x])
if (!vis[i]) dfs(u[i] ^ v[i] ^ x, i);
if (y != -1) {
int o = u[y] ^ v[y] ^ x;
if (o < x)
col[y] = 1;
else
col[y] = 0;
}
}
int32_t main() {
int n;
cin >> n;
vector<int> comp;
for (int i = 0; i < n; i++) {
cin >> u[i] >> v[i];
v[i]++;
comp.push_back(u[i]);
comp.push_back(v[i]);
}
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
for (int i = 0; i < n; i++) {
u[i] = lower_bound(comp.begin(), comp.end(), u[i]) - comp.begin();
v[i] = lower_bound(comp.begin(), comp.end(), v[i]) - comp.begin();
g[v[i]].push_back(i);
g[u[i]].push_back(i);
}
int last = -1;
int t = n;
for (int i = 0; i < ((int)comp.size()); i++)
if (((int)g[i].size()) & 1) {
if (last == -1)
u[t] = i, last = i;
else {
v[t] = i;
g[last].push_back(t);
g[i].push_back(t);
last = -1;
t++;
}
}
for (int i = 0; i < ((int)comp.size()); i++)
if (!mark[i]) dfs(i, -1);
for (int i = 0; i < n; i++) cout << col[i] << ' ';
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
vector<int> g[N];
int col[N], u[N], v[N], mark[N], vis[N];
void dfs(int x, int y) {
if (y != -1) vis[y] = true;
mark[x] = true;
for (auto i : g[x])
if (!vis[i]) dfs(u[i] ^ v[i] ^ x, i);
if (y != -1) {
int o = u[y] ^ v[y] ^ x;
if (o < x)
col[y] = 1;
else
col[y] = 0;
}
}
int32_t main() {
int n;
cin >> n;
vector<int> comp;
for (int i = 0; i < n; i++) {
cin >> u[i] >> v[i];
v[i]++;
comp.push_back(u[i]);
comp.push_back(v[i]);
}
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
for (int i = 0; i < n; i++) {
u[i] = lower_bound(comp.begin(), comp.end(), u[i]) - comp.begin();
v[i] = lower_bound(comp.begin(), comp.end(), v[i]) - comp.begin();
g[v[i]].push_back(i);
g[u[i]].push_back(i);
}
int last = -1;
int t = n;
for (int i = 0; i < ((int)comp.size()); i++)
if (((int)g[i].size()) & 1) {
if (last == -1)
u[t] = i, last = i;
else {
v[t] = i;
g[last].push_back(t);
g[i].push_back(t);
last = -1;
t++;
}
}
for (int i = 0; i < ((int)comp.size()); i++)
if (!mark[i]) dfs(i, -1);
for (int i = 0; i < n; i++) cout << col[i] << ' ';
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e60;
template <typename T>
inline void scf(T &x) {
bool f = 0;
x = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') {
f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (f) x = -x;
return;
}
template <typename T1, typename T2>
void scf(T1 &x, T2 &y) {
scf(x);
return scf(y);
}
template <typename T1, typename T2, typename T3>
void scf(T1 &x, T2 &y, T3 &z) {
scf(x);
scf(y);
return scf(z);
}
template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
scf(x);
scf(y);
scf(z);
return scf(w);
}
inline char mygetchar() {
char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
template <typename T>
void chkmax(T &x, const T &y) {
if (y > x) x = y;
return;
}
template <typename T>
void chkmin(T &x, const T &y) {
if (y < x) x = y;
return;
}
const int N = 2e5 + 100;
int n;
int ans[N], id[N], x[N];
vector<int> g[N];
void TZL() {
scf(n);
for (int i = (1); i <= (n); ++i) {
scf(x[i], x[i + n]);
x[i + n]++;
g[i].push_back(i + n);
g[i + n].push_back(i);
id[i] = i;
id[i + n] = i + n;
}
sort(id + 1, id + n + n + 1, [&](int i, int j) { return x[i] < x[j]; });
for (int i = 1; i <= n + n; i += 2) {
g[id[i]].push_back(id[i + 1]);
g[id[i + 1]].push_back(id[i]);
}
return;
}
void dfs(int u, int c = 1) {
ans[u] = c;
for (int v : g[u])
if (!ans[v]) dfs(v, 3 - c);
return;
}
void RANK1() {
for (int i = (1); i <= (n); ++i) {
if (!ans[i]) dfs(i);
printf("%d ", ans[i] - 1);
}
return;
}
int main() {
TZL();
RANK1();
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e60;
template <typename T>
inline void scf(T &x) {
bool f = 0;
x = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') {
f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (f) x = -x;
return;
}
template <typename T1, typename T2>
void scf(T1 &x, T2 &y) {
scf(x);
return scf(y);
}
template <typename T1, typename T2, typename T3>
void scf(T1 &x, T2 &y, T3 &z) {
scf(x);
scf(y);
return scf(z);
}
template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
scf(x);
scf(y);
scf(z);
return scf(w);
}
inline char mygetchar() {
char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
template <typename T>
void chkmax(T &x, const T &y) {
if (y > x) x = y;
return;
}
template <typename T>
void chkmin(T &x, const T &y) {
if (y < x) x = y;
return;
}
const int N = 2e5 + 100;
int n;
int ans[N], id[N], x[N];
vector<int> g[N];
void TZL() {
scf(n);
for (int i = (1); i <= (n); ++i) {
scf(x[i], x[i + n]);
x[i + n]++;
g[i].push_back(i + n);
g[i + n].push_back(i);
id[i] = i;
id[i + n] = i + n;
}
sort(id + 1, id + n + n + 1, [&](int i, int j) { return x[i] < x[j]; });
for (int i = 1; i <= n + n; i += 2) {
g[id[i]].push_back(id[i + 1]);
g[id[i + 1]].push_back(id[i]);
}
return;
}
void dfs(int u, int c = 1) {
ans[u] = c;
for (int v : g[u])
if (!ans[v]) dfs(v, 3 - c);
return;
}
void RANK1() {
for (int i = (1); i <= (n); ++i) {
if (!ans[i]) dfs(i);
printf("%d ", ans[i] - 1);
}
return;
}
int main() {
TZL();
RANK1();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
int id[N + N];
int a[N + N];
int where[N + N], color[N + N];
vector<int> E[N + N];
bool cmp(int i, int j) { return a[i] < a[j]; }
void dfs(int u, int c) {
if (color[u] == -1) {
color[u] = c;
} else {
return;
}
for (int v : E[u]) {
dfs(v, c ^ 1);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < (int)(n); i++) {
scanf("%d%d", &a[i * 2], &a[i * 2 + 1]);
a[i * 2 + 1]++;
id[i * 2] = i * 2;
id[i * 2 + 1] = i * 2 + 1;
}
sort(id, id + n + n, cmp);
for (int i = 0; i < (int)(n + n); i++) {
where[id[i]] = i;
}
for (int i = 0; i < (int)(n); i++) {
E[i * 2].push_back(i * 2 + 1);
E[i * 2 + 1].push_back(i * 2);
E[where[i * 2]].push_back(where[i * 2 + 1]);
E[where[i * 2 + 1]].push_back(where[i * 2]);
}
memset(color, 0xff, sizeof(color));
for (int i = 0; i < (int)(n + n); i++) {
if (color[i] == -1) {
dfs(i, 0);
}
}
for (int i = 0; i < (int)(n); i++) {
printf("%d ", color[where[i * 2]]);
}
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
int id[N + N];
int a[N + N];
int where[N + N], color[N + N];
vector<int> E[N + N];
bool cmp(int i, int j) { return a[i] < a[j]; }
void dfs(int u, int c) {
if (color[u] == -1) {
color[u] = c;
} else {
return;
}
for (int v : E[u]) {
dfs(v, c ^ 1);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < (int)(n); i++) {
scanf("%d%d", &a[i * 2], &a[i * 2 + 1]);
a[i * 2 + 1]++;
id[i * 2] = i * 2;
id[i * 2 + 1] = i * 2 + 1;
}
sort(id, id + n + n, cmp);
for (int i = 0; i < (int)(n + n); i++) {
where[id[i]] = i;
}
for (int i = 0; i < (int)(n); i++) {
E[i * 2].push_back(i * 2 + 1);
E[i * 2 + 1].push_back(i * 2);
E[where[i * 2]].push_back(where[i * 2 + 1]);
E[where[i * 2 + 1]].push_back(where[i * 2]);
}
memset(color, 0xff, sizeof(color));
for (int i = 0; i < (int)(n + n); i++) {
if (color[i] == -1) {
dfs(i, 0);
}
}
for (int i = 0; i < (int)(n); i++) {
printf("%d ", color[where[i * 2]]);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, l[100015], r[100015], o[100015 << 1], tot, cnt, ans[100015],
ind[100015 << 1], head[100015 << 1];
struct edge {
int to, next, w;
edge() {}
edge(int u, int v, int x) {
to = u;
next = v;
w = x;
}
} e[100015 << 2];
bool vis[100015 << 2], gkp[100015 << 2];
void add(int u, int v, int w) {
e[++cnt] = edge(v, head[u], w);
head[u] = cnt;
}
void dfs(int u) {
gkp[u] = 1;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if (vis[i]) continue;
vis[i] = vis[i ^ 1] = 1;
ans[e[i].w] = (u < v);
dfs(v);
}
}
int main() {
memset(head, -1, sizeof head);
scanf("%d", &n);
cnt = -1;
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), ++r[i], o[++tot] = l[i], o[++tot] = r[i];
sort(o + 1, o + tot + 1);
int m = unique(o + 1, o + tot + 1) - o - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(o + 1, o + m + 1, l[i]) - o;
r[i] = lower_bound(o + 1, o + m + 1, r[i]) - o;
add(l[i], r[i], i);
add(r[i], l[i], i);
ind[l[i]]++;
ind[r[i]]++;
}
int last = 0;
for (int i = 1; i <= m; i++) {
if (ind[i] & 1) {
if (last)
add(i, last, 0), add(last, i, 0), last = 0;
else
last = i;
}
}
for (int i = 1; i <= m; i++)
if (gkp[i] == 0) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, l[100015], r[100015], o[100015 << 1], tot, cnt, ans[100015],
ind[100015 << 1], head[100015 << 1];
struct edge {
int to, next, w;
edge() {}
edge(int u, int v, int x) {
to = u;
next = v;
w = x;
}
} e[100015 << 2];
bool vis[100015 << 2], gkp[100015 << 2];
void add(int u, int v, int w) {
e[++cnt] = edge(v, head[u], w);
head[u] = cnt;
}
void dfs(int u) {
gkp[u] = 1;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if (vis[i]) continue;
vis[i] = vis[i ^ 1] = 1;
ans[e[i].w] = (u < v);
dfs(v);
}
}
int main() {
memset(head, -1, sizeof head);
scanf("%d", &n);
cnt = -1;
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), ++r[i], o[++tot] = l[i], o[++tot] = r[i];
sort(o + 1, o + tot + 1);
int m = unique(o + 1, o + tot + 1) - o - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(o + 1, o + m + 1, l[i]) - o;
r[i] = lower_bound(o + 1, o + m + 1, r[i]) - o;
add(l[i], r[i], i);
add(r[i], l[i], i);
ind[l[i]]++;
ind[r[i]]++;
}
int last = 0;
for (int i = 1; i <= m; i++) {
if (ind[i] & 1) {
if (last)
add(i, last, 0), add(last, i, 0), last = 0;
else
last = i;
}
}
for (int i = 1; i <= m; i++)
if (gkp[i] == 0) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200200;
int fs[MAXN];
int sc[MAXN];
int color[MAXN];
vector<int> g[MAXN];
bool dfs(int v, int c) {
if (color[v] != -1) {
return color[v] == c;
}
color[v] = c;
for (int i = 0; i < (int)g[v].size(); i++) {
if (!dfs(g[v][i], c ^ 1)) {
return false;
}
}
return true;
}
int main() {
int n;
scanf("%d", &n);
vector<pair<int, int> > ord;
for (int i = 0; i < n; i++) {
int l, r;
scanf("%d%d", &l, &r);
ord.push_back(make_pair(l - 1, i));
ord.push_back(make_pair(r, i));
}
for (int i = 0; i < n; i++) {
fs[i] = sc[i] = -1;
}
sort(ord.begin(), ord.end());
for (int i = 0; i < 2 * n; i++) {
int j = ord[i].second;
if (fs[j] == -1) {
fs[j] = i;
} else {
sc[j] = i;
}
}
for (int i = 0; i < n; i++) {
g[2 * i].push_back(2 * i + 1);
g[2 * i + 1].push_back(2 * i);
g[fs[i]].push_back(sc[i]);
g[sc[i]].push_back(fs[i]);
}
for (int i = 0; i < 2 * n; i++) {
color[i] = -1;
}
bool ok = true;
for (int i = 0; i < 2 * n; i++) {
if (color[i] == -1) {
ok &= dfs(i, 0);
}
}
if (!ok) {
puts("-1");
} else {
for (int i = 0; i < n; i++) {
if (i > 0) {
printf(" ");
}
printf("%d", color[fs[i]]);
}
printf("\n");
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200200;
int fs[MAXN];
int sc[MAXN];
int color[MAXN];
vector<int> g[MAXN];
bool dfs(int v, int c) {
if (color[v] != -1) {
return color[v] == c;
}
color[v] = c;
for (int i = 0; i < (int)g[v].size(); i++) {
if (!dfs(g[v][i], c ^ 1)) {
return false;
}
}
return true;
}
int main() {
int n;
scanf("%d", &n);
vector<pair<int, int> > ord;
for (int i = 0; i < n; i++) {
int l, r;
scanf("%d%d", &l, &r);
ord.push_back(make_pair(l - 1, i));
ord.push_back(make_pair(r, i));
}
for (int i = 0; i < n; i++) {
fs[i] = sc[i] = -1;
}
sort(ord.begin(), ord.end());
for (int i = 0; i < 2 * n; i++) {
int j = ord[i].second;
if (fs[j] == -1) {
fs[j] = i;
} else {
sc[j] = i;
}
}
for (int i = 0; i < n; i++) {
g[2 * i].push_back(2 * i + 1);
g[2 * i + 1].push_back(2 * i);
g[fs[i]].push_back(sc[i]);
g[sc[i]].push_back(fs[i]);
}
for (int i = 0; i < 2 * n; i++) {
color[i] = -1;
}
bool ok = true;
for (int i = 0; i < 2 * n; i++) {
if (color[i] == -1) {
ok &= dfs(i, 0);
}
}
if (!ok) {
puts("-1");
} else {
for (int i = 0; i < n; i++) {
if (i > 0) {
printf(" ");
}
printf("%d", color[fs[i]]);
}
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, nxt, w;
} e[400001];
int edge_num = -1;
int head[200001];
void add(int u, int v) {
e[++edge_num].nxt = head[u];
e[edge_num].to = v;
head[u] = edge_num;
}
void add_edge(int u, int v) {
add(u, v);
add(v, u);
}
struct node {
int l, r;
} q[200001];
int in[200001];
void dfs(int u) {
for (int i = head[u]; i != -1; i = e[i].nxt) {
head[u] = i;
if (e[i].w == 0) {
e[i].w = 1, e[i ^ 1].w = 2;
dfs(e[i].to);
}
}
}
int main() {
memset(head, -1, sizeof(head));
int n;
scanf("%d", &n);
vector<int> v;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].r++;
v.push_back(q[i].l);
v.push_back(q[i].r);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; i++) {
q[i].l = lower_bound(v.begin(), v.end(), q[i].l) - v.begin() + 1;
q[i].r = lower_bound(v.begin(), v.end(), q[i].r) - v.begin() + 1;
add_edge(q[i].l, q[i].r);
in[q[i].l]++;
in[q[i].r]++;
}
vector<int> v1;
for (int i = 1; i <= v.size(); i++)
if (in[i] % 2 == 1) v1.push_back(i);
for (int i = 0; i < v1.size(); i += 2) add_edge(v1[i], v1[i + 1]);
for (int i = 1; i <= v.size(); i++) dfs(i);
for (int i = 0; i < 2 * n; i++) {
if (e[i].w == 1) printf("%d ", i % 2);
}
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, nxt, w;
} e[400001];
int edge_num = -1;
int head[200001];
void add(int u, int v) {
e[++edge_num].nxt = head[u];
e[edge_num].to = v;
head[u] = edge_num;
}
void add_edge(int u, int v) {
add(u, v);
add(v, u);
}
struct node {
int l, r;
} q[200001];
int in[200001];
void dfs(int u) {
for (int i = head[u]; i != -1; i = e[i].nxt) {
head[u] = i;
if (e[i].w == 0) {
e[i].w = 1, e[i ^ 1].w = 2;
dfs(e[i].to);
}
}
}
int main() {
memset(head, -1, sizeof(head));
int n;
scanf("%d", &n);
vector<int> v;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].r++;
v.push_back(q[i].l);
v.push_back(q[i].r);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; i++) {
q[i].l = lower_bound(v.begin(), v.end(), q[i].l) - v.begin() + 1;
q[i].r = lower_bound(v.begin(), v.end(), q[i].r) - v.begin() + 1;
add_edge(q[i].l, q[i].r);
in[q[i].l]++;
in[q[i].r]++;
}
vector<int> v1;
for (int i = 1; i <= v.size(); i++)
if (in[i] % 2 == 1) v1.push_back(i);
for (int i = 0; i < v1.size(); i += 2) add_edge(v1[i], v1[i + 1]);
for (int i = 1; i <= v.size(); i++) dfs(i);
for (int i = 0; i < 2 * n; i++) {
if (e[i].w == 1) printf("%d ", i % 2);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
pair<int, int> l[MAXN], r[MAXN], tot[MAXN * 2];
int n, cnt;
vector<int> G[MAXN * 2];
int col[MAXN * 2];
void dfs(int u, int c) {
if (col[u] != -1) return;
col[u] = c;
for (int i = 0; i < (int)G[u].size(); i++) dfs(G[u][i], c ^ 1);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
b++;
l[i] = make_pair(a, i << 1);
r[i] = make_pair(b, i << 1 | 1);
tot[cnt++] = l[i];
tot[cnt++] = r[i];
G[i * 2].push_back(i * 2 + 1);
G[i * 2 + 1].push_back(i * 2);
}
memset(col, -1, sizeof(col));
sort(tot, tot + cnt);
for (int i = 0; i < cnt; i += 2) {
G[tot[i].second].push_back(tot[i + 1].second);
G[tot[i + 1].second].push_back(tot[i].second);
}
for (int i = 0; i < cnt; i++) dfs(i, 0);
for (int i = 0; i < cnt; i += 2) printf("%d ", col[i]);
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
pair<int, int> l[MAXN], r[MAXN], tot[MAXN * 2];
int n, cnt;
vector<int> G[MAXN * 2];
int col[MAXN * 2];
void dfs(int u, int c) {
if (col[u] != -1) return;
col[u] = c;
for (int i = 0; i < (int)G[u].size(); i++) dfs(G[u][i], c ^ 1);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
b++;
l[i] = make_pair(a, i << 1);
r[i] = make_pair(b, i << 1 | 1);
tot[cnt++] = l[i];
tot[cnt++] = r[i];
G[i * 2].push_back(i * 2 + 1);
G[i * 2 + 1].push_back(i * 2);
}
memset(col, -1, sizeof(col));
sort(tot, tot + cnt);
for (int i = 0; i < cnt; i += 2) {
G[tot[i].second].push_back(tot[i + 1].second);
G[tot[i + 1].second].push_back(tot[i].second);
}
for (int i = 0; i < cnt; i++) dfs(i, 0);
for (int i = 0; i < cnt; i += 2) printf("%d ", col[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, px[100005], py[100005], ans[100005], deg[200005], vis[200005],
vis2[400005];
vector<int> all;
vector<pair<int, int> > e[200005];
void dfs(int x) {
vis[x] = 1;
while (!e[x].empty()) {
if (vis2[e[x].back().second]) {
e[x].pop_back();
continue;
}
if (e[x].back().second <= n)
ans[e[x].back().second] = (x < e[x].back().first);
vis2[e[x].back().second] = 1;
int t = e[x].back().first;
e[x].pop_back();
dfs(t);
}
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &px[i], &py[i]);
py[i]++;
all.push_back(px[i]);
all.push_back(py[i]);
}
sort(all.begin(), all.end());
all.resize(unique(all.begin(), all.end()) - all.begin());
for (i = 1; i <= n; i++) {
px[i] = upper_bound(all.begin(), all.end(), px[i]) - all.begin();
py[i] = upper_bound(all.begin(), all.end(), py[i]) - all.begin();
e[px[i]].push_back(make_pair(py[i], i));
e[py[i]].push_back(make_pair(px[i], i));
deg[px[i]]++;
deg[py[i]]++;
}
int lst = 0, tt = n;
for (i = 1; i <= all.size(); i++)
if (deg[i] & 1) {
if (lst) {
e[lst].push_back(make_pair(i, ++tt));
e[i].push_back(make_pair(lst, tt));
lst = 0;
} else
lst = i;
}
for (i = 1; i <= all.size(); i++)
if (!vis[i]) dfs(i);
for (i = 1; i <= n; i++) {
putchar(ans[i] | 48);
putchar(' ');
}
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, px[100005], py[100005], ans[100005], deg[200005], vis[200005],
vis2[400005];
vector<int> all;
vector<pair<int, int> > e[200005];
void dfs(int x) {
vis[x] = 1;
while (!e[x].empty()) {
if (vis2[e[x].back().second]) {
e[x].pop_back();
continue;
}
if (e[x].back().second <= n)
ans[e[x].back().second] = (x < e[x].back().first);
vis2[e[x].back().second] = 1;
int t = e[x].back().first;
e[x].pop_back();
dfs(t);
}
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &px[i], &py[i]);
py[i]++;
all.push_back(px[i]);
all.push_back(py[i]);
}
sort(all.begin(), all.end());
all.resize(unique(all.begin(), all.end()) - all.begin());
for (i = 1; i <= n; i++) {
px[i] = upper_bound(all.begin(), all.end(), px[i]) - all.begin();
py[i] = upper_bound(all.begin(), all.end(), py[i]) - all.begin();
e[px[i]].push_back(make_pair(py[i], i));
e[py[i]].push_back(make_pair(px[i], i));
deg[px[i]]++;
deg[py[i]]++;
}
int lst = 0, tt = n;
for (i = 1; i <= all.size(); i++)
if (deg[i] & 1) {
if (lst) {
e[lst].push_back(make_pair(i, ++tt));
e[i].push_back(make_pair(lst, tt));
lst = 0;
} else
lst = i;
}
for (i = 1; i <= all.size(); i++)
if (!vis[i]) dfs(i);
for (i = 1; i <= n; i++) {
putchar(ans[i] | 48);
putchar(' ');
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, nxt, w;
} e[400001];
int edge_num = -1;
int head[400001];
void add(int u, int v) {
e[++edge_num].nxt = head[u];
e[edge_num].to = v;
head[u] = edge_num;
}
void add_edge(int u, int v) {
add(u, v);
add(v, u);
}
struct node {
int l, r;
} q[200001];
int in[200001];
void dfs(int u) {
for (int i = head[u]; i != -1; i = e[i].nxt) {
head[u] = i;
if (e[i].w == 0) {
e[i].w = 1, e[i ^ 1].w = 2;
dfs(e[i].to);
}
}
}
int main() {
memset(head, -1, sizeof(head));
int n;
scanf("%d", &n);
vector<int> v;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].r++;
v.push_back(q[i].l);
v.push_back(q[i].r);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; i++) {
q[i].l = lower_bound(v.begin(), v.end(), q[i].l) - v.begin() + 1;
q[i].r = lower_bound(v.begin(), v.end(), q[i].r) - v.begin() + 1;
add_edge(q[i].l, q[i].r);
in[q[i].l]++;
in[q[i].r]++;
}
vector<int> v1;
for (int i = 1; i <= v.size(); i++)
if (in[i] % 2 == 1) v1.push_back(i);
for (int i = 0; i < v1.size(); i += 2) add_edge(v1[i], v1[i + 1]);
for (int i = 1; i <= v.size(); i++) dfs(i);
for (int i = 0; i < 2 * n; i++) {
if (e[i].w == 1) printf("%d ", i % 2);
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, nxt, w;
} e[400001];
int edge_num = -1;
int head[400001];
void add(int u, int v) {
e[++edge_num].nxt = head[u];
e[edge_num].to = v;
head[u] = edge_num;
}
void add_edge(int u, int v) {
add(u, v);
add(v, u);
}
struct node {
int l, r;
} q[200001];
int in[200001];
void dfs(int u) {
for (int i = head[u]; i != -1; i = e[i].nxt) {
head[u] = i;
if (e[i].w == 0) {
e[i].w = 1, e[i ^ 1].w = 2;
dfs(e[i].to);
}
}
}
int main() {
memset(head, -1, sizeof(head));
int n;
scanf("%d", &n);
vector<int> v;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].r++;
v.push_back(q[i].l);
v.push_back(q[i].r);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; i++) {
q[i].l = lower_bound(v.begin(), v.end(), q[i].l) - v.begin() + 1;
q[i].r = lower_bound(v.begin(), v.end(), q[i].r) - v.begin() + 1;
add_edge(q[i].l, q[i].r);
in[q[i].l]++;
in[q[i].r]++;
}
vector<int> v1;
for (int i = 1; i <= v.size(); i++)
if (in[i] % 2 == 1) v1.push_back(i);
for (int i = 0; i < v1.size(); i += 2) add_edge(v1[i], v1[i + 1]);
for (int i = 1; i <= v.size(); i++) dfs(i);
for (int i = 0; i < 2 * n; i++) {
if (e[i].w == 1) printf("%d ", i % 2);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
multimap<int, int> mp;
int n, L, R, head[200005], tot;
struct ccf {
int to, nxt;
} e[1000005];
void add(int a, int b) {
tot++;
e[tot].to = b;
e[tot].nxt = head[a];
head[a] = tot;
}
int vis[200005];
void dfs(int now, int se) {
if (vis[now]) return;
vis[now] = se;
for (int i = head[now]; i; i = e[i].nxt) dfs(e[i].to, 3 - se);
}
int x, y;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &L, &R);
mp.insert(make_pair(L * 2, i * 2));
mp.insert(make_pair(R * 2 + 1, i * 2 + 1));
add(i * 2, i * 2 + 1);
add(i * 2 + 1, i * 2);
}
for (map<int, int>::iterator it = mp.begin(); it != mp.end();) {
x = it->second;
it++;
y = it->second;
it++;
add(x, y);
add(y, x);
}
for (int i = 1; i <= n; i++)
if (!vis[i * 2]) dfs(i * 2, 1);
for (int i = 1; i <= n; i++) printf("%d ", vis[i * 2] - 1);
}
|
### Prompt
Please create a solution in CPP to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
multimap<int, int> mp;
int n, L, R, head[200005], tot;
struct ccf {
int to, nxt;
} e[1000005];
void add(int a, int b) {
tot++;
e[tot].to = b;
e[tot].nxt = head[a];
head[a] = tot;
}
int vis[200005];
void dfs(int now, int se) {
if (vis[now]) return;
vis[now] = se;
for (int i = head[now]; i; i = e[i].nxt) dfs(e[i].to, 3 - se);
}
int x, y;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &L, &R);
mp.insert(make_pair(L * 2, i * 2));
mp.insert(make_pair(R * 2 + 1, i * 2 + 1));
add(i * 2, i * 2 + 1);
add(i * 2 + 1, i * 2);
}
for (map<int, int>::iterator it = mp.begin(); it != mp.end();) {
x = it->second;
it++;
y = it->second;
it++;
add(x, y);
add(y, x);
}
for (int i = 1; i <= n; i++)
if (!vis[i * 2]) dfs(i * 2, 1);
for (int i = 1; i <= n; i++) printf("%d ", vis[i * 2] - 1);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
inline int gi() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
int n, l[maxn], r[maxn], ans[maxn], *q[maxn], num, cnt;
struct edge {
int to, next, Id;
} e[maxn * 2];
int h[maxn], vis[maxn], deg[maxn], tot = 1;
inline void add(int u, int v, int Id) {
e[++tot] = (edge){v, h[u], Id};
h[u] = tot;
e[++tot] = (edge){u, h[v], Id};
h[v] = tot;
}
void dfs(int u) {
for (int &i = h[u]; i; i = e[i].next)
if (!vis[i >> 1]) vis[i >> 1] = 1, ans[e[i].Id] = i & 1, dfs(e[i].to);
}
int main() {
n = gi();
for (int i = 1; i <= n; ++i)
l[i] = gi(), r[i] = gi() + 1, q[++cnt] = &l[i], q[++cnt] = &r[i];
sort(q + 1, q + cnt + 1, [](int *a, int *b) { return *a < *b; });
num = 0;
for (int k = -1, i = 1; i <= cnt; ++i)
if (*q[i] == k)
*q[i] = num;
else
k = *q[i], *q[i] = ++num;
for (int i = 1; i <= n; ++i)
deg[l[i]] ^= 1, deg[r[i]] ^= 1, add(l[i], r[i], i);
for (int i = 1; i <= num; ++i) {
deg[i] ^= deg[i - 1];
if (deg[i] == 1) add(i, i + 1, 0);
}
for (int i = 1; i <= num; ++i)
if (h[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
inline int gi() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
int n, l[maxn], r[maxn], ans[maxn], *q[maxn], num, cnt;
struct edge {
int to, next, Id;
} e[maxn * 2];
int h[maxn], vis[maxn], deg[maxn], tot = 1;
inline void add(int u, int v, int Id) {
e[++tot] = (edge){v, h[u], Id};
h[u] = tot;
e[++tot] = (edge){u, h[v], Id};
h[v] = tot;
}
void dfs(int u) {
for (int &i = h[u]; i; i = e[i].next)
if (!vis[i >> 1]) vis[i >> 1] = 1, ans[e[i].Id] = i & 1, dfs(e[i].to);
}
int main() {
n = gi();
for (int i = 1; i <= n; ++i)
l[i] = gi(), r[i] = gi() + 1, q[++cnt] = &l[i], q[++cnt] = &r[i];
sort(q + 1, q + cnt + 1, [](int *a, int *b) { return *a < *b; });
num = 0;
for (int k = -1, i = 1; i <= cnt; ++i)
if (*q[i] == k)
*q[i] = num;
else
k = *q[i], *q[i] = ++num;
for (int i = 1; i <= n; ++i)
deg[l[i]] ^= 1, deg[r[i]] ^= 1, add(l[i], r[i], i);
for (int i = 1; i <= num; ++i) {
deg[i] ^= deg[i - 1];
if (deg[i] == 1) add(i, i + 1, 0);
}
for (int i = 1; i <= num; ++i)
if (h[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
namespace FGF {
const int N = 5e5 + 5;
struct edg {
int to, nxt;
} e[N];
int cnt = 1, l[N], r[N], c[N], n, tot, tp, st[N], vis[N], col[N], head[N],
du[N];
void add(int u, int v) {
cnt++;
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
du[u]++;
}
void dfs(int u) {
vis[u] = 1;
for (int &i = head[u]; i; i = e[i].nxt) {
if (col[i >> 1]) continue;
col[i >> 1] = (u > e[i].to) + 1;
dfs(e[i].to);
}
}
void work() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), r[i]++, c[++tot] = l[i], c[++tot] = r[i];
sort(c + 1, c + tot + 1);
tot = unique(c + 1, c + tot + 1) - c - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(c + 1, c + tot + 1, l[i]) - c,
r[i] = lower_bound(c + 1, c + tot + 1, r[i]) - c;
add(l[i], r[i]), add(r[i], l[i]);
}
for (int i = 1; i <= tot; i++)
if (du[i] & 1) st[++tp] = i;
for (int i = 1; i <= tp; i += 2) add(st[i], st[i + 1]), add(st[i + 1], st[i]);
for (int i = 1; i <= tot; i++)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", col[i] - 1);
}
} // namespace FGF
int main() {
FGF::work();
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace FGF {
const int N = 5e5 + 5;
struct edg {
int to, nxt;
} e[N];
int cnt = 1, l[N], r[N], c[N], n, tot, tp, st[N], vis[N], col[N], head[N],
du[N];
void add(int u, int v) {
cnt++;
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
du[u]++;
}
void dfs(int u) {
vis[u] = 1;
for (int &i = head[u]; i; i = e[i].nxt) {
if (col[i >> 1]) continue;
col[i >> 1] = (u > e[i].to) + 1;
dfs(e[i].to);
}
}
void work() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &l[i], &r[i]), r[i]++, c[++tot] = l[i], c[++tot] = r[i];
sort(c + 1, c + tot + 1);
tot = unique(c + 1, c + tot + 1) - c - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(c + 1, c + tot + 1, l[i]) - c,
r[i] = lower_bound(c + 1, c + tot + 1, r[i]) - c;
add(l[i], r[i]), add(r[i], l[i]);
}
for (int i = 1; i <= tot; i++)
if (du[i] & 1) st[++tp] = i;
for (int i = 1; i <= tp; i += 2) add(st[i], st[i + 1]), add(st[i + 1], st[i]);
for (int i = 1; i <= tot; i++)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", col[i] - 1);
}
} // namespace FGF
int main() {
FGF::work();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
pair<int, int> e[maxn];
int n;
int vis[maxn];
int deg[maxn];
vector<int> adj[maxn];
void dfs(int u) {
while (adj[u].size()) {
int tmp = adj[u].back();
adj[u].pop_back();
if (vis[tmp] == 0) {
if (deg[u] > 0)
vis[tmp] = 1 + (e[tmp].first == u), deg[u]--;
else
vis[tmp] = 1 + (e[tmp].second == u),
deg[e[tmp].first + e[tmp].second - u]--;
dfs(e[tmp].first + e[tmp].second - u);
}
}
}
void dfs1(int u) {
while (adj[u].size()) {
int tmp = adj[u].back();
adj[u].pop_back();
if (vis[tmp] == 0) {
vis[tmp] = 1 + (e[tmp].first == u), deg[u]--,
deg[e[tmp].first + e[tmp].second - u]--;
if (deg[e[tmp].first + e[tmp].second - u] & 1)
dfs1(e[tmp].first + e[tmp].second - u);
break;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("A"
".INP",
"r"))
freopen(
"A"
".INP",
"r", stdin),
freopen(
"A"
".OUT",
"w", stdout);
vector<int> val;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> e[i].first >> e[i].second;
++e[i].second;
val.push_back(e[i].first);
val.push_back(e[i].second);
}
sort(val.begin(), val.end());
val.erase(unique(val.begin(), val.end()), val.end());
for (int i = 1; i <= n; ++i) {
e[i].first =
lower_bound(val.begin(), val.end(), e[i].first) - val.begin() + 1;
e[i].second =
lower_bound(val.begin(), val.end(), e[i].second) - val.begin() + 1;
deg[e[i].first]++;
deg[e[i].second]++;
}
bool cur = 0;
int m = n;
for (int i = 1; i < maxn; ++i) {
cur ^= (deg[i] & 1);
if (cur) {
++m;
e[m] = make_pair(i, i + 1);
}
}
for (int i = 1; i <= m; ++i) {
adj[e[i].first].push_back(i);
adj[e[i].second].push_back(i);
}
for (int i = 1; i < maxn; ++i) deg[i] = adj[i].size() / 2;
for (int i = 1; i < maxn; ++i) dfs(i);
for (int i = 1; i <= n; ++i) {
cout << vis[i] - 1 << " ";
}
}
|
### Prompt
Create a solution in CPP for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
pair<int, int> e[maxn];
int n;
int vis[maxn];
int deg[maxn];
vector<int> adj[maxn];
void dfs(int u) {
while (adj[u].size()) {
int tmp = adj[u].back();
adj[u].pop_back();
if (vis[tmp] == 0) {
if (deg[u] > 0)
vis[tmp] = 1 + (e[tmp].first == u), deg[u]--;
else
vis[tmp] = 1 + (e[tmp].second == u),
deg[e[tmp].first + e[tmp].second - u]--;
dfs(e[tmp].first + e[tmp].second - u);
}
}
}
void dfs1(int u) {
while (adj[u].size()) {
int tmp = adj[u].back();
adj[u].pop_back();
if (vis[tmp] == 0) {
vis[tmp] = 1 + (e[tmp].first == u), deg[u]--,
deg[e[tmp].first + e[tmp].second - u]--;
if (deg[e[tmp].first + e[tmp].second - u] & 1)
dfs1(e[tmp].first + e[tmp].second - u);
break;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("A"
".INP",
"r"))
freopen(
"A"
".INP",
"r", stdin),
freopen(
"A"
".OUT",
"w", stdout);
vector<int> val;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> e[i].first >> e[i].second;
++e[i].second;
val.push_back(e[i].first);
val.push_back(e[i].second);
}
sort(val.begin(), val.end());
val.erase(unique(val.begin(), val.end()), val.end());
for (int i = 1; i <= n; ++i) {
e[i].first =
lower_bound(val.begin(), val.end(), e[i].first) - val.begin() + 1;
e[i].second =
lower_bound(val.begin(), val.end(), e[i].second) - val.begin() + 1;
deg[e[i].first]++;
deg[e[i].second]++;
}
bool cur = 0;
int m = n;
for (int i = 1; i < maxn; ++i) {
cur ^= (deg[i] & 1);
if (cur) {
++m;
e[m] = make_pair(i, i + 1);
}
}
for (int i = 1; i <= m; ++i) {
adj[e[i].first].push_back(i);
adj[e[i].second].push_back(i);
}
for (int i = 1; i < maxn; ++i) deg[i] = adj[i].size() / 2;
for (int i = 1; i < maxn; ++i) dfs(i);
for (int i = 1; i <= n; ++i) {
cout << vis[i] - 1 << " ";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 200 * 1000 + 10;
int n, cnt_e;
vector<int> adj[N], vec, ans, odd, tour;
vector<pii> ed;
map<pii, int> mp;
map<int, int> mp_ed;
set<int> vis;
void add_edge(int u, int v) {
cnt_e++;
mp_ed[cnt_e] = mp_ed[cnt_e] = u ^ v;
adj[u].push_back(cnt_e);
adj[v].push_back(cnt_e);
}
void euler(int v) {
while (!adj[v].empty()) {
int e = adj[v].back();
adj[v].pop_back();
if (!vis.count(e)) {
int u = mp_ed[e] ^ v;
vis.insert(e);
if (mp.count({u, v}) && ans[mp[{u, v}] - 1] == -1)
ans[mp[{u, v}] - 1] = 1;
else if (mp.count({v, u}) && ans[mp[{v, u}] - 1] == -1)
ans[mp[{v, u}] - 1] = 0;
euler(u);
}
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n;
ans = vector<int>(n, -1);
for (int i = 0, l, r; i < n; i++) {
cin >> l >> r, r++, ed.push_back({l, r});
vec.push_back(l), vec.push_back(r);
}
sort(vec.begin(), vec.end());
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
for (int i = 0; i < n; i++) {
ed[i].first =
lower_bound(vec.begin(), vec.end(), ed[i].first) - vec.begin();
ed[i].second =
lower_bound(vec.begin(), vec.end(), ed[i].second) - vec.begin();
add_edge(ed[i].first, ed[i].second);
mp[{ed[i].first, ed[i].second}] = i + 1;
}
for (int i = 0; i < vec.size(); i++)
if ((int)adj[i].size() & 1) odd.push_back(i);
for (int i = 0; i < odd.size(); i += 2) add_edge(odd[i], odd[i + 1]);
for (int i = 0; i < vec.size(); i++)
if (!adj[i].empty()) euler(i);
for (int x : ans) cout << x << ' ';
cout << '\n';
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 200 * 1000 + 10;
int n, cnt_e;
vector<int> adj[N], vec, ans, odd, tour;
vector<pii> ed;
map<pii, int> mp;
map<int, int> mp_ed;
set<int> vis;
void add_edge(int u, int v) {
cnt_e++;
mp_ed[cnt_e] = mp_ed[cnt_e] = u ^ v;
adj[u].push_back(cnt_e);
adj[v].push_back(cnt_e);
}
void euler(int v) {
while (!adj[v].empty()) {
int e = adj[v].back();
adj[v].pop_back();
if (!vis.count(e)) {
int u = mp_ed[e] ^ v;
vis.insert(e);
if (mp.count({u, v}) && ans[mp[{u, v}] - 1] == -1)
ans[mp[{u, v}] - 1] = 1;
else if (mp.count({v, u}) && ans[mp[{v, u}] - 1] == -1)
ans[mp[{v, u}] - 1] = 0;
euler(u);
}
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n;
ans = vector<int>(n, -1);
for (int i = 0, l, r; i < n; i++) {
cin >> l >> r, r++, ed.push_back({l, r});
vec.push_back(l), vec.push_back(r);
}
sort(vec.begin(), vec.end());
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
for (int i = 0; i < n; i++) {
ed[i].first =
lower_bound(vec.begin(), vec.end(), ed[i].first) - vec.begin();
ed[i].second =
lower_bound(vec.begin(), vec.end(), ed[i].second) - vec.begin();
add_edge(ed[i].first, ed[i].second);
mp[{ed[i].first, ed[i].second}] = i + 1;
}
for (int i = 0; i < vec.size(); i++)
if ((int)adj[i].size() & 1) odd.push_back(i);
for (int i = 0; i < odd.size(); i += 2) add_edge(odd[i], odd[i + 1]);
for (int i = 0; i < vec.size(); i++)
if (!adj[i].empty()) euler(i);
for (int x : ans) cout << x << ' ';
cout << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> e[210000];
int col[210000], n;
pair<int, int> p[210000];
void dfs(int u, int color) {
if (col[u] != -1) return;
col[u] = color;
for (int i = 0; i < (int)e[u].size(); ++i) dfs(e[u][i], color ^ 1);
}
void solve() {
for (int i = 0; i <= n * 2; ++i) e[i].clear();
int l, r;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &l, &r);
e[i << 1].push_back(i << 1 | 1), e[i << 1 | 1].push_back(i << 1);
p[i << 1] = make_pair(l << 1, i << 1),
p[i << 1 | 1] = make_pair(r << 1 | 1, i << 1 | 1);
}
sort(p, p + 2 * n);
for (int i = 0; i < n; ++i) {
int x = p[i << 1].second, y = p[i << 1 | 1].second;
e[x].push_back(y), e[y].push_back(x);
}
memset(col, -1, sizeof(col));
for (int i = 0; i < n; ++i) {
if (col[i << 1] == -1) dfs(i << 1, 0);
printf("%d ", col[i << 1]);
}
}
int main() {
while (scanf("%d", &n) != EOF) {
solve();
}
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> e[210000];
int col[210000], n;
pair<int, int> p[210000];
void dfs(int u, int color) {
if (col[u] != -1) return;
col[u] = color;
for (int i = 0; i < (int)e[u].size(); ++i) dfs(e[u][i], color ^ 1);
}
void solve() {
for (int i = 0; i <= n * 2; ++i) e[i].clear();
int l, r;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &l, &r);
e[i << 1].push_back(i << 1 | 1), e[i << 1 | 1].push_back(i << 1);
p[i << 1] = make_pair(l << 1, i << 1),
p[i << 1 | 1] = make_pair(r << 1 | 1, i << 1 | 1);
}
sort(p, p + 2 * n);
for (int i = 0; i < n; ++i) {
int x = p[i << 1].second, y = p[i << 1 | 1].second;
e[x].push_back(y), e[y].push_back(x);
}
memset(col, -1, sizeof(col));
for (int i = 0; i < n; ++i) {
if (col[i << 1] == -1) dfs(i << 1, 0);
printf("%d ", col[i << 1]);
}
}
int main() {
while (scanf("%d", &n) != EOF) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
struct aa {
int l, r;
} pp[201000];
vector<pair<int, int> > _hash;
vector<pair<int, int> >::iterator it;
int color[201000];
struct node {
int t, nxt;
} edge[201000 << 3];
int headline[201000], E;
inline void add(int f, int t) {
edge[E].t = t;
edge[E].nxt = headline[f];
headline[f] = E++;
edge[E].t = f;
edge[E].nxt = headline[t];
headline[t] = E++;
}
void dfs(int u, int c) {
color[u] = c;
for (int i = headline[u]; ~i; i = edge[i].nxt) {
int v = edge[i].t;
if (color[v] == -1) dfs(v, c ^ 1);
}
}
void solve(void) {
_hash.clear();
memset(headline, -1, sizeof(headline));
E = 0;
for (int i = 0; i < (n); i++) {
scanf("%d%d", &pp[i].l, &pp[i].r);
add(i << 1, i << 1 | 1);
_hash.push_back(pair<int, int>(pp[i].l << 1, i << 1));
_hash.push_back(pair<int, int>(pp[i].r << 1 | 1, i << 1 | 1));
}
sort(_hash.begin(), _hash.end());
int ss = _hash.size();
for (int i = 0; i < ss; i += 2) {
add(_hash[i].second, _hash[i + 1].second);
}
memset(color, -1, sizeof(color));
for (int i = 0; i < (2 * n); i++) {
if (color[i] == -1) dfs(i, 0);
}
for (int i = 0; i < (n); i++) {
printf("%d%c", color[i << 1], (i == n - 1) ? '\n' : ' ');
}
}
int main(void) {
while (EOF != scanf("%d", &n)) solve();
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
struct aa {
int l, r;
} pp[201000];
vector<pair<int, int> > _hash;
vector<pair<int, int> >::iterator it;
int color[201000];
struct node {
int t, nxt;
} edge[201000 << 3];
int headline[201000], E;
inline void add(int f, int t) {
edge[E].t = t;
edge[E].nxt = headline[f];
headline[f] = E++;
edge[E].t = f;
edge[E].nxt = headline[t];
headline[t] = E++;
}
void dfs(int u, int c) {
color[u] = c;
for (int i = headline[u]; ~i; i = edge[i].nxt) {
int v = edge[i].t;
if (color[v] == -1) dfs(v, c ^ 1);
}
}
void solve(void) {
_hash.clear();
memset(headline, -1, sizeof(headline));
E = 0;
for (int i = 0; i < (n); i++) {
scanf("%d%d", &pp[i].l, &pp[i].r);
add(i << 1, i << 1 | 1);
_hash.push_back(pair<int, int>(pp[i].l << 1, i << 1));
_hash.push_back(pair<int, int>(pp[i].r << 1 | 1, i << 1 | 1));
}
sort(_hash.begin(), _hash.end());
int ss = _hash.size();
for (int i = 0; i < ss; i += 2) {
add(_hash[i].second, _hash[i + 1].second);
}
memset(color, -1, sizeof(color));
for (int i = 0; i < (2 * n); i++) {
if (color[i] == -1) dfs(i, 0);
}
for (int i = 0; i < (n); i++) {
printf("%d%c", color[i << 1], (i == n - 1) ? '\n' : ' ');
}
}
int main(void) {
while (EOF != scanf("%d", &n)) solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct E {
int x, o, i;
} e[100010 << 1];
int cnt;
struct _E {
int ed, o, i, ne;
} _e[100010 << 3];
int in[100010 << 1], he[100010 << 1], en = 1;
bool vis[100010 << 3];
int path[100010 << 2], tot;
int ans[100010];
bool cmp1(E a, E b) { return a.x < b.x; }
bool cmp2(E a, E b) { return a.i == b.i ? a.o < b.o : a.i < b.i; }
void ins(int a, int b, int i) {
++in[a];
++in[b];
_e[++en].ed = b;
_e[en].o = 0;
_e[en].i = i;
_e[en].ne = he[a];
he[a] = en;
_e[++en].ed = a;
_e[en].o = 1;
_e[en].i = i;
_e[en].ne = he[b];
he[b] = en;
}
void dfs(int u) {
for (int &i = he[u]; i; i = _e[i].ne)
if (!vis[i]) {
vis[i] = vis[i ^ 1] = 1;
int j = i;
dfs(_e[i].ed);
path[++tot] = j;
}
}
int main() {
int i, x, la;
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
scanf("%d", &x);
e[++cnt] = (E){x, 0, i};
scanf("%d", &x);
e[++cnt] = (E){x + 1, 1, i};
}
sort(e + 1, e + cnt + 1, cmp1);
for (i = 1; i <= cnt; ++i) {
if (i == 1 || e[i].x != la) {
++m;
la = e[i].x;
}
e[i].x = m;
}
sort(e + 1, e + cnt + 1, cmp2);
for (i = 1; i <= n; ++i) ins(e[(i << 1) - 1].x, e[i << 1].x, e[i << 1].i);
la = 0;
for (i = 1; i <= m; ++i)
if (in[i] & 1) {
if (!la)
la = i;
else {
ins(la, i, 0);
la = 0;
}
}
for (i = 1; i <= m; ++i) dfs(i);
for (i = 1; i <= tot; ++i) {
x = path[i];
ans[_e[x].i] = _e[x].o;
}
for (i = 1; i <= n; ++i) printf("%d ", ans[i]);
puts("");
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct E {
int x, o, i;
} e[100010 << 1];
int cnt;
struct _E {
int ed, o, i, ne;
} _e[100010 << 3];
int in[100010 << 1], he[100010 << 1], en = 1;
bool vis[100010 << 3];
int path[100010 << 2], tot;
int ans[100010];
bool cmp1(E a, E b) { return a.x < b.x; }
bool cmp2(E a, E b) { return a.i == b.i ? a.o < b.o : a.i < b.i; }
void ins(int a, int b, int i) {
++in[a];
++in[b];
_e[++en].ed = b;
_e[en].o = 0;
_e[en].i = i;
_e[en].ne = he[a];
he[a] = en;
_e[++en].ed = a;
_e[en].o = 1;
_e[en].i = i;
_e[en].ne = he[b];
he[b] = en;
}
void dfs(int u) {
for (int &i = he[u]; i; i = _e[i].ne)
if (!vis[i]) {
vis[i] = vis[i ^ 1] = 1;
int j = i;
dfs(_e[i].ed);
path[++tot] = j;
}
}
int main() {
int i, x, la;
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
scanf("%d", &x);
e[++cnt] = (E){x, 0, i};
scanf("%d", &x);
e[++cnt] = (E){x + 1, 1, i};
}
sort(e + 1, e + cnt + 1, cmp1);
for (i = 1; i <= cnt; ++i) {
if (i == 1 || e[i].x != la) {
++m;
la = e[i].x;
}
e[i].x = m;
}
sort(e + 1, e + cnt + 1, cmp2);
for (i = 1; i <= n; ++i) ins(e[(i << 1) - 1].x, e[i << 1].x, e[i << 1].i);
la = 0;
for (i = 1; i <= m; ++i)
if (in[i] & 1) {
if (!la)
la = i;
else {
ins(la, i, 0);
la = 0;
}
}
for (i = 1; i <= m; ++i) dfs(i);
for (i = 1; i <= tot; ++i) {
x = path[i];
ans[_e[x].i] = _e[x].o;
}
for (i = 1; i <= n; ++i) printf("%d ", ans[i]);
puts("");
return 0;
}
```
|
#include <bits/stdc++.h>
int n;
struct pii {
int p, t;
} P[1111111];
bool operator<(const pii x, const pii y) {
return (x.p != y.p) ? x.p < y.p : x.t > y.t;
}
std::vector<pii> v[1111111];
void ins(int x, int y, int z) {
if (x < 0) x = -x;
if (y < 0) y = -y;
v[x].push_back(pii{y, z}), v[y].push_back(pii{x, z});
}
int col[1111111];
void dfs(int x, int c = 1) {
col[x] = c;
for (pii t : v[x])
if (!col[t.p]) dfs(t.p, c * t.t);
}
int main() {
scanf("%d", &n);
register int i;
for (i = 1; i <= n; i++) {
int L, R;
scanf("%d%d", &L, &R);
P[i] = pii{L, i}, P[i + n] = pii{R, -i};
}
std::sort(P + 1, P + n * 2 + 1);
for (i = 1; i <= n * 2; i += 2) {
if (P[i].t * 1ll * P[i + 1].t > 0)
ins(P[i].t, P[i + 1].t, -1);
else
ins(P[i].t, P[i + 1].t, 1);
}
for (i = 1; i <= n; i++)
if (!col[i]) dfs(i);
for (i = 1; i <= n; i++) printf("%d ", col[i] > 0);
puts("");
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
int n;
struct pii {
int p, t;
} P[1111111];
bool operator<(const pii x, const pii y) {
return (x.p != y.p) ? x.p < y.p : x.t > y.t;
}
std::vector<pii> v[1111111];
void ins(int x, int y, int z) {
if (x < 0) x = -x;
if (y < 0) y = -y;
v[x].push_back(pii{y, z}), v[y].push_back(pii{x, z});
}
int col[1111111];
void dfs(int x, int c = 1) {
col[x] = c;
for (pii t : v[x])
if (!col[t.p]) dfs(t.p, c * t.t);
}
int main() {
scanf("%d", &n);
register int i;
for (i = 1; i <= n; i++) {
int L, R;
scanf("%d%d", &L, &R);
P[i] = pii{L, i}, P[i + n] = pii{R, -i};
}
std::sort(P + 1, P + n * 2 + 1);
for (i = 1; i <= n * 2; i += 2) {
if (P[i].t * 1ll * P[i + 1].t > 0)
ins(P[i].t, P[i + 1].t, -1);
else
ins(P[i].t, P[i + 1].t, 1);
}
for (i = 1; i <= n; i++)
if (!col[i]) dfs(i);
for (i = 1; i <= n; i++) printf("%d ", col[i] > 0);
puts("");
}
```
|
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, sgn = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * sgn;
}
const int N = 1e5 + 10;
int n, b[N << 1], tot, l[N], r[N], deg[N << 1], h[N << 1], cnt = 1, now[N << 1],
ans[N << 1];
struct edge {
int v, w, nxt;
} e[N << 2];
bool v[N << 2], vis[N << 1];
void link(int x, int y, int w) {
e[++cnt] = (edge){y, w, h[x]};
h[x] = cnt;
}
void dfs(int x) {
vis[x] = 1;
for (int i = now[x], y; i; i = e[now[x]].nxt) {
now[x] = i;
if (v[i]) continue;
y = e[i].v;
v[i] = v[i ^ 1] = 1;
ans[e[i].w] = x < y;
dfs(y);
}
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
b[++tot] = l[i] = read(), b[++tot] = r[i] = read() + 1;
sort(b + 1, b + tot + 1);
tot = unique(b + 1, b + tot + 1) - b - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(b + 1, b + tot + 1, l[i]) - b;
r[i] = lower_bound(b + 1, b + tot + 1, r[i]) - b;
deg[l[i]]++;
deg[r[i]]++;
link(l[i], r[i], i);
link(r[i], l[i], i);
}
int lst = 0;
for (int i = 1; i <= tot; i++) {
if (!(deg[i] & 1)) continue;
if (lst)
link(lst, i, 0), link(i, lst, 0), lst = 0;
else
lst = i;
}
for (int i = 1; i <= tot; i++) now[i] = h[i];
for (int i = 1; i <= tot; i++)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; i++) {
if (ans[i])
printf("1 ");
else
printf("0 ");
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, sgn = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * sgn;
}
const int N = 1e5 + 10;
int n, b[N << 1], tot, l[N], r[N], deg[N << 1], h[N << 1], cnt = 1, now[N << 1],
ans[N << 1];
struct edge {
int v, w, nxt;
} e[N << 2];
bool v[N << 2], vis[N << 1];
void link(int x, int y, int w) {
e[++cnt] = (edge){y, w, h[x]};
h[x] = cnt;
}
void dfs(int x) {
vis[x] = 1;
for (int i = now[x], y; i; i = e[now[x]].nxt) {
now[x] = i;
if (v[i]) continue;
y = e[i].v;
v[i] = v[i ^ 1] = 1;
ans[e[i].w] = x < y;
dfs(y);
}
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
b[++tot] = l[i] = read(), b[++tot] = r[i] = read() + 1;
sort(b + 1, b + tot + 1);
tot = unique(b + 1, b + tot + 1) - b - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(b + 1, b + tot + 1, l[i]) - b;
r[i] = lower_bound(b + 1, b + tot + 1, r[i]) - b;
deg[l[i]]++;
deg[r[i]]++;
link(l[i], r[i], i);
link(r[i], l[i], i);
}
int lst = 0;
for (int i = 1; i <= tot; i++) {
if (!(deg[i] & 1)) continue;
if (lst)
link(lst, i, 0), link(i, lst, 0), lst = 0;
else
lst = i;
}
for (int i = 1; i <= tot; i++) now[i] = h[i];
for (int i = 1; i <= tot; i++)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; i++) {
if (ans[i])
printf("1 ");
else
printf("0 ");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
char buf[100000], *p1 = buf, *p2 = buf;
inline int gi() {
int x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
}
return (f == 1) ? x : -x;
}
const int maxn = 2e5 + 5;
int l[maxn], r[maxn], temp[maxn], tot, n, cnt;
vector<pair<int, int>> e[maxn];
int vis[maxn], d[maxn];
inline void input() {
n = gi();
for (int i = 1; i <= n; ++i)
l[i] = gi(), r[i] = gi(), temp[++tot] = l[i], temp[++tot] = r[i] + 1;
sort(temp + 1, temp + tot + 1),
tot = unique(temp + 1, temp + tot + 1) - temp - 1;
for (int i = 1; i <= n; ++i)
l[i] = lower_bound(temp + 1, temp + tot + 1, l[i]) - temp,
r[i] = lower_bound(temp + 1, temp + tot + 1, r[i] + 1) - temp;
}
int cur[maxn];
inline void dfs(int u) {
for (int &i = cur[u]; i < (int)(e[u].size()); i++) {
int id = e[u][i].first, v = e[u][i].second;
if (vis[id]) continue;
vis[id] = v > u ? 2 : 1;
dfs(v);
}
}
inline void solve() {
vector<int> v;
for (int i = 1; i <= n; ++i)
e[l[i]].push_back({i, r[i]}), e[r[i]].push_back({i, l[i]}), d[l[i]]++,
d[r[i]]++;
for (int i = 1; i <= tot; ++i)
if (d[i] & 1) v.push_back(i);
cnt = n;
for (int i = 0; i < (int)(v.size()); i += 2) {
++cnt;
e[v[i]].push_back({cnt, v[i + 1]});
e[v[i + 1]].push_back({cnt, v[i]});
}
for (int i = 1; i <= tot; ++i) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", vis[i] - 1);
}
int main() {
input();
solve();
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char buf[100000], *p1 = buf, *p2 = buf;
inline int gi() {
int x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
}
return (f == 1) ? x : -x;
}
const int maxn = 2e5 + 5;
int l[maxn], r[maxn], temp[maxn], tot, n, cnt;
vector<pair<int, int>> e[maxn];
int vis[maxn], d[maxn];
inline void input() {
n = gi();
for (int i = 1; i <= n; ++i)
l[i] = gi(), r[i] = gi(), temp[++tot] = l[i], temp[++tot] = r[i] + 1;
sort(temp + 1, temp + tot + 1),
tot = unique(temp + 1, temp + tot + 1) - temp - 1;
for (int i = 1; i <= n; ++i)
l[i] = lower_bound(temp + 1, temp + tot + 1, l[i]) - temp,
r[i] = lower_bound(temp + 1, temp + tot + 1, r[i] + 1) - temp;
}
int cur[maxn];
inline void dfs(int u) {
for (int &i = cur[u]; i < (int)(e[u].size()); i++) {
int id = e[u][i].first, v = e[u][i].second;
if (vis[id]) continue;
vis[id] = v > u ? 2 : 1;
dfs(v);
}
}
inline void solve() {
vector<int> v;
for (int i = 1; i <= n; ++i)
e[l[i]].push_back({i, r[i]}), e[r[i]].push_back({i, l[i]}), d[l[i]]++,
d[r[i]]++;
for (int i = 1; i <= tot; ++i)
if (d[i] & 1) v.push_back(i);
cnt = n;
for (int i = 0; i < (int)(v.size()); i += 2) {
++cnt;
e[v[i]].push_back({cnt, v[i + 1]});
e[v[i + 1]].push_back({cnt, v[i]});
}
for (int i = 1; i <= tot; ++i) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", vis[i] - 1);
}
int main() {
input();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 400050;
int a[N], b[N], w[N], Id, d[N], nn[N][2], o[N], head[N], top, tot, bl[N], cnt,
vis[N], st[N], bj[N];
void dfs(int x) {
vis[x] = 1;
for (int i = head[x], y; i; i = nn[i][0])
if (!bj[bl[i]]) {
bj[bl[i]] = 1, dfs(y = nn[i][1]);
if (x < y)
w[bl[i]] = 0;
else
w[bl[i]] = 1;
}
return;
}
int main() {
int n, u, v;
cin >> n;
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &u, &v), o[++top] = u * 2, o[++top] = v * 2 + 1;
a[i] = u * 2, b[i] = v * 2 + 1;
}
sort(o + 1, o + top + 1);
top = unique(o + 1, o + top + 1) - o - 1;
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(o + 1, o + top + 1, a[i]) - o;
b[i] = lower_bound(o + 1, o + top + 1, b[i]) - o;
(nn[++cnt][1] = b[i], nn[cnt][0] = head[a[i]], head[a[i]] = cnt,
bl[cnt] = i),
(nn[++cnt][1] = a[i], nn[cnt][0] = head[b[i]], head[b[i]] = cnt,
bl[cnt] = i),
++d[a[i]], ++d[b[i]];
}
Id = n;
for (int i = 1; i <= top; ++i)
if (d[i] & 1) o[++tot] = i;
for (int i = 2; i <= tot; i += 2)
++Id,
(nn[++cnt][1] = o[i - 1], nn[cnt][0] = head[o[i]], head[o[i]] = cnt,
bl[cnt] = Id),
(nn[++cnt][1] = o[i], nn[cnt][0] = head[o[i - 1]], head[o[i - 1]] = cnt,
bl[cnt] = Id);
for (int i = 1; i <= top; ++i)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", w[i]);
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 400050;
int a[N], b[N], w[N], Id, d[N], nn[N][2], o[N], head[N], top, tot, bl[N], cnt,
vis[N], st[N], bj[N];
void dfs(int x) {
vis[x] = 1;
for (int i = head[x], y; i; i = nn[i][0])
if (!bj[bl[i]]) {
bj[bl[i]] = 1, dfs(y = nn[i][1]);
if (x < y)
w[bl[i]] = 0;
else
w[bl[i]] = 1;
}
return;
}
int main() {
int n, u, v;
cin >> n;
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &u, &v), o[++top] = u * 2, o[++top] = v * 2 + 1;
a[i] = u * 2, b[i] = v * 2 + 1;
}
sort(o + 1, o + top + 1);
top = unique(o + 1, o + top + 1) - o - 1;
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(o + 1, o + top + 1, a[i]) - o;
b[i] = lower_bound(o + 1, o + top + 1, b[i]) - o;
(nn[++cnt][1] = b[i], nn[cnt][0] = head[a[i]], head[a[i]] = cnt,
bl[cnt] = i),
(nn[++cnt][1] = a[i], nn[cnt][0] = head[b[i]], head[b[i]] = cnt,
bl[cnt] = i),
++d[a[i]], ++d[b[i]];
}
Id = n;
for (int i = 1; i <= top; ++i)
if (d[i] & 1) o[++tot] = i;
for (int i = 2; i <= tot; i += 2)
++Id,
(nn[++cnt][1] = o[i - 1], nn[cnt][0] = head[o[i]], head[o[i]] = cnt,
bl[cnt] = Id),
(nn[++cnt][1] = o[i], nn[cnt][0] = head[o[i - 1]], head[o[i - 1]] = cnt,
bl[cnt] = Id);
for (int i = 1; i <= top; ++i)
if (!vis[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", w[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, pair<int, int> > > v;
vector<pair<int, int> > e[100005];
int n, c[100005];
void dfs(int v, int n) {
c[v] = n;
for (int i = 0; i < e[v].size(); i++) {
if (c[e[v][i].first] == -1) {
dfs(e[v][i].first, (n ^ e[v][i].second));
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
v.push_back(make_pair(a, make_pair(0, i)));
v.push_back(make_pair(b, make_pair(1, i)));
}
sort(v.begin(), v.end());
for (int i = 0; i < 2 * n; i += 2) {
int x = (v[i].second.first == v[i + 1].second.first);
e[v[i].second.second].push_back(make_pair(v[i + 1].second.second, x));
e[v[i + 1].second.second].push_back(make_pair(v[i].second.second, x));
}
memset(c, -1, sizeof(c));
for (int i = 0; i < n; i++) {
if (c[i] != -1) continue;
dfs(i, 0);
}
for (int i = 0; i < n; i++) printf("%d ", c[i]);
puts("");
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, pair<int, int> > > v;
vector<pair<int, int> > e[100005];
int n, c[100005];
void dfs(int v, int n) {
c[v] = n;
for (int i = 0; i < e[v].size(); i++) {
if (c[e[v][i].first] == -1) {
dfs(e[v][i].first, (n ^ e[v][i].second));
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
v.push_back(make_pair(a, make_pair(0, i)));
v.push_back(make_pair(b, make_pair(1, i)));
}
sort(v.begin(), v.end());
for (int i = 0; i < 2 * n; i += 2) {
int x = (v[i].second.first == v[i + 1].second.first);
e[v[i].second.second].push_back(make_pair(v[i + 1].second.second, x));
e[v[i + 1].second.second].push_back(make_pair(v[i].second.second, x));
}
memset(c, -1, sizeof(c));
for (int i = 0; i < n; i++) {
if (c[i] != -1) continue;
dfs(i, 0);
}
for (int i = 0; i < n; i++) printf("%d ", c[i]);
puts("");
}
```
|
#include <bits/stdc++.h>
using namespace std;
int read() {
char ch = getchar();
int x = 0, fl = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch - '0');
return x * fl;
}
const int N = 400005;
int n, l[N], r[N], num[N], len, de[N], cur[N], pv[N];
vector<pair<int, int> > adj[N];
void dfs(int x) {
while (cur[x] < adj[x].size()) {
pair<int, int> e = adj[x][cur[x]];
if (pv[e.first] < 0) {
pv[e.first] = x;
dfs(e.second);
}
cur[x]++;
}
}
int main() {
memset(pv, -1, sizeof(pv));
n = read();
for (int i = 1; i <= n; i++) {
l[i] = read();
r[i] = read() + 1;
num[++len] = l[i];
num[++len] = r[i];
}
sort(num + 1, num + len + 1);
len = unique(num + 1, num + len + 1) - num - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(num + 1, num + len + 1, l[i]) - num;
r[i] = lower_bound(num + 1, num + len + 1, r[i]) - num;
de[l[i]] ^= 1;
de[r[i]] ^= 1;
adj[l[i]].push_back(make_pair(i, r[i]));
adj[r[i]].push_back(make_pair(i, l[i]));
}
int r = n, lst = 0;
for (int i = 1; i <= len; i++) {
if (de[i]) {
if (lst) {
adj[i].push_back(make_pair(++r, lst));
adj[lst].push_back(make_pair(r, i));
lst = 0;
} else
lst = i;
}
}
for (int i = 0; i <= len; i++) dfs(i);
for (int i = 1; i <= n; i++) cout << (pv[i] == l[i]) << ' ';
cout << '\n';
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int read() {
char ch = getchar();
int x = 0, fl = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch - '0');
return x * fl;
}
const int N = 400005;
int n, l[N], r[N], num[N], len, de[N], cur[N], pv[N];
vector<pair<int, int> > adj[N];
void dfs(int x) {
while (cur[x] < adj[x].size()) {
pair<int, int> e = adj[x][cur[x]];
if (pv[e.first] < 0) {
pv[e.first] = x;
dfs(e.second);
}
cur[x]++;
}
}
int main() {
memset(pv, -1, sizeof(pv));
n = read();
for (int i = 1; i <= n; i++) {
l[i] = read();
r[i] = read() + 1;
num[++len] = l[i];
num[++len] = r[i];
}
sort(num + 1, num + len + 1);
len = unique(num + 1, num + len + 1) - num - 1;
for (int i = 1; i <= n; i++) {
l[i] = lower_bound(num + 1, num + len + 1, l[i]) - num;
r[i] = lower_bound(num + 1, num + len + 1, r[i]) - num;
de[l[i]] ^= 1;
de[r[i]] ^= 1;
adj[l[i]].push_back(make_pair(i, r[i]));
adj[r[i]].push_back(make_pair(i, l[i]));
}
int r = n, lst = 0;
for (int i = 1; i <= len; i++) {
if (de[i]) {
if (lst) {
adj[i].push_back(make_pair(++r, lst));
adj[lst].push_back(make_pair(r, i));
lst = 0;
} else
lst = i;
}
}
for (int i = 0; i <= len; i++) dfs(i);
for (int i = 1; i <= n; i++) cout << (pv[i] == l[i]) << ' ';
cout << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
int f, qr;
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char x) {
*oS++ = x;
if (oS == oT) flush();
}
template <class I>
inline void gi(I &x) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-') f = -1;
for (x = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
template <class I>
inline void get(I &x) {
for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < 'A' || c > 'Z';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
;
x = c;
}
inline void read(char *s) {
for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < 'A' || c > 'Z';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
;
for (; c >= 'A' && c <= 'Z';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
*++s = c;
*++s = '\0';
}
template <class I>
inline void print(I x) {
if (!x) putc('0');
if (x < 0) putc('-'), x = -x;
while (x) qu[++qr] = x % 10 + '0', x /= 10;
while (qr) putc(qu[qr--]);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::get;
using io ::gi;
using io ::print;
using io ::putc;
using io ::read;
const int N = 1e5 + 5, M = N * 6;
int pl[N], pr[N], a[N << 1], cnt[N << 1];
int tot = 1, head[M], nxt[M], adj[M], vis[M], col[M];
inline void addedge(register int x, register int y) {
nxt[++tot] = head[x];
adj[head[x] = tot] = y;
}
inline void dfs(register int x) {
register int i;
while (i = head[x])
if (!vis[i])
vis[i] = vis[i ^ 1] = 1, dfs(adj[i]), col[i / 2] = i & 1;
else
head[x] = nxt[i];
}
int main() {
register int n, m, i;
gi(n);
m = 0;
for (i = 1; i <= n; ++i)
gi(pl[i]), gi(pr[i]), ++pr[i], a[++m] = pl[i], a[++m] = pr[i];
sort(a + 1, a + 1 + m);
m = unique(a + 1, a + 1 + m) - a - 1;
for (i = 1; i <= n; ++i)
pl[i] = lower_bound(a + 1, a + 1 + m, pl[i]) - a,
pr[i] = lower_bound(a + 1, a + 1 + m, pr[i]) - a, addedge(pl[i], pr[i]),
addedge(pr[i], pl[i]), ++cnt[pl[i]], --cnt[pr[i]];
for (i = 1; i <= m; ++i) {
cnt[i] += cnt[i - 1];
if (cnt[i] & 1) addedge(i, i + 1), addedge(i + 1, i);
}
for (i = 1; i <= m; ++i) dfs(i);
for (i = 1; i <= n; ++i) putc(col[i] ? '0' : '1'), putc(' ');
putc('\n');
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
int f, qr;
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char x) {
*oS++ = x;
if (oS == oT) flush();
}
template <class I>
inline void gi(I &x) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-') f = -1;
for (x = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
template <class I>
inline void get(I &x) {
for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < 'A' || c > 'Z';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
;
x = c;
}
inline void read(char *s) {
for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < 'A' || c > 'Z';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
;
for (; c >= 'A' && c <= 'Z';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
*++s = c;
*++s = '\0';
}
template <class I>
inline void print(I x) {
if (!x) putc('0');
if (x < 0) putc('-'), x = -x;
while (x) qu[++qr] = x % 10 + '0', x /= 10;
while (qr) putc(qu[qr--]);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::get;
using io ::gi;
using io ::print;
using io ::putc;
using io ::read;
const int N = 1e5 + 5, M = N * 6;
int pl[N], pr[N], a[N << 1], cnt[N << 1];
int tot = 1, head[M], nxt[M], adj[M], vis[M], col[M];
inline void addedge(register int x, register int y) {
nxt[++tot] = head[x];
adj[head[x] = tot] = y;
}
inline void dfs(register int x) {
register int i;
while (i = head[x])
if (!vis[i])
vis[i] = vis[i ^ 1] = 1, dfs(adj[i]), col[i / 2] = i & 1;
else
head[x] = nxt[i];
}
int main() {
register int n, m, i;
gi(n);
m = 0;
for (i = 1; i <= n; ++i)
gi(pl[i]), gi(pr[i]), ++pr[i], a[++m] = pl[i], a[++m] = pr[i];
sort(a + 1, a + 1 + m);
m = unique(a + 1, a + 1 + m) - a - 1;
for (i = 1; i <= n; ++i)
pl[i] = lower_bound(a + 1, a + 1 + m, pl[i]) - a,
pr[i] = lower_bound(a + 1, a + 1 + m, pr[i]) - a, addedge(pl[i], pr[i]),
addedge(pr[i], pl[i]), ++cnt[pl[i]], --cnt[pr[i]];
for (i = 1; i <= m; ++i) {
cnt[i] += cnt[i - 1];
if (cnt[i] & 1) addedge(i, i + 1), addedge(i + 1, i);
}
for (i = 1; i <= m; ++i) dfs(i);
for (i = 1; i <= n; ++i) putc(col[i] ? '0' : '1'), putc(' ');
putc('\n');
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int tt = 1;
bool w[N], c[N];
int head[N], to[N], nxt[N], in[N], a[N], b[N], v[N];
inline int gi() {
int x = 0, o = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
inline void lnk(int x, int y) {
++in[x], ++in[y];
to[++tt] = y, nxt[tt] = head[x], head[x] = tt;
to[++tt] = x, nxt[tt] = head[y], head[y] = tt;
}
inline void dfs(int x) {
for (int &i = head[x]; i; i = nxt[i])
if (!w[i]) {
w[i] = w[i ^ 1] = 1, c[i] = 1;
dfs(to[i]);
}
}
int main() {
int n, tp = 0;
cin >> n;
for (int i = 1; i <= n; i++) a[i] = v[++tp] = gi(), b[i] = v[++tp] = gi() + 1;
sort(v + 1, v + 1 + tp);
tp = unique(v + 1, v + 1 + tp) - v - 1;
for (int i = 1; i <= n; i++) {
int x, y;
x = lower_bound(v + 1, v + 1 + tp, a[i]) - v;
y = lower_bound(v + 1, v + 1 + tp, b[i]) - v;
lnk(x, y);
}
for (int i = 1, x = 0; i <= tp; i++)
if (in[i] & 1) {
if (x)
lnk(x, i), x = 0;
else
x = i;
}
for (int i = 1; i <= tp; i++)
if (head[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", c[i << 1]);
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int tt = 1;
bool w[N], c[N];
int head[N], to[N], nxt[N], in[N], a[N], b[N], v[N];
inline int gi() {
int x = 0, o = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
inline void lnk(int x, int y) {
++in[x], ++in[y];
to[++tt] = y, nxt[tt] = head[x], head[x] = tt;
to[++tt] = x, nxt[tt] = head[y], head[y] = tt;
}
inline void dfs(int x) {
for (int &i = head[x]; i; i = nxt[i])
if (!w[i]) {
w[i] = w[i ^ 1] = 1, c[i] = 1;
dfs(to[i]);
}
}
int main() {
int n, tp = 0;
cin >> n;
for (int i = 1; i <= n; i++) a[i] = v[++tp] = gi(), b[i] = v[++tp] = gi() + 1;
sort(v + 1, v + 1 + tp);
tp = unique(v + 1, v + 1 + tp) - v - 1;
for (int i = 1; i <= n; i++) {
int x, y;
x = lower_bound(v + 1, v + 1 + tp, a[i]) - v;
y = lower_bound(v + 1, v + 1 + tp, b[i]) - v;
lnk(x, y);
}
for (int i = 1, x = 0; i <= tp; i++)
if (in[i] & 1) {
if (x)
lnk(x, i), x = 0;
else
x = i;
}
for (int i = 1; i <= tp; i++)
if (head[i]) dfs(i);
for (int i = 1; i <= n; i++) printf("%d ", c[i << 1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
inline long long rd() {
long long x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * w;
}
int to[N << 2], nt[N << 2], hd[N << 1], dg[N << 1], tot = 1;
void add(int x, int y) {
++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot, ++dg[x];
++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot, ++dg[y];
}
bool cs[N << 2], ban[N << 2], v[N << 1];
int n, m, a[N][2], b[N << 1];
void dfs(int x) {
v[x] = 1;
for (int &i = hd[x]; i; i = nt[i]) {
if (ban[i]) continue;
int y = to[i];
ban[i] = ban[i ^ 1] = 1, cs[i] = 1;
dfs(y);
}
}
int main() {
n = rd();
for (int i = 1; i <= n; ++i)
a[i][0] = b[i * 2 - 1] = rd(), a[i][1] = b[i * 2] = rd() + 1;
sort(b + 1, b + n + n + 1), m = unique(b + 1, b + n + n + 1) - b - 1;
for (int i = 1; i <= n; ++i) {
a[i][0] = lower_bound(b + 1, b + m + 1, a[i][0]) - b;
a[i][1] = lower_bound(b + 1, b + m + 1, a[i][1]) - b;
add(a[i][0], a[i][1]);
}
for (int i = 1, la = 0; i <= m; ++i)
if (dg[i] & 1) {
if (!la)
la = i;
else
add(la, i), la = 0;
}
for (int i = 1; i <= m; ++i)
if (!v[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", cs[i << 1]);
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
inline long long rd() {
long long x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * w;
}
int to[N << 2], nt[N << 2], hd[N << 1], dg[N << 1], tot = 1;
void add(int x, int y) {
++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot, ++dg[x];
++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot, ++dg[y];
}
bool cs[N << 2], ban[N << 2], v[N << 1];
int n, m, a[N][2], b[N << 1];
void dfs(int x) {
v[x] = 1;
for (int &i = hd[x]; i; i = nt[i]) {
if (ban[i]) continue;
int y = to[i];
ban[i] = ban[i ^ 1] = 1, cs[i] = 1;
dfs(y);
}
}
int main() {
n = rd();
for (int i = 1; i <= n; ++i)
a[i][0] = b[i * 2 - 1] = rd(), a[i][1] = b[i * 2] = rd() + 1;
sort(b + 1, b + n + n + 1), m = unique(b + 1, b + n + n + 1) - b - 1;
for (int i = 1; i <= n; ++i) {
a[i][0] = lower_bound(b + 1, b + m + 1, a[i][0]) - b;
a[i][1] = lower_bound(b + 1, b + m + 1, a[i][1]) - b;
add(a[i][0], a[i][1]);
}
for (int i = 1, la = 0; i <= m; ++i)
if (dg[i] & 1) {
if (!la)
la = i;
else
add(la, i), la = 0;
}
for (int i = 1; i <= m; ++i)
if (!v[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", cs[i << 1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mx = 2e5 + 10;
set<pair<long long, long long> > ad[mx];
vector<long long> ver;
long long ans[mx], l[mx], r[mx];
void dfst(long long v) {
while ((long long)ad[v].size()) {
long long u = ad[v].begin()->first, yal = ad[v].begin()->second;
ad[v].erase(ad[v].begin());
ans[yal] = v < u;
ad[u].erase({v, yal});
dfst(u);
}
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> l[i] >> r[i];
r[i]++;
ver.push_back(l[i]);
ver.push_back(r[i]);
}
sort(ver.begin(), ver.end());
ver.resize(unique(ver.begin(), ver.end()) - ver.begin());
for (long long i = 0; i < n; i++) {
l[i] = lower_bound(ver.begin(), ver.end(), l[i]) - ver.begin();
r[i] = lower_bound(ver.begin(), ver.end(), r[i]) - ver.begin();
ad[l[i]].insert({r[i], i});
ad[r[i]].insert({l[i], i});
}
long long fard = -1;
for (long long i = 0; i < (long long)ver.size(); i++)
if ((long long)ad[i].size() % 2) {
if (fard != -1) {
ad[i].insert({fard, n});
ad[fard].insert({i, n});
fard = -1;
} else
fard = i;
}
for (long long i = 0; i < (long long)ver.size(); i++) dfst(i);
for (long long i = 0; i < n; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mx = 2e5 + 10;
set<pair<long long, long long> > ad[mx];
vector<long long> ver;
long long ans[mx], l[mx], r[mx];
void dfst(long long v) {
while ((long long)ad[v].size()) {
long long u = ad[v].begin()->first, yal = ad[v].begin()->second;
ad[v].erase(ad[v].begin());
ans[yal] = v < u;
ad[u].erase({v, yal});
dfst(u);
}
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> l[i] >> r[i];
r[i]++;
ver.push_back(l[i]);
ver.push_back(r[i]);
}
sort(ver.begin(), ver.end());
ver.resize(unique(ver.begin(), ver.end()) - ver.begin());
for (long long i = 0; i < n; i++) {
l[i] = lower_bound(ver.begin(), ver.end(), l[i]) - ver.begin();
r[i] = lower_bound(ver.begin(), ver.end(), r[i]) - ver.begin();
ad[l[i]].insert({r[i], i});
ad[r[i]].insert({l[i], i});
}
long long fard = -1;
for (long long i = 0; i < (long long)ver.size(); i++)
if ((long long)ad[i].size() % 2) {
if (fard != -1) {
ad[i].insert({fard, n});
ad[fard].insert({i, n});
fard = -1;
} else
fard = i;
}
for (long long i = 0; i < (long long)ver.size(); i++) dfst(i);
for (long long i = 0; i < n; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
inline long long rd() {
long long x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * w;
}
int to[N << 2], nt[N << 2], hd[N << 1], dg[N << 1], tot = 1;
void add(int x, int y) {
++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot, ++dg[x];
++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot, ++dg[y];
}
bool cs[N << 2], ban[N << 2], v[N << 1];
int n, m, a[N][2], b[N << 1];
void dfs(int x) {
v[x] = 1;
for (int &i = hd[x]; i; i = nt[i]) {
if (ban[i]) continue;
int y = to[i];
ban[i] = ban[i ^ 1] = 1, cs[i] = 1;
dfs(y);
}
}
int main() {
n = rd();
for (int i = 1; i <= n; ++i)
a[i][0] = b[i * 2 - 1] = rd(), a[i][1] = b[i * 2] = rd() + 1;
sort(b + 1, b + n + n + 1), m = unique(b + 1, b + n + n + 1) - b - 1;
for (int i = 1; i <= n; ++i) {
a[i][0] = lower_bound(b + 1, b + m + 1, a[i][0]) - b;
a[i][1] = lower_bound(b + 1, b + m + 1, a[i][1]) - b;
add(a[i][0], a[i][1]);
}
for (int i = 1, la = 0; i <= m; ++i)
if (dg[i] & 1) {
if (!la)
la = i;
else
add(la, i), la = 0;
}
for (int i = 1; i <= m; ++i)
if (!v[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", cs[i << 1]);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
inline long long rd() {
long long x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * w;
}
int to[N << 2], nt[N << 2], hd[N << 1], dg[N << 1], tot = 1;
void add(int x, int y) {
++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot, ++dg[x];
++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot, ++dg[y];
}
bool cs[N << 2], ban[N << 2], v[N << 1];
int n, m, a[N][2], b[N << 1];
void dfs(int x) {
v[x] = 1;
for (int &i = hd[x]; i; i = nt[i]) {
if (ban[i]) continue;
int y = to[i];
ban[i] = ban[i ^ 1] = 1, cs[i] = 1;
dfs(y);
}
}
int main() {
n = rd();
for (int i = 1; i <= n; ++i)
a[i][0] = b[i * 2 - 1] = rd(), a[i][1] = b[i * 2] = rd() + 1;
sort(b + 1, b + n + n + 1), m = unique(b + 1, b + n + n + 1) - b - 1;
for (int i = 1; i <= n; ++i) {
a[i][0] = lower_bound(b + 1, b + m + 1, a[i][0]) - b;
a[i][1] = lower_bound(b + 1, b + m + 1, a[i][1]) - b;
add(a[i][0], a[i][1]);
}
for (int i = 1, la = 0; i <= m; ++i)
if (dg[i] & 1) {
if (!la)
la = i;
else
add(la, i), la = 0;
}
for (int i = 1; i <= m; ++i)
if (!v[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", cs[i << 1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
inline int gi() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
int n, l[maxn], r[maxn], ans[maxn], *q[maxn], num, cnt;
struct edge {
int to, next, Id;
} e[maxn * 2];
int h[maxn], vis[maxn], deg[maxn], tot = 1;
inline void add(int u, int v, int Id) {
e[++tot] = (edge){v, h[u], Id};
h[u] = tot;
e[++tot] = (edge){u, h[v], Id};
h[v] = tot;
}
void dfs(int u) {
for (int &i = h[u]; i; i = e[i].next)
if (!vis[i >> 1]) vis[i >> 1] = 1, ans[e[i].Id] = i & 1, dfs(e[i].to);
}
int main() {
n = gi();
for (int i = 1; i <= n; ++i)
l[i] = gi(), r[i] = gi() + 1, q[++cnt] = &l[i], q[++cnt] = &r[i];
sort(q + 1, q + cnt + 1, [](int *a, int *b) { return *a < *b; });
num = 0;
for (int k = -1, i = 1; i <= cnt; ++i)
if (*q[i] == k)
*q[i] = num;
else
k = *q[i], *q[i] = ++num;
for (int i = 1; i <= n; ++i)
deg[l[i]] ^= 1, deg[r[i]] ^= 1, add(l[i], r[i], i);
for (int lst = 0, i = 1; i <= num; ++i)
if (deg[i] & 1) {
if (lst)
add(lst, i, 0), lst = 0;
else
lst = i;
}
for (int i = 1; i <= num; ++i)
if (h[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
inline int gi() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
int n, l[maxn], r[maxn], ans[maxn], *q[maxn], num, cnt;
struct edge {
int to, next, Id;
} e[maxn * 2];
int h[maxn], vis[maxn], deg[maxn], tot = 1;
inline void add(int u, int v, int Id) {
e[++tot] = (edge){v, h[u], Id};
h[u] = tot;
e[++tot] = (edge){u, h[v], Id};
h[v] = tot;
}
void dfs(int u) {
for (int &i = h[u]; i; i = e[i].next)
if (!vis[i >> 1]) vis[i >> 1] = 1, ans[e[i].Id] = i & 1, dfs(e[i].to);
}
int main() {
n = gi();
for (int i = 1; i <= n; ++i)
l[i] = gi(), r[i] = gi() + 1, q[++cnt] = &l[i], q[++cnt] = &r[i];
sort(q + 1, q + cnt + 1, [](int *a, int *b) { return *a < *b; });
num = 0;
for (int k = -1, i = 1; i <= cnt; ++i)
if (*q[i] == k)
*q[i] = num;
else
k = *q[i], *q[i] = ++num;
for (int i = 1; i <= n; ++i)
deg[l[i]] ^= 1, deg[r[i]] ^= 1, add(l[i], r[i], i);
for (int lst = 0, i = 1; i <= num; ++i)
if (deg[i] & 1) {
if (lst)
add(lst, i, 0), lst = 0;
else
lst = i;
}
for (int i = 1; i <= num; ++i)
if (h[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int _ = 1e5 + 5;
inline int read() {
char ch = '!';
int z = 1, num = 0;
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') z = -1, ch = getchar();
while (ch <= '9' && ch >= '0')
num = (num << 3) + (num << 1) + ch - '0', ch = getchar();
return z * num;
}
int n, l[_], r[_], o[_ << 1], m, ans[_];
struct ed {
int to, next, id;
} e[_ << 2];
int cnt = 1, head[_ << 1], d[_ << 1];
bool vis[_ << 2];
void link(int u, int v, int id) {
d[u]++, d[v]++;
e[++cnt] = (ed){v, head[u], id};
head[u] = cnt;
e[++cnt] = (ed){u, head[v], id};
head[v] = cnt;
}
void dfs(int u) {
for (int i = head[u]; i; i = e[i].next)
if (!vis[i]) {
ans[e[i].id] = (u < e[i].to);
vis[i] = vis[i ^ 1] = 1;
dfs(e[i].to);
}
}
int main() {
n = read();
for (int i = 1; i <= n; ++i)
l[i] = read(), r[i] = read() + 1, o[++m] = l[i], o[++m] = r[i];
sort(o + 1, o + 1 + m);
m = unique(o + 1, o + 1 + m) - o - 1;
for (int i = 1; i <= n; ++i)
l[i] = lower_bound(o + 1, o + 1 + m, l[i]) - o,
r[i] = lower_bound(o + 1, o + 1 + m, r[i]) - o;
for (int i = 1; i <= n; ++i) link(l[i], r[i], i);
int lst = 0;
for (int i = 1; i <= m; ++i)
if (d[i] & 1) {
if (lst)
link(lst, i, 0), lst = 0;
else
lst = i;
}
if (lst) return puts("-1"), 0;
for (int i = 1; i <= m; ++i)
if (d[i]) dfs(i);
for (int i = 1; i <= n; ++i)
if (!ans[i])
printf("0 ");
else
printf("1 ");
puts("");
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int _ = 1e5 + 5;
inline int read() {
char ch = '!';
int z = 1, num = 0;
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') z = -1, ch = getchar();
while (ch <= '9' && ch >= '0')
num = (num << 3) + (num << 1) + ch - '0', ch = getchar();
return z * num;
}
int n, l[_], r[_], o[_ << 1], m, ans[_];
struct ed {
int to, next, id;
} e[_ << 2];
int cnt = 1, head[_ << 1], d[_ << 1];
bool vis[_ << 2];
void link(int u, int v, int id) {
d[u]++, d[v]++;
e[++cnt] = (ed){v, head[u], id};
head[u] = cnt;
e[++cnt] = (ed){u, head[v], id};
head[v] = cnt;
}
void dfs(int u) {
for (int i = head[u]; i; i = e[i].next)
if (!vis[i]) {
ans[e[i].id] = (u < e[i].to);
vis[i] = vis[i ^ 1] = 1;
dfs(e[i].to);
}
}
int main() {
n = read();
for (int i = 1; i <= n; ++i)
l[i] = read(), r[i] = read() + 1, o[++m] = l[i], o[++m] = r[i];
sort(o + 1, o + 1 + m);
m = unique(o + 1, o + 1 + m) - o - 1;
for (int i = 1; i <= n; ++i)
l[i] = lower_bound(o + 1, o + 1 + m, l[i]) - o,
r[i] = lower_bound(o + 1, o + 1 + m, r[i]) - o;
for (int i = 1; i <= n; ++i) link(l[i], r[i], i);
int lst = 0;
for (int i = 1; i <= m; ++i)
if (d[i] & 1) {
if (lst)
link(lst, i, 0), lst = 0;
else
lst = i;
}
if (lst) return puts("-1"), 0;
for (int i = 1; i <= m; ++i)
if (d[i]) dfs(i);
for (int i = 1; i <= n; ++i)
if (!ans[i])
printf("0 ");
else
printf("1 ");
puts("");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int n;
int l[maxn];
int r[maxn];
int clr[maxn];
bool mark[maxn];
vector<int> cmp;
map<int, int> compress;
stack<pair<int, int>> adj[maxn];
void in();
void solve();
void dfs(int u);
void out();
int main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
in();
solve();
out();
}
void in() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
r[i]++;
cmp.push_back(l[i]);
cmp.push_back(r[i]);
}
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
for (int i = 0; i < cmp.size(); i++) compress[cmp[i]] = i;
for (int i = 0; i < n; i++) {
l[i] = compress[l[i]];
r[i] = compress[r[i]];
adj[l[i]].push({i, r[i]});
adj[r[i]].push({i, l[i]});
}
}
void solve() {
int last = -1;
for (int i = 0; i < maxn; i++) {
if (adj[i].size() % 2) {
if (last == -1)
last = i;
else {
adj[i].push({maxn - i, last});
adj[last].push({maxn - i, i});
last = -1;
}
}
}
for (int i = 0; i < maxn; i++)
while (adj[i].size()) dfs(i);
}
void dfs(int u) {
if (adj[u].size()) {
auto x = adj[u].top();
adj[u].pop();
if (mark[x.first]) dfs(u);
mark[x.first] = 1;
clr[x.first] = x.second > u;
dfs(x.second);
}
}
void out() {
for (int i = 0; i < n; i++) cout << clr[i] << ' ';
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| β€ 1 must be satisfied.
A segment [l, r] contains a point x if and only if l β€ x β€ r.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him.
Input
The first line of input contains integer n (1 β€ n β€ 105) β the number of segments. The i-th of the next n lines contains two integers li and ri (0 β€ li β€ ri β€ 109) β the borders of the i-th segment.
It's guaranteed that all the segments are distinct.
Output
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue).
If there are multiple good drawings you can output any of them.
Examples
Input
2
0 2
2 3
Output
0 1
Input
6
1 5
1 3
3 5
2 10
11 11
12 12
Output
0 1 0 1 0 0
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int n;
int l[maxn];
int r[maxn];
int clr[maxn];
bool mark[maxn];
vector<int> cmp;
map<int, int> compress;
stack<pair<int, int>> adj[maxn];
void in();
void solve();
void dfs(int u);
void out();
int main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
in();
solve();
out();
}
void in() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
r[i]++;
cmp.push_back(l[i]);
cmp.push_back(r[i]);
}
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
for (int i = 0; i < cmp.size(); i++) compress[cmp[i]] = i;
for (int i = 0; i < n; i++) {
l[i] = compress[l[i]];
r[i] = compress[r[i]];
adj[l[i]].push({i, r[i]});
adj[r[i]].push({i, l[i]});
}
}
void solve() {
int last = -1;
for (int i = 0; i < maxn; i++) {
if (adj[i].size() % 2) {
if (last == -1)
last = i;
else {
adj[i].push({maxn - i, last});
adj[last].push({maxn - i, i});
last = -1;
}
}
}
for (int i = 0; i < maxn; i++)
while (adj[i].size()) dfs(i);
}
void dfs(int u) {
if (adj[u].size()) {
auto x = adj[u].top();
adj[u].pop();
if (mark[x.first]) dfs(u);
mark[x.first] = 1;
clr[x.first] = x.second > u;
dfs(x.second);
}
}
void out() {
for (int i = 0; i < n; i++) cout << clr[i] << ' ';
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.