Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. //@Author: Ozhetov Arsen Nurlanuly
  2. #pragma comment (linker, "/stack:20000000")
  3. #pragma GCC optimize ("Ofast")
  4. #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. #include <ext/pb_ds/detail/standard_policies.hpp>
  9. #define sz(s) (int)(s.size ())
  10. #define all(s) s.begin (), s.end ()
  11. #define rall(s) s.rbegin (), s.rend ()
  12. #define Unique(s) s.resize (unique (all (s)) - s.begin ());
  13. #define fi first
  14. #define se second
  15. #define endl "\n"
  16. #define debug(...) fprintf (stderr, __VA_ARGS__)
  17. #define Time clock () * 1.0 / CLOCKS_PER_SEC
  18. #define sqr(x) ((x) * 1ll * (x))
  19. #define lcm(a, b) ((a) / gcd (a, b) * (b))
  20. #define bit(x) __builtin_popcount (x)
  21. #define bitll(x) __builtin_popcountll (x)
  22. #define foreach(it, s) for (__typeof (s.begin ()) it = s.begin (); it != s.end (); ++it)
  23. #define rep(a, b, c) for (int a = b; a <= c; ++a)
  24. #define per(a, b, c) for (int a = b; a >= c; --a)
  25. #define _ ios_base::sync_with_stdio (false); cin.tie (NULL); cout.tie (NULL);
  26. using namespace std;
  27. using namespace __gnu_pbds;
  28. template<typename T1, typename T2>inline bool updmin (T1 &a, T2 &b) { return a > b ? a = b, 1 : 0; }
  29. template<typename T1, typename T2>inline bool updmax (T1 &a, T2 &b) { return a < b ? a = b, 1 : 0; }
  30. template<typename T>inline T gcd (T &a, T &b) { while (a && b) a > b ? a %= b : b %= a; return a + b; }
  31. typedef unsigned long long ull;
  32. typedef long long ll;
  33. typedef long double ld;
  34. typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  35. const double PI = (double)(acos (-1.0)), EPS = (double)(1e-7);
  36. const int MOD = 1e9 + 7, PR = 15937, INF = 1 << 30, MXN = 3e5 + 17;
  37. const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
  38.  
  39. inline int readChar ();
  40. template<class T = int>inline T readInt ();
  41. template<class T>inline void writeInt (T x, char end = 0);
  42. inline void writeChar (int x);
  43. inline void writeWord (const char *s);
  44.  
  45. static const int buf_size = 4096;
  46.  
  47. inline int getChar () {
  48. static char buf[buf_size];
  49. static int len = 0, pos = 0;
  50. if (pos == len)
  51. pos = 0, len = fread (buf, 1, buf_size, stdin);
  52. if (pos == len) return -1;
  53. return buf[pos++];
  54. }
  55.  
  56. inline int readChar () {
  57. int c = getChar ();
  58. while (c <= 32) c = getChar ();
  59. return c;
  60. }
  61.  
  62. template<class T>inline T readInt () {
  63. int s = 1, c = readChar ();
  64. T x = 0;
  65. if (c == '-') s = -1, c = getChar ();
  66. while ('0' <= c && c <= '9')
  67. x = x * 10 + c - '0', c = getChar ();
  68. return s == 1 ? x : -x;
  69. }
  70.  
  71. static int write_pos = 0;
  72. static char write_buf[buf_size];
  73.  
  74. inline void writeChar (int x) {
  75. if (write_pos == buf_size)
  76. fwrite (write_buf, 1, buf_size, stdout), write_pos = 0;
  77. write_buf[write_pos++] = x;
  78. }
  79.  
  80. template<class T>inline void writeInt (T x, char end) {
  81. if (x < 0) writeChar ('-'), x = -x;
  82. char s[24];
  83. int n = 0;
  84. while (x || !n)
  85. s[n++] = '0' + x % 10, x /= 10;
  86. while (n--) writeChar (s[n]);
  87. if (end) writeChar (end);
  88. }
  89.  
  90. inline void writeWord (const char *s) {
  91. while (*s) writeChar (*s++);
  92. }
  93.  
  94. struct Flusher {
  95. ~Flusher () {
  96. if (write_pos)
  97. fwrite (write_buf, 1, write_pos, stdout), write_pos = 0;
  98. }
  99. } flusher;
  100.  
  101. int n, k, a[MXN], ans[MXN];
  102.  
  103. vector<int>g[MXN];
  104.  
  105. int add[MXN * 4], t[MXN * 4];
  106.  
  107. inline void push (int v, int tl, int tr) {
  108. if (add[v] != -1) {
  109. if (tl != tr)
  110. add[v << 1] = add[v],
  111. add[v << 1 | 1] = add[v];
  112. t[v] = add[v];
  113. add[v] = -1;
  114. }
  115. }
  116.  
  117. void update (int v, int tl, int tr, int l, int r, int val) {
  118. push (v, tl, tr);
  119. if (l > tr || tl > r)
  120. return;
  121. if (l <= tl && tr <= r) {
  122. add[v] = val;
  123. return push (v, tl, tr);
  124. }
  125. int tm = (tl + tr) >> 1;
  126. update (v << 1, tl, tm, l, r, val);
  127. update (v << 1 | 1, tm + 1, tr, l, r, val);
  128. t[v] = t[v << 1] + t[v << 1 | 1];
  129. }
  130.  
  131. int get (int v, int tl, int tr, int l, int r) {
  132. push (v, tl, tr);
  133. if (l > tr || tl > r)
  134. return 0;
  135. if (l <= tl && tr <= r)
  136. return t[v];
  137. int tm = (tl + tr) >> 1;
  138. return get (v << 1, tl, tm, l, r) + get (v << 1 | 1, tm + 1, tr, l, r);
  139. }
  140.  
  141. void dfs (int v) {
  142. push (1, 1, n);
  143. ans[v] = t[1];
  144. int l = max (0, v - k);
  145. int r = min (n, v + k);
  146. update (1, 1, n, l, r, 1);
  147. for (auto &to : g[v])
  148. dfs (to);
  149. }
  150.  
  151. inline void Solve () {
  152. n = readInt ();
  153. k = readInt ();
  154. memset (ans, -1, sizeof ans);
  155. rep (i, 1, n)
  156. a[i] = readInt (),
  157. g[a[i]].push_back (i);
  158. dfs (0);
  159. rep (i, 1, n)
  160. writeInt (ans[i], ' ');
  161. }
  162.  
  163. inline void Clear () {}
  164.  
  165. int main () { //_
  166. unsigned int FOR;
  167. asm ("rdtsc" : "=A"(FOR)); srand (FOR);
  168. #define HaveTestCase 0
  169. #define LightOjTestCase 0
  170. #ifdef _DeSeiSH_
  171. freopen ("Input.txt", "r", stdin);
  172. freopen ("OutputMain.txt", "w", stdout);
  173. #endif
  174. int T = 1;
  175. if (HaveTestCase) T = readInt ();
  176. rep (i, 1, T) {
  177. if (LightOjTestCase)
  178. writeWord ("Case "), writeInt (i, ':');
  179. Solve ();
  180. if (i != T) Clear ();
  181. }
  182. #ifdef _DeSeiSH_
  183. debug ("\ntime: %.6lf\n", Time);
  184. #endif
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement