fork download
  1. #include <cstdio>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <queue>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. const int knmax = 401337;
  11.  
  12. class edg{
  13. public:
  14. int x, y, color;
  15.  
  16. edg(){};
  17.  
  18. edg(int a1, int a2){
  19. x = a1;
  20. y = a2;
  21. color = -1;
  22. }
  23.  
  24. int nbr(int x1){
  25. if(x == x1)
  26. return y;
  27. return x;
  28. }
  29. };
  30.  
  31. vector<int> g[knmax];
  32. vector<edg> t;
  33.  
  34. int evis[knmax * 5], nvis[knmax];
  35. vector<int> ans;
  36.  
  37. void dfs(int x){
  38. nvis[x] = 1;
  39. for(int i = 0; i < g[x].size(); ++i)
  40. if(!evis[g[x][i]]){
  41. evis[g[x][i]] = 1;
  42. dfs(t[g[x][i]].nbr(x));
  43. }
  44. ans.push_back(x);
  45. }
  46.  
  47. class seg{
  48. public:
  49. int x, y;
  50.  
  51. seg(){};
  52.  
  53. seg(int x1, int y1){
  54. x = x1;
  55. y = y1;
  56. }
  57. };
  58.  
  59. int nAt, nSg;
  60. seg gv[knmax];
  61. vector<int> atomz;
  62.  
  63. class hdata{
  64. public:
  65. int x, y, num;
  66.  
  67. hdata(){};
  68.  
  69. hdata(int x1, int y1, int n1){
  70. x = x1;
  71. y = y1;
  72. num = n1;
  73. }
  74.  
  75. bool operator ==(hdata other){
  76. return x == other.x && y == other.y;
  77. }
  78. };
  79.  
  80. const int kmod = 666013, kp1 = 29, kp2 = 37;
  81.  
  82. vector<hdata> h[kmod];
  83.  
  84. int make_key(int x, int y){
  85. return (x * kp1 + y * kp2) % kmod;
  86. }
  87.  
  88. void insert(hdata x){
  89. int key = make_key(x.x, x.y);
  90. h[key].push_back(x);
  91. }
  92.  
  93. void color(int x, int y, int col){
  94. hdata now(x, y, 1337);
  95. int key = make_key(x, y);
  96. for(int i = 0; i < h[key].size(); ++i)
  97. if(h[key][i] == now){
  98. t[h[key][i].num].color = col;
  99. swap(h[key].back(), h[key][i]);
  100. h[key].pop_back();
  101. return;
  102. }
  103. }
  104.  
  105. int deg[knmax];
  106.  
  107. void new_edge(int x, int y){
  108. t.push_back(edg(x, y));
  109. g[x].push_back(t.size() - 1);
  110. g[y].push_back(t.size() - 1);
  111. ++deg[y];
  112. ++deg[x];
  113.  
  114. insert(hdata(x, y, (int)t.size() - 1));
  115. }
  116.  
  117. void make_edges(){
  118. for(int i = 1; i <= nSg; ++i){
  119. int a, b;
  120. int l = 0, r = nAt, mid, last;
  121. while(l <= r){
  122. mid = (l + r) / 2;
  123. if(atomz[mid] > gv[i].x)
  124. r = mid - 1;
  125. else{
  126. l = mid + 1;
  127. last = mid;
  128. }
  129. }
  130. a = last;
  131. l = 0;r = nAt;
  132. while(l <= r){
  133. mid = (l + r) / 2;
  134. if(atomz[mid] > gv[i].y)
  135. r = mid - 1;
  136. else{
  137. l = mid + 1;
  138. last = mid;
  139. }
  140. }
  141. b = last;
  142.  
  143. new_edge(a, b + 1);
  144. }
  145. }
  146.  
  147. void get_nodes(){
  148. scanf("%d", &nSg);
  149.  
  150. set<int> ends;
  151. for(int i = 1; i <= nSg; ++i){
  152. scanf("%d%d", &gv[i].x, &gv[i].y);
  153. ends.insert(gv[i].x);
  154. ends.insert(gv[i].y + 1);
  155. }
  156.  
  157. atomz.push_back(-1e9);
  158. for(set<int>::iterator it = ends.begin(); it != ends.end(); ++it)
  159. atomz.push_back(*it);
  160. atomz.push_back(1e9);
  161. nAt = atomz.size() - 1;
  162.  
  163. make_edges();
  164.  
  165. vector<int> unp;
  166. for(int i = 1; i <= nAt; ++i)
  167. if(deg[i] & 1)
  168. unp.push_back(i);
  169. int u = 200042;
  170. for(int i = 0; i < unp.size(); i += 2){
  171. new_edge(unp[i], u);
  172. new_edge(unp[i + 1], u);
  173. ++u;
  174. }
  175. }
  176.  
  177. void color_edges(){
  178. for(int i = 1; i < ans.size(); ++i){
  179. int x = ans[i - 1];
  180. int y = ans[i];
  181. if(x > y)
  182. color(y, x, 0);
  183. else
  184. color(x, y, 1);
  185. }
  186. }
  187.  
  188. void output_colors(){
  189. for(int i = 0; i < nSg; ++i)
  190. printf("%d ", t[i].color);
  191. }
  192.  
  193. int main(){
  194. #ifndef ONLINE_JUDGE
  195. freopen("E.in", "r", stdin);
  196. freopen("E.out", "w", stdout);
  197. #endif
  198.  
  199. get_nodes();
  200. for(int i = 1; i <= nAt; ++i)
  201. if(!nvis[i]){
  202. ans.clear();
  203. dfs(i);
  204. color_edges();
  205. }
  206.  
  207. output_colors();
  208.  
  209. return 0;
  210. }
  211.  
Success #stdin #stdout 0.01s 30096KB
stdin
Standard input is empty
stdout
Standard output is empty