a53

episodul3

a53
Sep 11th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int transform (int x, int y, int m)
  6. {
  7. return (x-1)*m + y-1;
  8. }
  9.  
  10. int dp[1000001][21];
  11. bitset <1000001> fv;
  12. int nivel[1000001];
  13. int cate2;
  14. void adauga (int x, int y, int n, int m)
  15. {
  16. int current = transform(x, y, m);
  17. int stanga = transform (x, y-1, m), dreapta = transform(x, y+1, m), sus = transform(x-1, y, m), jos = transform(x+1, y, m);
  18. if (x == n) jos = -1;
  19. if (y == m) dreapta = -1;
  20. if (x == 1) sus = -1;
  21. if (y == 1) stanga = -1;
  22. if (stanga >= 0 && fv[stanga])
  23. {
  24. fv[current] = 1;
  25. nivel[current] = nivel[stanga]+1;
  26. dp[current][0] = stanga;
  27. for (int i = 1; i<=20; ++i)
  28. dp[current][i] = dp[dp[current][i-1]][i-1];
  29. return ;
  30. }
  31. if (dreapta >= 0 && fv[dreapta])
  32. {
  33. fv[current] = 1;
  34. nivel[current] = nivel[dreapta]+1;
  35. dp[current][0] = dreapta;
  36. for (int i = 1; i<=20; ++i)
  37. dp[current][i] = dp[dp[current][i-1]][i-1];
  38. return ;
  39. }
  40. if (jos >= 0 && fv[jos])
  41. {
  42. fv[current] = 1;
  43. nivel[current] = nivel[jos]+1;
  44. dp[current][0] = jos;
  45. for (int i = 1; i<=20; ++i)
  46. dp[current][i] = dp[dp[current][i-1]][i-1];
  47. return ;
  48. }
  49. if (sus >= 0 && fv[sus])
  50. {
  51. fv[current] = 1;
  52. nivel[current] = nivel[sus]+1;
  53. dp[current][0] = sus;
  54. for (int i = 1; i<=20; ++i)
  55. dp[current][i] = dp[dp[current][i-1]][i-1];
  56. return ;
  57. }
  58. fv[current] = 1;
  59. nivel[current] = 1;
  60. }
  61. class InParser {
  62. private:
  63. FILE *fin;
  64. char *buff;
  65. int sp;
  66.  
  67. char read_ch() {
  68. ++sp;
  69. if (sp == 4096) {
  70. sp = 0;
  71. fread(buff, 1, 4096, fin);
  72. }
  73. return buff[sp];
  74. }
  75.  
  76. public:
  77. InParser(const char* nume) {
  78. fin = fopen(nume, "r");
  79. buff = new char[4096]();
  80. sp = 4095;
  81. }
  82.  
  83. InParser& operator >> (int &n) {
  84. char c;
  85. while (!isdigit(c = read_ch()) && c != '-');
  86. int sgn = 1;
  87. if (c == '-') {
  88. n = 0;
  89. sgn = -1;
  90. } else {
  91. n = c - '0';
  92. }
  93. while (isdigit(c = read_ch())) {
  94. n = 10 * n + c - '0';
  95. }
  96. n *= sgn;
  97. return *this;
  98. }
  99.  
  100. InParser& operator >> (long long &n) {
  101. char c;
  102. n = 0;
  103. while (!isdigit(c = read_ch()) && c != '-');
  104. long long sgn = 1;
  105. if (c == '-') {
  106. n = 0;
  107. sgn = -1;
  108. } else {
  109. n = c - '0';
  110. }
  111. while (isdigit(c = read_ch())) {
  112. n = 10 * n + c - '0';
  113. }
  114. n *= sgn;
  115. return *this;
  116. }
  117. };
  118. class OutParser {
  119. private:
  120. FILE *fout;
  121. char *buff;
  122. int sp;
  123.  
  124. void write_ch(char ch) {
  125. if (sp == 50000) {
  126. fwrite(buff, 1, 50000, fout);
  127. sp = 0;
  128. buff[sp++] = ch;
  129. } else {
  130. buff[sp++] = ch;
  131. }
  132. }
  133.  
  134.  
  135. public:
  136. OutParser(const char* name) {
  137. fout = fopen(name, "w");
  138. buff = new char[50000]();
  139. sp = 0;
  140. }
  141. ~OutParser() {
  142. fwrite(buff, 1, sp, fout);
  143. fclose(fout);
  144. }
  145.  
  146. OutParser& operator << (int vu32) {
  147. if (vu32 <= 9) {
  148. write_ch(vu32 + '0');
  149. } else {
  150. (*this) << (vu32 / 10);
  151. write_ch(vu32 % 10 + '0');
  152. }
  153. return *this;
  154. }
  155.  
  156. OutParser& operator << (long long vu64) {
  157. if (vu64 <= 9) {
  158. write_ch(vu64 + '0');
  159. } else {
  160. (*this) << (vu64 / 10);
  161. write_ch(vu64 % 10 + '0');
  162. }
  163. return *this;
  164. }
  165.  
  166. OutParser& operator << (char ch) {
  167. write_ch(ch);
  168. return *this;
  169. }
  170. OutParser& operator << (const char *ch) {
  171. while (*ch) {
  172. write_ch(*ch);
  173. ++ch;
  174. }
  175. return *this;
  176. }
  177. };
  178. int distanta (int x1, int y1, int x2, int y2, int n, int m)
  179. {
  180. int nod1 = transform(x1, y1, m), nod2 = transform(x2, y2, m);
  181. if (nod1 == nod2) return 1;
  182. if (nivel[nod1] > nivel[nod2]) swap(nod1, nod2);
  183. int copie1 = nod1, copie2 = nod2;
  184. int diferenta = nivel[nod2] - nivel[nod1];
  185. for (int i = 20; i>=0; --i)
  186. if (diferenta >= (1<<i))
  187. {
  188. nod2 = dp[nod2][i];
  189. diferenta -= (1<<i);
  190. }
  191. if (nod1 == nod2) return nivel[copie1] + nivel[copie2] - 2*nivel[nod1] + 1;
  192. for (int i = 20; i>=0; --i)
  193. if (dp[nod1][i] != dp[nod2][i])
  194. {
  195. nod1 = dp[nod1][i];
  196. nod2 = dp[nod2][i];
  197. }
  198. nod1 = dp[nod1][0];
  199. nod2 = dp[nod2][0];
  200. return nivel[copie1] + nivel[copie2] - 2*nivel[nod1] + 1;
  201. }
  202. int main(int argc, char const *argv[])
  203. {
  204. InParser fin ("episodul3.in");
  205. OutParser fout ("episodul3.out");
  206. int n, m;
  207. fin >> n >> m;
  208. for (int i = 1; i<=m; ++i)
  209. {
  210. int tip;
  211. fin >> tip;
  212. if (tip == 1)
  213. {
  214. int x, y;
  215. fin >> x >> y;
  216. adauga(x, y, n, n);
  217. }
  218. else
  219. {
  220. int x1, y1, x2, y2;
  221. fin >> x1 >> y1 >> x2 >> y2;
  222. fout << distanta(x1, y1, x2, y2, n, n) << '\n';
  223. }
  224. }
  225. return 0;
  226. }
Add Comment
Please, Sign In to add comment