Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Collections;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Alex
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
int[] a;
int m;
long[][] mem;
int[] powers10;
long DONE;
long[] dp(int used) {
if (mem[used] != null) return mem[used];
if (used == DONE) {
long[] res = new long[m];
res[0] = 1;
return res;
}
int index = Integer.bitCount(used);
long[] res = new long[m];
for (int pos = 0; pos < a.length; pos++) {
if (index == 0 && a[pos] == 0) continue;
if (((used >> pos) & 1) != 0) continue;
long[] get = dp(used | (1 << pos));
int curval = powers10[a.length - index - 1];
for (int mod = 0; mod < get.length; mod++) {
res[(curval * a[pos] + mod) % m] += get[mod];
}
}
return mem[used] = res;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
long n = in.readLong();
m = in.readInt();
a = toArray(n);
Arrays.sort(a);
DONE = (1 << a.length) - 1;
powers10 = new int[a.length];
powers10[0] = 1 % m;
for (int i = 1; i < powers10.length; i++) {
powers10[i] = powers10[i - 1] * 10;
powers10[i] %= m;
}
mem = new long[1 << a.length][];
long[] res = dp(0);
long divisor = 1;
int[] count = new int[10];
for (int val : a) count[val]++;
for (int c : count) divisor *= IntegerUtils.factorial(c);
out.printLine(res[0] / divisor);
}
private int[] toArray(long n) {
ArrayList<Integer> al = new ArrayList<>();
while (n > 0) {
int last = (int) (n % 10);
al.add(last);
n /= 10;
}
Collections.reverse(al);
int[] res = new int[al.size()];
for (int i = 0; i < res.length; i++) res[i] = al.get(i);
return res;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class IntegerUtils {
public static long factorial(int n) {
long result = 1;
for (int i = 2; i <= n; i++)
result *= i;
return result;
}
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRomanAndNumbers solver = new DRomanAndNumbers();
solver.solve(1, in, out);
out.close();
}
static class DRomanAndNumbers {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
char[] s = in.scanString().toCharArray();
int m = in.scanInt();
int n = s.length;
int[] si = new int[n];
for (int i = 0; i < n; i++) si[i] = s[i] - '0';
Arrays.sort(si);
long[][] dp = new long[1 << n][m];
dp[0][0] = 1;
for (int i = 0; i < 1 << n; i++) {
for (int j = 0; j < m; j++) {
if (dp[i][j] == 0) continue;
for (int k = 0; k < n; k++) {
if (i << 31 - k >= 0) {
if (i == 0 && si[k] == 0) continue;
if (k > 0 && si[k] == si[k - 1] && i << 31 - (k - 1) >= 0) continue;
dp[i | 1 << k][(j * 10 + si[k]) % m] += dp[i][j];
}
}
}
}
out.println(dp[(1 << n) - 1][0]);
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public String scanString() {
int c = scan();
if (c == -1) return null;
while (isWhiteSpace(c)) c = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
String n = in.next();
int m = in.nextInt();
byte[] nums = new byte[10];
for(char ch: n.toCharArray()) {
++nums[Character.digit(ch, 10)];
}
int total = 1;
for(byte num: nums) {
total *= num + 1;
}
long[][] dp = new long[total][m];
dp[0][0] = 1;
for(int i = 1; i < total; ++i) {
int d = 1;
int numCount = 0;
for(int k = 0; k < 10; ++k) {
if(nums[k] != 0) {
numCount += i / d % (nums[k] + 1);
d *= nums[k] + 1;
}
}
long level = Fun.power(10, numCount - 1);
for(int j = 0; j < m; ++j) {
d = 1;
for(int k = 0; k < 10; ++k) {
if(nums[k] != 0) {
int count = i / d % (nums[k] + 1);
if(count != 0) {
int currem = (int) (k * level % m);
int level2 = (d * (nums[k] + 1));
dp[i][j] += dp[i / level2 * level2 + (count - 1) * d + i % d][(j + m - currem) % m];
}
d *= nums[k] + 1;
}
}
}
}
if(nums[0] == 0) {
System.out.println(dp[total - 1][0]);
} else {
System.out.println(dp[total - 1][0] - dp[total - 1 - 1][0]);
}
}
}
// ///////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////
class Fun {
public static long power(long n, long e) {
long result = 1;
for(long i = 0; i < e; ++i) {
result *= n;
}
return result;
}
public static int[] nextInts(Scanner in, int count) {
int[] result = new int[count];
for (int i = 0; i < count; ++i) {
result[i] = in.nextInt();
}
return result;
}
public static boolean isPrime(int n) {
boolean result = true;
for(int i = 2; i * i <= n; ++i) {
if(n % i == 0) {
result = false;
break;
}
}
return result;
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
public static int[][] transpose(int[][] mat) {
int r = mat.length;
int c = mat[0].length;
int[][] result = new int[c][r];
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j)
result[i][j] = mat[j][i];
return result;
}
public static String repeatString(String s, long count) {
StringBuilder sb = new StringBuilder();
for (long i = 0; i < count; ++i) {
sb.append(s);
}
return sb.toString();
}
}
class Treap<K extends Comparable<? super K>, V> {
Treap() {
nullNode = new TreapNode<K, V>();
nullNode.left = nullNode.right = nullNode;
nullNode.priority = Integer.MAX_VALUE;
rootNode = nullNode;
}
public V put(K key, V value) {
PutResult<K, V> putResult = putHelp(rootNode, key, value);
rootNode = putResult.newNode;
return putResult.originalValue;
}
public V get(K key) {
TreapNode<K, V> currentNode = rootNode;
while(currentNode != nullNode && currentNode.key.compareTo(key) != 0) {
if(key.compareTo(currentNode.key) < 0) {
currentNode = currentNode.left;
} else {
currentNode = currentNode.right;
}
}
if(currentNode == nullNode) {
return null;
} else {
return currentNode.value;
}
}
public V remove(K key) {
V result = get(key);
if(result != null) {
rootNode = removeHelp(rootNode, key);
}
return result;
}
public K select(int rank) {
if(rank >= size() || rank < 0) {
return null;
}
TreapNode<K, V> currentNode = rootNode;
while(rank != currentNode.left.size) {
if(rank < currentNode.left.size) {
currentNode = currentNode.left;
} else {
rank -= currentNode.left.size + 1;
currentNode = currentNode.right;
}
}
return currentNode.key;
}
public int rank(K key) {
if(rootNode == nullNode) {
return -1;
}
int result = rootNode.left.size;
TreapNode<K, V> currentNode = rootNode;
while(currentNode != nullNode && currentNode.key.compareTo(key) != 0) {
if(key.compareTo(currentNode.key) < 0) {
result -= currentNode.left.right.size + 1;
currentNode = currentNode.left;
} else {
result += currentNode.right.left.size + 1;
currentNode = currentNode.right;
}
}
if(currentNode == nullNode) {
return -1;
} else {
return result;
}
}
public void clear() {
rootNode = nullNode;
}
public int size() {
return rootNode.size;
}
private static class TreapNode<K, V> {
public K key;
public V value;
public int size;
public int priority;
TreapNode<K, V> left, right;
}
private static class PutResult<K, V> {
public TreapNode<K, V> newNode;
public V originalValue;
}
private PutResult<K, V> putHelp(TreapNode<K, V> node, K key, V value) {
if(node == nullNode) {
PutResult<K, V> result = new PutResult<K, V>();
result.newNode = new TreapNode<K, V>();
result.newNode.key = key;
result.newNode.value = value;
result.newNode.size = 1;
result.newNode.priority = randomPriority.nextInt(Integer.MAX_VALUE);
result.newNode.left = result.newNode.right = nullNode;
return result;
} else if(key.compareTo(node.key) == 0) {
PutResult<K, V> result = new PutResult<K, V>();
result.originalValue = node.value;
node.value = value;
result.newNode = node;
return result;
} else if(key.compareTo(node.key) < 0) {
PutResult<K, V> result = putHelp(node.left, key, value);
node.left = result.newNode;
if(result.originalValue == null) {
++node.size;
if(node.left.priority < node.priority) {
node = rightRotate(node);
}
}
result.newNode = node;
return result;
} else {
PutResult<K, V> result = putHelp(node.right, key, value);
node.right = result.newNode;
if(result.originalValue == null) {
++node.size;
if(node.right.priority < node.priority) {
node = leftRotate(node);
}
}
result.newNode = node;
return result;
}
}
private TreapNode<K, V> removeHelp(TreapNode<K, V> node, K key) {
if(key.compareTo(node.key) == 0) {
node = removeHelp2(node);
} else if(key.compareTo(node.key) < 0) {
--node.size;
node.left = removeHelp(node.left, key);
} else {
--node.size;
node.right = removeHelp(node.right, key);
}
return node;
}
private TreapNode<K, V> removeHelp2(TreapNode<K, V> node) {
if(node.left == nullNode && node.right == nullNode) {
return nullNode;
}
if(node.left.priority < node.right.priority) {
node = rightRotate(node);
--node.size;
node.right = removeHelp2(node.right);
} else {
node = leftRotate(node);
--node.size;
node.left = removeHelp2(node.left);
}
return node;
}
private TreapNode<K, V> leftRotate(TreapNode<K, V> node) {
TreapNode<K, V> result = node.right;
node.right = result.left;
result.left = node;
result.size = node.size;
node.size -= 1 + result.right.size;
return result;
}
private TreapNode<K, V> rightRotate(TreapNode<K, V> node) {
TreapNode<K, V> result = node.left;
node.left = result.right;
result.right = node;
result.size = node.size;
node.size -= 1 + result.left.size;
return result;
}
private TreapNode<K, V> nullNode;
private TreapNode<K, V> rootNode;
private Random randomPriority = new Random();
} | JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 |
import java.io.*;
import java.util.*;
public class D {
static StringBuilder st = new StringBuilder();
static int n, m, a[];
static Long[][][][][][][][][][][][] memo;// idx , taken , mod , zero , one , two , three , four , five , six , seven , eight , nine
static int mult(int a , int b , int MOD) {return (int)((1l * a * b) % MOD) ; }
static int add(int a , int b , int MOD) {return (a+b) % MOD;}
static long dp(int idx, int mod, int[] cnt) {
if (idx == n && mod == 0)
return 1;
if (idx == n)
return 0;
if (check(idx, mod, cnt))
return get(idx, mod, cnt);
long ans = 0 ;
for (int i = 0; i < 10; i++) {
if (cnt[i] > 0) {
int[] newCnt = cnt.clone();
newCnt[i]--;
if (i == 0 && idx == 0) continue;
ans += dp(idx + 1, add(i, mult(10, mod, m), m), newCnt );
}
}
return set(idx, mod, cnt, ans);
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
char[] c = sc.next().toCharArray();
n = c.length;
m = sc.nextInt();
init(n, m, c);
int [] cnt = new int [10];
for(int x : a)
cnt[x]++;
out.println(dp(0, m, cnt));
out.flush();
out.close();
}
static class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
Scanner() {
}
Scanner(String path) throws Exception {
br = new BufferedReader(new FileReader(path));
}
String next() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(next());
}
long nextLong() throws Exception {
return Long.parseLong(next());
}
double nextDouble() throws Exception {
return Double.parseDouble(next());
}
}
static void shuffle(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
static void shuffle(char[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
char tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
static void shuffle(long[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
long tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.out.println(Arrays.deepToString(o));
}
static boolean check(int idx, int mod, int[] cnt) {
return (memo[idx][cnt[0]][cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][cnt[6]][cnt[7]][cnt[8]][cnt[9]][mod] != null);
}
static long get(int idx, int mod, int[] cnt) {
return memo[idx][cnt[0]][cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][cnt[6]][cnt[7]][cnt[8]][cnt[9]][mod];
}
static long set(int idx, int mod, int[] cnt, long val) {
return memo[idx][cnt[0]][cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][cnt[6]][cnt[7]][cnt[8]][cnt[9]][mod] = val;
}
static void init(int n, int m, char[] c) {
a = new int[n];
for (int i = 0; i < n; i++)
a[n - i - 1] = c[i] - '0';
int [] cnt = new int [10];
for(int x : a)
cnt[x]++;
memo = new Long[n][cnt[0]+1][cnt[1]+1][cnt[2]+1][cnt[3]+1][cnt[4]+1][cnt[5]+1][cnt[6]+1][cnt[7]+1][cnt[8]+1][cnt[9]+1][m+1];
}
} | JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRomanAndNumbers solver = new DRomanAndNumbers();
solver.solve(1, in, out);
out.close();
}
static class DRomanAndNumbers {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
char[] s = in.scanString().toCharArray();
int m = in.scanInt();
long[][] dp = new long[1 << s.length][m];
for (int i = 0; i < s.length; i++) {
if (s[i] != '0') dp[1 << i][(s[i] - '0') % m] = 1;
}
long[] fact = new long[20];
fact[0] = 1;
for (int i = 1; i < 20; i++) fact[i] = fact[i - 1] * i;
for (int i = 1; i < s.length; i++) {
for (int j = (1 << s.length) - 1; j >= 0; j--) {
for (int k = 0; k < m; k++) {
if (dp[j][k] != 0) {
for (int l = 0; l < s.length; l++) {
if ((j & (1 << l)) == 0) {
dp[j ^ (1 << l)][(k * 10 + (s[l] - '0')) % m] += dp[j][k];
}
}
dp[j][k] = 0;
}
}
}
}
long ans = dp[(1 << s.length) - 1][0];
int[] count = new int[10];
for (char c : s) count[c - '0']++;
for (int i : count) ans /= fact[i];
out.println(ans);
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public String scanString() {
int c = scan();
if (c == -1) return null;
while (isWhiteSpace(c)) c = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 |
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
String n1 = in.next();
String n2 = in.next();
int m = Integer.parseInt(n2);
int[] count = new int[10];
Arrays.fill(count, 0);
for (int i = 0; i < n1.length(); i++) {
count[n1.charAt(i) - '0']++;
}
long[] f = new long[19];
f[0] = 1;
for (int i = 1; i < 19; i++) {
f[i] = f[i - 1] * i;
}
int sh = 1 << n1.length();
long[][] dp = new long[sh][m];
for (int s = 0; s < sh; s++) {
Arrays.fill(dp[s], 0);
}
dp[0][0] = 1;
for (int i = 0; i < sh; i++) {
for (int j = 0; j < n1.length(); j++) {
int now = n1.charAt(j) - '0';
if ((now == 0 && i == 0) || (i & (1 << j)) != 0) {
continue;
}
for (int k = 0; k < m; k++) {
if (dp[i][k] > 0) {
int i1 = i | (1 << j);
int i2 = (10 * k + now) % m;
dp[i1][i2] += dp[i][k];
}
}
}
}
long x = dp[sh - 1][0];
for (int i = 0; i <= 9; i++) {
x /= f[count[i]];
}
out.println(x);
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.*;
import java.util.*;
import java.math.*;
public class Sol4{
public static void main(String[] args) throws IOException{
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
String str = sc.nextToken();
int m= sc.nextInt();
int n = str.length();
long fact[] = new long[19];
fact[0] = 1;
for(int i=1; i<19; i++) {
fact[i] = fact[i-1]*i;
}
int num[] = new int[10];
int dig[] = new int[n];
for(int i=0; i<n; i++) {
dig[i] = str.charAt(i)-'0';
num[dig[i]]++;
}
long tot = 1;
for(int i=0; i<10; i++) {
tot*=fact[num[i]];
}
long dp[][] = new long[1<<n][m];
for(int i=0; i<(1<<n); i++) {
Arrays.fill(dp[i], 0);
}
dp[0][0] = 1;
for(int i=0; i<(1<<n); i++) {
for(int j=0; j<m; j++) {
if(dp[i][j]==0) continue;
for(int b=0; b<n; b++) {
if((i&(1<<b))!=0) continue;
if(i==0&&dig[b]==0) continue;
dp[i|(1<<b)][(10*j+dig[b])%m]+=dp[i][j];
}
}
}
out.println(dp[(1<<n)-1][0]/tot);
out.close();
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
st = null;
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.Map.Entry;
public class Main implements Runnable {
static final int MOD = (int) 1e9 + 7;
static final int MI = (int) 1e9;
static final long ML = (long) 1e18;
static final Reader in = new Reader();
static final PrintWriter out = new PrintWriter(System.out);
StringBuilder answer = new StringBuilder();
Random random = new Random(751454315315L + System.currentTimeMillis());
public static void main(String[] args) {
new Thread(null, new Main(), "persefone", 1 << 28).start();
}
@Override
public void run() {
solve();
printf();
flush();
}
void solve() {
int[] s = in.next().chars().map(n -> n - '0').toArray();
int m = in.nextInt();
int n = s.length;
long[][] dp = new long[1 << n][m];
dp[0][0] = 1;
for (int mask = 0; mask < 1 << n; mask++) {
for (int reminder = 0; reminder < m; reminder++) {
if (dp[mask][reminder] == 0) {
continue;
}
for (int pos = 0; pos < n; pos++) {
if (mask == 0 && s[pos] == 0) continue;
if ((mask & (1 << pos)) > 0) continue;
int new_reminder = (10 * reminder + s[pos]) % m;
dp[mask | (1 << pos)][new_reminder] += dp[mask][reminder];
}
}
}
long ans = dp[(1 << n) - 1][0];
long[] fact = new long[20];
fact[0] = 1;
for (int i = 1; i < 20; i++) {
fact[i] = fact[i - 1] * i;
}
int[] cnt = new int[10];
for (int i = 0; i < n; i++) {
cnt[s[i]]++;
}
for (int i = 0; i < 10; i++) {
ans /= fact[cnt[i]];
}
printf(ans);
}
void printf() {
out.print(answer);
}
void close() {
out.close();
}
void flush() {
out.flush();
}
void printf(Stream<?> str) {
str.forEach(o -> add(o, " "));
add("\n");
}
void printf(Object... obj) {
printf(false, obj);
}
void printfWithDescription(Object... obj) {
printf(true, obj);
}
private void printf(boolean b, Object... obj) {
if (obj.length > 1) {
for (int i = 0; i < obj.length; i++) {
if (b) add(obj[i].getClass().getSimpleName(), " - ");
if (obj[i] instanceof Collection<?>) {
printf((Collection<?>) obj[i]);
} else if (obj[i] instanceof int[][]) {
printf((int[][]) obj[i]);
} else if (obj[i] instanceof long[][]) {
printf((long[][]) obj[i]);
} else if (obj[i] instanceof double[][]) {
printf((double[][]) obj[i]);
} else printf(obj[i]);
}
return;
}
if (b) add(obj[0].getClass().getSimpleName(), " - ");
printf(obj[0]);
}
void printf(Object o) {
if (o instanceof int[])
printf(Arrays.stream((int[]) o).boxed());
else if (o instanceof char[])
printf(new String((char[]) o));
else if (o instanceof long[])
printf(Arrays.stream((long[]) o).boxed());
else if (o instanceof double[])
printf(Arrays.stream((double[]) o).boxed());
else if (o instanceof boolean[]) {
for (boolean b : (boolean[]) o) add(b, " ");
add("\n");
} else
add(o, "\n");
}
void printf(int[]... obj) {
for (int i = 0; i < obj.length; i++) printf(obj[i]);
}
void printf(long[]... obj) {
for (int i = 0; i < obj.length; i++) printf(obj[i]);
}
void printf(double[]... obj) {
for (int i = 0; i < obj.length; i++) printf(obj[i]);
}
void printf(boolean[]... obj) {
for (int i = 0; i < obj.length; i++) printf(obj[i]);
}
void printf(Collection<?> col) {
printf(col.stream());
}
<T, K> void add(T t, K k) {
if (t instanceof Collection<?>) {
((Collection<?>) t).forEach(i -> add(i, " "));
} else if (t instanceof Object[]) {
Arrays.stream((Object[]) t).forEach(i -> add(i, " "));
} else
add(t);
add(k);
}
<T> void add(T t) {
answer.append(t);
}
static class Reader {
private BufferedReader br;
private StringTokenizer st;
Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Reader(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
boolean isReady() throws IOException {
return br.ready();
}
String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
} | JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.util.*;
import java.io.*;
import java.awt.Point;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.lang.Math.*;
// Solution is at the bottom of code
public class D implements Runnable{
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
OutputWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args){
new Thread(null, new D(), "", 128 * (1L << 20)).start();
}
/////////////////////////////////////////////////////////////////////
void init() throws FileNotFoundException{
Locale.setDefault(Locale.US);
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new OutputWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new OutputWriter("output.txt");
}
}
////////////////////////////////////////////////////////////////
long timeBegin, timeEnd;
void time(){
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
void debug(Object... objects){
if (ONLINE_JUDGE){
for (Object o: objects){
System.err.println(o.toString());
}
}
}
/////////////////////////////////////////////////////////////////////
public void run(){
try{
timeBegin = System.currentTimeMillis();
Locale.setDefault(Locale.US);
init();
solve();
out.close();
time();
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
/////////////////////////////////////////////////////////////////////
String delim = " ";
String readString() throws IOException{
while(!tok.hasMoreTokens()){
try{
tok = new StringTokenizer(in.readLine());
}catch (Exception e){
return null;
}
}
return tok.nextToken(delim);
}
String readLine() throws IOException{
return in.readLine();
}
/////////////////////////////////////////////////////////////////
final char NOT_A_SYMBOL = '\0';
char readChar() throws IOException{
int intValue = in.read();
if (intValue == -1){
return NOT_A_SYMBOL;
}
return (char) intValue;
}
char[] readCharArray() throws IOException{
return readLine().toCharArray();
}
/////////////////////////////////////////////////////////////////
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int index = 0; index < size; ++index){
array[index] = readInt();
}
return array;
}
int[] readIntArrayWithDecrease(int size) throws IOException {
int[] array = readIntArray(size);
for (int i = 0; i < size; ++i) {
array[i]--;
}
return array;
}
///////////////////////////////////////////////////////////////////
long readLong() throws IOException{
return Long.parseLong(readString());
}
long[] readLongArray(int size) throws IOException{
long[] array = new long[size];
for (int index = 0; index < size; ++index){
array[index] = readLong();
}
return array;
}
////////////////////////////////////////////////////////////////////
double readDouble() throws IOException{
return Double.parseDouble(readString());
}
double[] readDoubleArray(int size) throws IOException{
double[] array = new double[size];
for (int index = 0; index < size; ++index){
array[index] = readDouble();
}
return array;
}
////////////////////////////////////////////////////////////////////
BigInteger readBigInteger() throws IOException {
return new BigInteger(readString());
}
BigDecimal readBigDecimal() throws IOException {
return new BigDecimal(readString());
}
/////////////////////////////////////////////////////////////////////
Point readPoint() throws IOException{
int x = readInt();
int y = readInt();
return new Point(x, y);
}
Point[] readPointArray(int size) throws IOException{
Point[] array = new Point[size];
for (int index = 0; index < size; ++index){
array[index] = readPoint();
}
return array;
}
/////////////////////////////////////////////////////////////////////
List<Integer>[] readGraph(int vertexNumber, int edgeNumber)
throws IOException{
@SuppressWarnings("unchecked")
List<Integer>[] graph = new List[vertexNumber];
for (int index = 0; index < vertexNumber; ++index){
graph[index] = new ArrayList<Integer>();
}
while (edgeNumber-- > 0){
int from = readInt() - 1;
int to = readInt() - 1;
graph[from].add(to);
graph[to].add(from);
}
return graph;
}
/////////////////////////////////////////////////////////////////////
static class IntIndexPair {
static Comparator<IntIndexPair> increaseComparator = new Comparator<D.IntIndexPair>() {
@Override
public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) return value1 - value2;
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
}
};
static Comparator<IntIndexPair> decreaseComparator = new Comparator<D.IntIndexPair>() {
@Override
public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) return -(value1 - value2);
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
}
};
int value, index;
public IntIndexPair(int value, int index) {
super();
this.value = value;
this.index = index;
}
public int getRealIndex() {
return index + 1;
}
}
IntIndexPair[] readIntIndexArray(int size) throws IOException {
IntIndexPair[] array = new IntIndexPair[size];
for (int index = 0; index < size; ++index) {
array[index] = new IntIndexPair(readInt(), index);
}
return array;
}
/////////////////////////////////////////////////////////////////////
static class OutputWriter extends PrintWriter{
final int DEFAULT_PRECISION = 12;
int precision;
String format, formatWithSpace;
{
precision = DEFAULT_PRECISION;
format = createFormat(precision);
formatWithSpace = format + " ";
}
public OutputWriter(OutputStream out) {
super(out);
}
public OutputWriter(String fileName) throws FileNotFoundException {
super(fileName);
}
public int getPrecision() {
return precision;
}
public void setPrecision(int precision) {
precision = max(0, precision);
this.precision = precision;
format = createFormat(precision);
formatWithSpace = format + " ";
}
private String createFormat(int precision){
return "%." + precision + "f";
}
@Override
public void print(double d){
printf(format, d);
}
public void printWithSpace(double d){
printf(formatWithSpace, d);
}
public void printAll(double...d){
for (int i = 0; i < d.length - 1; ++i){
printWithSpace(d[i]);
}
print(d[d.length - 1]);
}
@Override
public void println(double d){
printlnAll(d);
}
public void printlnAll(double... d){
printAll(d);
println();
}
}
/////////////////////////////////////////////////////////////////////
int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int[][] steps8 = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1},
{-1, -1}, {1, 1}, {1, -1}, {-1, 1}
};
boolean check(int index, int lim){
return (0 <= index && index < lim);
}
/////////////////////////////////////////////////////////////////////
boolean checkBit(int mask, int bit){
return (mask & (1 << bit)) != 0;
}
/////////////////////////////////////////////////////////////////////
void solve() throws IOException {
char[] number = readString().toCharArray();
int n = number.length;
int md = readInt();
int lim = (1 << n);
long[][] dp = new long[lim][md];
dp[0][0] = 1;
int[] pows = new int[n];
pows[0] = 1;
for (int i = 1; i < n; ++i) {
pows[i] = pows[i-1] * 10 % md;
}
int[] used = new int[10];
for (int mask = 1; mask < lim; ++mask) {
int pow = pows[n - Integer.bitCount(mask)];
for (int bit = 0; bit < n; ++bit) {
int digit = number[bit] - '0';
if (checkBit(mask, bit) && used[digit] != mask) {
int fromMask = (mask ^ (1 << bit));
if (digit == 0 && fromMask == 0) {
// bad idea
} else {
used[digit] = mask;
for (int fromRem = 0; fromRem < md; ++fromRem) {
int curRem = (fromRem + digit * pow) % md;
dp[mask][curRem] = (dp[mask][curRem] + dp[fromMask][fromRem]);
}
}
}
}
}
long ans = dp[lim-1][0];
out.println(ans);
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | //Author: net12k44
import java.io.*;
import java.util.*;
//public
class Main{//}
static PrintWriter out;
boolean check(int state, int a[] , int n){
boolean d[] = new boolean[10];
for(int i = 0; i < n; ++i)
if ( ( state & (1 << i) ) == 0 ) d[ a[i] ] = true; else
if ( d[a[i]] == true ) return false;
return true;
}
private void solve() {
long number = in.nextLong();
int m = in.nextInt();
int a[] = new int[20] , n = 0;
while (number > 0){
a[n++] = (int)( number%10 );
number /=10;
}
Arrays.sort(a,0,n);
long f[][] = new long[1 << n][m];
for(int i = 0; i < n; ++i)
if (a[i] != 0) f[1<<i][a[i]%m] = 1;
for(int state = 1; state+1 < (1 << n); ++state)
if (check(state , a , n) )
for(int i = 0; i < n; ++i)
if ( ( state & (1 << i) ) == 0 ){
int next = state | (1 << i);
for(int mod = 0; mod < m; ++mod)
f[next][ (mod*10 + a[i]) % m ] += f[state][mod];
}
out.println( f[ (1 << n) - 1 ][0] );
}
public static void main (String[] args) throws java.lang.Exception {
long startTime = System.currentTimeMillis();
out = new PrintWriter(System.out);
new Main().solve();
//out.println((String.format("%.2f",(double)(System.currentTimeMillis()-startTime)/1000)));
out.close();
}
static class in {
static BufferedReader reader = new BufferedReader( new InputStreamReader(System.in) ) ;
static StringTokenizer tokenizer = new StringTokenizer("");
static String next() {
while ( !tokenizer.hasMoreTokens() )
try { tokenizer = new StringTokenizer( reader.readLine() ); }
catch (IOException e){
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
static int nextInt() { return Integer.parseInt( next() ); }
static long nextLong(){ return Long.parseLong( next() ); }
}
//////////////////////////////////////////////////
}// | JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class _D {
static long dp[][],ten[];
static int len,arr[],m;
static long solve(int index, int mod,int mask){
if(index == len){
if(mod%m==0){
return 1;
}
return 0;
}
else if(dp[mod][mask]!=-1){
return dp[mod][mask];
}
else {
long way =0;
int red[] = new int [10];
for(int i= 0;i<len;i++){
if((mask&(1<<i))==0){
if((index==0&&arr[i]==0)||red[arr[i]]==1)
continue;
red[arr[i]] = 1;
way+=solve(index+1, (int) (mod+(arr[i]*(ten[len-index-1]%m))%m)%m, mask|(1<<i));
}
}
return dp[mod][mask]= way;
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
long t = System.currentTimeMillis();
StringTokenizer st = new StringTokenizer(in.readLine());
long n = Long.parseLong(st.nextToken());
ten = new long [19];
ten[0] = 1;
for(int i=1;i<ten.length;i++)
ten[i]=ten[i-1]*10;
m = Integer.parseInt(st.nextToken());
long temp = n;
while(temp>0){
temp/=10;
len++;
}
arr= new int[len];
int i = 0;
int digit[] = new int [10];
while(n>0){
arr[i] = (int) (n%10);
digit[arr[i]]++;
i++;
n/=10;
}
dp = new long [m][1<<len];
for(i = 0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
out.println(solve(0,0, 0));
in.close();
out.close();
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
new Task().solve(scanner, out);
scanner.close();
out.close();
}
}
class Task {
public void solve(Scanner in, PrintWriter out) {
String bigNumber = in.next();
int mod = in.nextInt();
int n = bigNumber.length();
int maxMask = (1 << n) - 1;
double dp[][] = new double[maxMask + 1][mod];
for (int k = 0; k < n; k++) {
int bit = 1 << k;
if ('0' != bigNumber.charAt(k))
dp[bit][(bigNumber.charAt(k) - '0') % mod] = 1;
}
for (int i = 0; i < maxMask; i++) {
for (int j =0; j < mod; j++) {
for (int k = 0; k < n; k++) {
int bit = 1 << k;
if (0 == (i & bit)) {
dp[i | bit][(j * 10 + (bigNumber.charAt(k) - '0')) % mod] += dp[i][j];
}
}
}
}
int[] count = new int[10];
for (int i =0; i < n; i++) {
count[bigNumber.charAt(i) - '0']++;
}
double result = dp[maxMask][0];
for (int i=0; i < 10; i++) {
for (int j=1; j <= count[i]; j++) {
result = result / j;
}
}
out.println((long) result);
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | import java.io.*;
import java.util.*;
public class Main implements Runnable {
final boolean isFileIO = false;
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
String delim = " ";
public static void main(String[] args) {
new Thread(null, new Main(), "", 268435456).start();
}
public void run() {
try {
initIO();
solve();
out.close();
} catch(Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
public void initIO() throws IOException {
if(!isFileIO) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String nextToken() throws IOException {
if(!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken(delim);
}
String readLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
void solve() throws IOException {
long n; int m;
n = nextLong(); m = nextInt();
ArrayList<Integer> digits = new ArrayList<Integer>();
long temp = n;
while(temp != 0) {
digits.add((int)(temp % 10));
temp /= 10;
}
int cnt = digits.size();
int max = 1 << cnt;
long[][] dp = new long[max][m];
long[] fact = new long[10];
for(int i = 0; i < 10; i++) {
int c = 0;
for(int j = 0; j < cnt; j++)
if(digits.get(j) == i) c++;
fact[i] = 1;
for(long j = 2; j <= c; j++)
fact[i] = fact[i] * j;
}
for(int i = 0; i < cnt; i++) {
int digit = digits.get(i);
if(digit == 0) continue;
dp[1 << i][digit % m] = 1;
}
for(int mask = 1; mask < max; mask++) {
for(int j = 0; j < cnt; j++) {
int cur = (1 << j);
if((mask & cur) == cur) {
for(int k = 0; k < m; k++) {
dp[mask][(k * 10 + digits.get(j)) % m] += dp[mask ^ cur][k];
}
}
}
}
long ans = 0;
ans += dp[max - 1][0];
for(int i = 0; i < 10; i++) {
ans /= fact[i];
}
out.println(ans);
}
}
| JAVA |
401_D. Roman and Numbers | 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. | 2 | 10 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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);
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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] << " ";
}
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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("");
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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]);
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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();
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | import java.io.*;
import java.util.*;
import static java.util.Arrays.sort;
import static java.util.Arrays.fill;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) {
if((new File("input.txt")).exists())
try {
System.setIn(new FileInputStream("input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
new Thread(null, new Runnable() {
@Override
public void run() {
try {
new Main().run();
} catch (IOException e) {
e.printStackTrace();
}
}
}, "th1", 1 << 24).start();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
void run() throws IOException{
in = new BufferedReader(new InputStreamReader(System.in));
// in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
TreeSet<Range> rs = new TreeSet<>();
int color[];
MultiList g;
int idGen = 0;
void solve() throws IOException{
int n = nextInt();
g = new MultiList(2 * n + 1, 4 * n + 1);
color = new int[n * 2 + 1];
for(int i = 0; i < n; i++){
Range r;
rs.add( r = new Range(idGen++, nextInt(), nextInt() + 1));
}
solveRanges();
fill(color, -1);
for(int i = 0; i < n; i++)
if(color[i] == -1) dfs(i, 0);
for(int i = 0; i < n; i++)
out.print(((i != 0) ? " " : "") + color[i]);
}
private void solveRanges() {
while(rs.size() > 1){
Range fst = rs.pollFirst();
Range snd = rs.pollFirst();
if(fst.ri <= snd.le) {
rs.add(snd);
continue;
}
addEdge(fst.id, snd.id);
Range next = new Range(idGen++, min(fst.ri, snd.ri), max(fst.ri, snd.ri));
rs.add(next);
if(fst.ri < snd.ri)
addEdge(idGen - 1, fst.id);
else
addEdge(idGen - 1, snd.id);
}
}
void dfs(int v, int cc) {
if(color[v] != -1 && color[v] != cc) throw new RuntimeException();
if(color[v] != -1) return;
color[v] = cc;
for(int j = g.head[v]; j != 0; j = g.next[j]) {
int nv = g.vert[j];
dfs(nv, cc ^ 1);
}
}
void addEdge(int a, int b){
g.add(a, b);
g.add(b, a);
}
class MultiList {
int[] head, vert, next;
int cnt;
MultiList(int h, int s){
s++;
head = new int[h];
vert = new int[s];
next = new int[s];
cnt = 1;
}
void add(int h, int v){
vert[cnt] = v;
next[cnt] = head[h];
head[h] = cnt++;
}
}
class Range implements Comparable<Range>{
int le, ri;
int id;
public Range(int id, int le, int ri) {
super();
this.le = le;
this.ri = ri;
this.id = id;
}
@Override
public int compareTo(Range e) {
return (le != e.le) ? (le - e.le) : ( (ri != e.ri) ? (ri - e.ri) : (id - e.id));
}
@Override
public String toString() {
return "[" + le + "; " + (ri - 1) + "]" + "%" + id;
}
}
public String nextToken() throws IOException{
while(!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
public int nextInt() throws IOException{
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException{
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException{
return Double.parseDouble(nextToken());
}
}
| JAVA |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.AbstractQueue;
import java.io.IOException;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
EPointsAndSegments solver = new EPointsAndSegments();
solver.solve(1, in, out);
out.close();
}
}
static class EPointsAndSegments {
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.ri();
Interval[] intervals = new Interval[n];
for (int i = 0; i < n; i++) {
intervals[i] = new Interval(in.ri(), in.ri());
}
PriorityQueue<Interval> pq = new PriorityQueue<>(n, Comparator.comparingLong(x -> x.l));
pq.addAll(Arrays.asList(intervals));
while (pq.size() >= 2) {
Interval a = pq.remove();
Interval b = pq.remove();
if (a.r < b.l) {
pq.add(b);
continue;
}
a.adj.add(b);
b.adj.add(a);
if (a.r >= b.r) {
a.l = b.r + 1;
if (a.l <= a.r) {
pq.add(a);
}
} else {
b.l = a.r + 1;
pq.add(b);
}
}
for (Interval interval : intervals) {
dfs(interval, 0);
out.append(interval.v).append(' ');
}
}
public void dfs(Interval root, int c) {
if (root.v != -1) {
return;
}
root.v = c;
for (Interval interval : root.adj) {
dfs(interval, c ^ 1);
}
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int ri() {
return readInt();
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class Interval {
int v = -1;
int l;
int r;
List<Interval> adj = new ArrayList<>();
public Interval(int l, int r) {
this.l = l;
this.r = r;
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 32 << 10;
private final Writer os;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(int c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput flush() {
try {
// boolean success = false;
// if (stringBuilderValueField != null) {
// try {
// char[] value = (char[]) stringBuilderValueField.get(cache);
// os.write(value, 0, cache.length());
// success = true;
// } catch (Exception e) {
// }
// }
// if (!success) {
os.append(cache);
// }
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
}
| JAVA |
429_E. Points and Segments | 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 | 2 | 11 | import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.Collections.*;
import static java.lang.Math.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.*;
import static java.math.BigInteger.*;
public class Main{
void run(){
Locale.setDefault(Locale.US);
boolean my;
try {
my = System.getProperty("MY_LOCAL") != null;
} catch (Exception e) {
my = false;
}
try{
err = System.err;
if( my ){
sc = new FastScanner(new BufferedReader(new FileReader("input.txt")));
// sc = new FastScanner(new BufferedReader(new FileReader("C:\\myTest.txt")));
out = new PrintWriter (new FileWriter("output.txt"));
}
else {
sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(new OutputStreamWriter(System.out));
}
// out = new PrintWriter(new OutputStreamWriter(System.out));
}catch(Exception e){
MLE();
}
if( my )
tBeg = System.currentTimeMillis();
solve();
if( my )
err.println( "TIME: " + (System.currentTimeMillis() - tBeg ) / 1e3 );
exit(0);
}
void exit( int val ){
err.flush();
out.flush();
System.exit(val);
}
double tBeg;
FastScanner sc;
PrintWriter out;
PrintStream err;
class FastScanner{
StringTokenizer st;
BufferedReader br;
FastScanner( BufferedReader _br ){
br = _br;
}
String readLine(){
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
String next(){
while( st==null || !st.hasMoreElements() )
st = new StringTokenizer(readLine());
return st.nextToken();
}
int nextInt(){ return Integer.parseInt(next()); }
long nextLong(){ return Long.parseLong(next()); }
double nextDouble(){ return Double.parseDouble(next()); }
}
void MLE(){
int[][] arr = new int[1024*1024][]; for( int i = 0; i < 1024*1024; ++i ) arr[i] = new int[1024*1024];
}
void MLE( boolean doNotMLE ){ if( !doNotMLE ) MLE(); }
void TLE(){
for(;;);
}
public static void main(String[] args) {
new Main().run();
// new Thread( null, new Runnable() {
// @Override
// public void run() {
// new Main().run();
// }
// }, "Lolka", 256_000_000L ).run();
}
////////////////////////////////////////////////////////////////
class Item{
int x, vertex;
Item( int _x, int _vertex ){
x = _x;
vertex = _vertex;
}
}
int n;
ArrayList<Integer>[] gr;
ArrayList<Item> arr;
int[] color;
void dfs( int v, int colorV ){
if( color[v] == colorV )
return;
if( color[v] != -1 && color[v] != colorV ){
out.println("-1");
exit(0);
}
color[v] = colorV;
for (Integer to : gr[v]) {
dfs( to, 1 - colorV );
}
}
void solve(){
n = sc.nextInt();
arr = new ArrayList<>();
gr = new ArrayList[2*n];
for (int i = 0; i < gr.length; i++) {
gr[i] = new ArrayList<>();
}
for (int i = 0; i < n; i++) {
int a = 2*sc.nextInt();
int b = 2*sc.nextInt() + 1;
arr.add( new Item(a,2*i ) );
arr.add( new Item(b,2*i+1) );
gr[2*i ].add(2*i+1);
gr[2*i+1].add(2*i );
}
Collections.sort( arr, (a,b) -> a.x - b.x );
for (int i = 0; i < arr.size(); i += 2) {
int v = arr.get(i).vertex;
int to = arr.get(i+1).vertex;
gr[v].add(to);
gr[to].add(v);
}
color = new int[2*n];
fill( color, -1 );
for (int v = 0; v < 2*n; v++) {
if( color[v] == -1 )
dfs(v, 0);
}
for (int v = 0; v < n; v++) {
out.print( " " + color[2*v] );
}
out.println();
}
} | JAVA |
429_E. Points and Segments | 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 | 2 | 11 | #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] << ' ';
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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";
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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));
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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("");
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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]);
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | import java.io.*;
import java.util.*;
public class R245qE {
static int n;
static TreeSet<Pair> set;
static int ans[];
static ArrayList<Integer> g[];
@SuppressWarnings("unchecked")
public static void main(String args[]) {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
n = in.nextInt();
set = new TreeSet<Pair>();
for(int i=1;i<=n;i++)
set.add(new Pair(i,in.nextInt(),in.nextInt()));
g = new ArrayList[n + 1];
for(int i=1;i<=n;i++)
g[i] = new ArrayList<Integer>();
while(set.size() >= 2){
Pair a = set.pollFirst();
Pair b = set.pollFirst();
if(a.y < b.x)
set.add(b);
else{
g[a.idx].add(b.idx);
g[b.idx].add(a.idx);
if(a.y < b.y){
b.x = a.y + 1;
set.add(b);
}
else if(a.y > b.y){
a.x = b.y + 1;
set.add(a);
}
}
}
ans = new int[n + 1];
Arrays.fill(ans, -1);
for(int i=1;i<=n;i++)
dfs(i,0);
for(int i=1;i<=n;i++)
w.print(ans[i] + " ");
w.println();
w.close();
}
static public void dfs(int curr,int color){
if(ans[curr] != -1) return;
ans[curr] = color;
for(int x : g[curr])
dfs(x,1 - color);
}
static class Pair implements Comparable<Pair>{
int idx,x,y;
Pair(int idx,int x,int y){
this.idx = idx;
this.x = x;
this.y = y;
}
public int compareTo(Pair o){
if(x != o.x)
return Integer.compare(x, o.x);
if(y != o.y)
return Integer.compare(y, o.y);
return Integer.compare(idx, o.idx);
}
public String toString(){
return idx + " " + x + " " + y;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int res = 0;
do {
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| JAVA |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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]);
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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);
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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] << ' ';
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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]]);
}
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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);
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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 << " ";
}
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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';
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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("");
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | import java.io.*;
import java.util.*;
public class Main {
private static Reader in;
private static PrintWriter out;
static class Interval implements Comparable<Interval> {
public int a, b, id;
public Interval(int a, int b, int id) {
this.a = a;
this.b = b;
this.id = id;
}
public int compareTo(Interval other) {
return a == other.a ? b - other.b : a - other.a;
}
}
private static int[] eadj, elast, eprev;
private static int eidx;
private static void addEdge(int a, int b) {
eadj[eidx] = b;
eprev[eidx] = elast[a];
elast[a] = eidx++;
eadj[eidx] = a;
eprev[eidx] = elast[b];
elast[b] = eidx++;
}
private static int[] ans, queue;
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(System.out, true);
int N = in.nextInt();
if (N == 1) {
out.println(1);
out.close();
System.exit(0);
}
PriorityQueue<Interval> pq = new PriorityQueue<Interval>();
for (int i = 0; i < N; i++) {
int a = in.nextInt(), b = in.nextInt();
pq.add(new Interval(a, b, i));
}
eadj = new int[2 * N];
elast = new int[N];
eprev = new int[2 * N];
eidx = 0;
Arrays.fill(elast, -1);
while (pq.size() > 1) {
Interval a = pq.poll(), b = pq.poll();
if (b.a > a.b) {
pq.add(b);
continue;
}
addEdge(a.id, b.id);
if (a.b > b.b) {
pq.add(new Interval(b.b + 1, a.b, a.id));
} else if (a.b < b.b) {
pq.add(new Interval(a.b + 1, b.b, b.id));
}
}
ans = new int[N];
queue = new int[N];
int front = 0, back = 0;
for (int i = 0; i < N; i++)
if (ans[i] == 0) {
ans[i] = 1;
queue[back++] = i;
while (front < back) {
int node = queue[front++];
for (int e = elast[node]; e != -1; e = eprev[e]) {
if (ans[eadj[e]] != 0)
continue;
ans[eadj[e]] = -ans[node];
queue[back++] = eadj[e];
}
}
}
out.print (ans[0] == -1 ? 0 : 1);
for (int i = 1; i < N; i++)
out.print(" " + (ans[i] == -1 ? 0 : 1));
out.close();
System.exit(0);
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[1024];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}
| JAVA |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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("");
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | import java.io.*;
import java.util.*;
public class R245qE {
static int n;
static TreeSet<Pair> set;
static int ans[];
static ArrayList<Integer> g[];
@SuppressWarnings("unchecked")
public static void main(String args[]) {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
n = in.nextInt();
set = new TreeSet<Pair>();
for(int i=1;i<=n;i++)
set.add(new Pair(i,in.nextInt(),in.nextInt()));
g = new ArrayList[n + 1];
for(int i=1;i<=n;i++)
g[i] = new ArrayList<Integer>();
while(set.size() >= 2){
// System.out.println(set);
Pair a = set.pollFirst();
Pair b = set.pollFirst();
if(a.y < b.x)
set.add(b);
else{
//System.out.println(a.idx + " " + b.idx);
g[a.idx].add(b.idx);
g[b.idx].add(a.idx);
if(a.y < b.y){
b.x = a.y + 1;
set.add(b);
}
else if(a.y > b.y){
a.x = b.y + 1;
set.add(a);
}
}
}
ans = new int[n + 1];
Arrays.fill(ans, -1);
ans[0] = 0;
for(int i=1;i<=n;i++)
if(ans[i] == -1)
dfs(i,0);
for(int i=1;i<=n;i++)
w.print(ans[i] + " ");
w.println();
w.close();
}
static public void dfs(int curr,int color){
if(ans[curr] != -1){
if(ans[curr] != color)
System.out.println("here");
return;
}
ans[curr] = color;
color = 1 - color;
for(int x : g[curr])
dfs(x,color);
}
static class Pair implements Comparable<Pair>{
int idx,x,y;
Pair(int idx,int x,int y){
this.idx = idx;
this.x = x;
this.y = y;
}
public int compareTo(Pair o){
if(x != o.x)
return Integer.compare(x, o.x);
if(y != o.y)
return Integer.compare(y, o.y);
return Integer.compare(idx, o.idx);
}
public String toString(){
return idx + " " + x + " " + y;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| JAVA |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
429_E. Points and Segments | 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 | 2 | 11 | #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;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.