Advertisement
newb_ie

New

Aug 8th, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. == == == == == =
  2. [TEMPLATES]
  3. == == == == == =
  4. Direction array :
  5. int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
  6. int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
  7. //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  8. #define filein freopen ("in.txt", "r", stdin)
  9. #define fileout freopen ("out.txt", "w", stdout)
  10. == == ==
  11. [PBDS]
  12. == == ==
  13. #include <ext/pb_ds/assoc_container.hpp>
  14. #include <ext/pb_ds/tree_policy.hpp>
  15. using namespace __gnu_pbds;
  16. typedef tree<int64_t, null_type, less_equal<int64_t>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  17.  
  18. freopen ("input.txt", "r", stdin);
  19. freopen ("output.txt", "w", stdout);
  20. template<typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
  21. template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ", "; os << "]"; return os; }
  22. template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ", "; os << "]"; return os; }
  23. template<typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ", "; os << "}"; return os; }
  24. template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ", "; os << "}"; return os; }
  25. template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ", "; os << "}"; return os; }
  26. template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ", "; os << "}"; return os; }
  27. template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << ", " << pa.second << ")"; return os; }
  28. template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }
  29. template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }
  30. template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
  31. template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
  32. template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
  33. template<typename T> bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }
  34. template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
  35. template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
  36. == == == == == == == == == == == =
  37. [ BITWISE FUNCTIONS ]
  38. == == == == == == == == == == == =
  39.  
  40. int set_bit(int n, int pos) {return n = (n | (1 << pos));}
  41. int reset_bit(int n, int pos) {return n = n & ~(1 << pos);}
  42. bool check_bit(int n, int pos) {return n = n & (1 << pos);}
  43.  
  44. void add (int & s, int n) {
  45. if ((s & (1 << (n - 1)))) return;
  46. s ^= (1 << (n - 1));
  47. }
  48.  
  49. void remove (int & s, int n) {
  50. if (!(s & (1 << (n - 1)))) return;
  51. s ^= (1 << (n - 1));
  52. }
  53.  
  54. void display (int s) {
  55. for (int bit = 0; bit < 9; ++bit) {
  56. if (s & (1 << bit)) {
  57. cout << bit + 1 << ' ';
  58. }
  59. }
  60. cout << '\n';
  61. }
  62. == == == == == == == == == == == =
  63. [NUMBER THEORY SECTION]
  64. == == == == == == == == == == == =
  65.  
  66. int64_t Fibo (int64_t n) {
  67. //res[-1] = 0,res[0] = res[1] = 1;
  68. //check fibo of n, cout << Fibo(n - 1);
  69. if (res.find(n) != res.end()) {
  70. return res[n];
  71. }
  72. if (n % 2 == 0) {
  73. return res[n] = (Fibo(n / 2) * Fibo(n / 2) + Fibo(n / 2 - 1) * Fibo(n / 2 - 1)) % mod;
  74. } else {
  75. return res[n] = (Fibo(n / 2) * Fibo(n / 2 + 1) + Fibo(n / 2 - 1) * Fibo(n / 2)) % mod;
  76. }
  77. }
  78.  
  79. /*
  80. GCD & LCM Template
  81. */
  82.  
  83. template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); }
  84. template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
  85.  
  86. /*
  87. Moduler Arithmetic
  88. */
  89. inline void normal(int64_t &a) { a %= mod; (a < 0) && (a += mod); }
  90. inline int64_t modMul(int64_t a, int64_t b) { a %= mod, b %= mod; normal(a), normal(b); return (a * b) % mod; }
  91. inline int64_t modAdd(int64_t a, int64_t b) { a %= mod, b %= mod; normal(a), normal(b); return (a + b) % mod; }
  92. inline int64_t modSub(int64_t a, int64_t b) { a %= mod, b %= mod; normal(a), normal(b); a -= b; normal(a); return a; }
  93. inline int64_t modPow(int64_t b, int64_t p) { int64_t r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
  94. inline int64_t modInverse(int64_t a) { return modPow(a, mod - 2); }
  95. inline int64_t modDiv(int64_t a, int64_t b) { return modMul(a, modInverse(b)); }
  96. == == == == == == ==
  97. [MATH FORMULA]
  98. == == == == == == ==
  99. //sum of (n)
  100. int sum = n * (n + 1) / 2;
  101. //sum of(n^2)
  102. int sum = ((n * (n + 1)) * (2 * n + 1)) / 6;
  103. //sum of range
  104. sum_of_range = n * ((r - l + 1) * (l + r)) / 2;
  105. /*
  106. Bitwise Sieve_of_Eratosthenes
  107. */
  108. const int maxN = 10000;
  109. int64_t isVisited[maxN / 64 + 200];
  110. vector<int> primes;
  111. void Sieve_of_Eratosthenes(int limit) {
  112. limit += 100;
  113. for (int64_t i = 3; i <= sqrt(limit) ; i += 2) {
  114. if (!(isVisited[i / 64] & (1LL << (i % 64)))) {
  115. for (int64_t j = i * i; j <= limit; j += 2 * i) {
  116. isVisited[j / 64] |= (1LL << (j % 64));
  117. }
  118. }
  119. }
  120. primes.push_back(2);
  121. for (int64_t i = 3; i <= limit; i += 2) {
  122. if (!(isVisited[i / 64] & (1LL << (i % 64)))) {
  123. primes.push_back(i);
  124. }
  125. }
  126. }
  127.  
  128. /*
  129. IS PRIME Function(Bitwise Seive)
  130. */
  131. bool is_prime(int n) {
  132. if (n < 2) return false;
  133. if (n == 2) return true;
  134. if (n % 2 == 0) return false;
  135. if (!(isVisited[n / 64] & (1LL << (n % 64)))) return true;
  136. return false;
  137. }
  138.  
  139. == == == == == == == == == == == == ==
  140. [DIS - JOINT SET UNION (DSU)]
  141. == == == == == == == == == == == == ==
  142. const int maxN = 100100;
  143. int parent[maxN];
  144.  
  145. int Find(int child) {
  146. if (parent[child] < 0) {
  147. return child;
  148. }
  149. return parent[child] = Find(parent[child]);
  150. }
  151.  
  152. void Union(int a, int b) {
  153. parent[a] += parent[b];
  154. parent[b] = a;
  155. }
  156. == == == == == == == == == == == == =
  157. [KMP SEARCH ALGORITHM]
  158. [COMPLEXITY : O(N + M) ]
  159. == == == == == == == == == == == == =
  160.  
  161. vector<int> lps_array(string p) {
  162. vector<int> lps((int) p.size());
  163. int i = 1, idx = 0;
  164. while (i < (int) p.size()) {
  165. if (p[idx] == p[i]) {
  166. lps[i] = idx + 1;
  167. idx += 1, i += 1;
  168. } else {
  169. if (idx != 0) {
  170. idx = lps[idx - 1];
  171. } else {
  172. lps[i] = 0;
  173. i += 1;
  174. }
  175. }
  176. }
  177. return lps;
  178. }
  179. void kmp(string s, string p) {
  180. vector<int> lps = lps_array(p);
  181. int i = 0, j = 0;
  182. while (i < (int) s.size()) {
  183. if (s[i] == p[j]) {
  184. i += 1, j += 1;
  185. } else {
  186. if (j != 0) {
  187. j = lps[j - 1];
  188. } else {
  189. i += 1;
  190. }
  191. }
  192. if (j == (int) p.size()) {
  193. cout << i - (int) p.size() << "\n";
  194. }
  195. }
  196. }
  197.  
  198.  
  199. /*
  200. Build Suffix Array (basic)
  201. */
  202. for (int t = 1; t <= T; ++t) {
  203. string s;
  204. cin >> s;
  205. s += '$';
  206. int n = s.size();
  207. int order[n]; // order
  208. int class_[n]; // equivalent class
  209. pair<char, int> pos[n]; // position
  210. for (int i = 0; i < n; ++i) {
  211. pos[i] = make_pair(s[i], i);
  212. }
  213. sort(pos, pos + n);
  214. for (int i = 0; i < n; ++i) {
  215. order[i] = pos[i].second;
  216. }
  217. class_[order[0]] = 0;
  218. for (int i = 1; i < n; ++i) {
  219. if (pos[i].first == pos[i - 1].first) {
  220. class_[order[i]] = class_[order[i - 1]];
  221. } else {
  222. class_[order[i]] = class_[order[i - 1]] + 1;
  223. }
  224. }
  225. int k = 0;
  226. while ((1 << k) < n) {
  227. pair<pair<int, int>, int> suf[n]; //positon array
  228. for (int i = 0; i < n; ++i) {
  229. suf[i] = make_pair(make_pair(class_[i], class_[(i + (1 << k)) % n]), i);
  230. }
  231. sort(suf, suf + n);
  232. for (int i = 0; i < n; ++i) {
  233. order[i] = suf[i].second;
  234. }
  235. class_[order[0]] = 0; // first class to be order[0] = 0
  236. for (int i = 1; i < n; ++i) {
  237. if (suf[i].first == suf[i - 1].first) {
  238. class_[order[i]] = class_[order[i - 1]];
  239. } else {
  240. class_[order[i]] = class_[order[i - 1]] + 1;
  241. }
  242. }
  243. k += 1;
  244. }
  245. for (int i = 0; i < n; ++i) {
  246. cout << order[i] << " ";
  247. }
  248.  
  249.  
  250. == == == == == == =
  251. [Z Algorithm]
  252. == == == == == == =
  253. vector<int> z_algo (string s) {
  254. int n = (int) s.size();
  255. int l = 0, r = 0;
  256. vector<int> z(n);
  257. for (int i = 1; i < n; ++i) {
  258. if (i > r) {
  259. l = r = i;
  260. while (r < n and s[r] == s[r - l]) {
  261. ++r;
  262. }
  263. z[i] = r - l;
  264. --r;
  265. } else {
  266. if (i + z[i - l] <= r) {
  267. z[i] = z[i - l];
  268. } else {
  269. l = i;
  270. while (r < n and s[r] == s[r - l]) {
  271. ++r;
  272. }
  273. z[i] = r - l;
  274. --r;
  275. }
  276. }
  277. }
  278. return z;
  279. }
  280. == == == == ==
  281. [DIJKSTRA]
  282. == == == == ==
  283. void dijkstra (int source, int n) {
  284. for (int i = 1; i <= n; ++i) dist[i] = LLONG_MAX;
  285. dist[source] = 0;
  286. priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>, greater<pair<int64_t, int64_t>>> pq;
  287. pq.push(make_pair(0, source));
  288.  
  289. while (!pq.empty()) {
  290. int64_t u = pq.top().second;
  291. int64_t current_dist = pq.top().first;
  292. pq.pop();
  293.  
  294. if (dist[u] < current_dist) {
  295. continue;
  296. }
  297.  
  298. for (pair<int64_t, int64_t> v : adj[u]) {
  299. if (current_dist + v.second < dist[v.first]) {
  300. dist[v.first] = current_dist + v.second;
  301. pq.push(make_pair(dist[v.first], v.first));
  302. }
  303. }
  304. }
  305. }
  306.  
  307. //Sparse Table
  308.  
  309. const int maxN = 1e5 + 10;
  310. int64_t st[maxN][20];
  311. int bin_log[maxN];
  312. int64_t input[maxN];
  313.  
  314. void sparseTable (int n) {
  315. for (int i = 2; i <= n; ++i) bin_log[i] = bin_log[i / 2] + 1;
  316. for (int i = 0; i < n; ++i) st[i][0] = input[i];
  317. for (int j = 1; j < 20; ++j) {
  318. for (int i = 0; i + (1 << j) - 1 < n; ++i) {
  319. st[i][j] = max(st[i][j - 1],st[i + (1 << (j - 1))][j - 1]);
  320. }
  321. }
  322. //for query min_max
  323. /*
  324. int l,r;
  325. cin >> l >> r;
  326. int j = bin_log[r - l + 1];
  327. cout << min(st[l][j],st[r - (1 << j) + 1][j]) << '\n';
  328. */
  329. }
  330.  
  331.  
  332. //String Hashing
  333. const int maxN = 2100;
  334. const int64_t p = 31;
  335. const int64_t m = 1e9 + 7;
  336. int64_t h[maxN],inv[maxN];
  337.  
  338. int64_t bin_pow (int64_t a,int64_t b) {
  339. a %= m;
  340. int64_t res = 1;
  341. while (b > 0) {
  342. if (b & 1) res = res * a % m;
  343. a = a * a % m;
  344. b >>= 1;
  345. }
  346. return res;
  347. }
  348.  
  349. void compute_hash (string & s) {
  350. int64_t p_pow = 1;
  351. inv[0] = 1;
  352. h[0] = s[0] - 'a' + 1;
  353. for (int i = 1; i < (int) s.size(); ++i) {
  354. p_pow = (p_pow * p) % m;
  355. inv[i] = bin_pow(p_pow,m - 2);
  356. h[i] = (h[i - 1] + (s[i] - 'a' + 1) * p_pow) % m;
  357. }
  358. }
  359.  
  360. int64_t sub_hash (int l,int r) {
  361. int64_t res = h[r];
  362. if (l > 0) res -= h[l - 1];
  363. res = (res * inv[l]) % m;
  364. if (res < 0) res = (res + m) % m;
  365. return res;
  366. }
  367.  
  368. int64_t single_hash (string & s) {
  369. int64_t hash_value = 0;
  370. int64_t p_pow = 1;
  371. for (char c : s) {
  372. hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
  373. p_pow = (p_pow * p) % m;
  374. }
  375. return hash_value;
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement