Advertisement
999ms

Untitled

Nov 26th, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define all(x) begin(x),end(x)
  4.  
  5. using namespace std;
  6. using ll = long long;
  7.  
  8. mt19937 rnd(514234123);
  9.  
  10. const ll mod1 = 1e9 + 7;
  11. const ll mod2 = 1e9 + 9;
  12. const ll a1 = 3e7 + 4123;
  13. const ll a2 = 3e7 + 141;
  14.  
  15. template<ll A, ll MOD>
  16. struct THash {
  17. ll val;
  18.  
  19. THash(ll k) : val(k % MOD) {}
  20. };
  21.  
  22. template<ll MOD>
  23. ll FastPower(ll a, int p) {
  24. ll res = 1;
  25. while (p) {
  26. if (p & 1) {
  27. res *= a;
  28. res %= MOD;
  29. }
  30. a = (a * a) % MOD;
  31. p >>= 1;
  32. }
  33. return res;
  34. }
  35.  
  36. template<ll A, ll MOD>
  37. THash<A, MOD> merge(THash<A, MOD> l, THash<A, MOD> r, int lsz) {
  38. return THash<A, MOD>(l.val * FastPower<MOD>(A, lsz) + r.val);
  39. }
  40.  
  41.  
  42. struct Treap {
  43. Treap *l = nullptr;
  44. Treap *r = nullptr;
  45. int sz = 0;
  46. ll key;
  47. ll pr;
  48. THash<a1, mod1> hsh;
  49. THash<a2, mod2> hsh2;
  50.  
  51. explicit Treap(ll k) : key(k), sz(1), pr(rnd()), hsh(key), hsh2(key) {}
  52. };
  53.  
  54. int gsz(Treap *v) {
  55. return v ? v->sz : 0;
  56. }
  57.  
  58. void rsz(Treap *v) {
  59. v->sz = gsz(v->l) + gsz(v->r) + 1;
  60. }
  61.  
  62. void upd(Treap *v) {
  63. rsz(v);
  64. if (v->l) {
  65. v->hsh = v->l->hsh;
  66. v->hsh2 = v->l->hsh2;
  67. } else {
  68. v->hsh = {0};
  69. v->hsh2 = {0};
  70. }
  71. if (v->r) {
  72. v->hsh = merge(v->hsh, v->r->hsh, gsz(v->l));
  73. v->hsh2 = merge(v->hsh2, v->r->hsh2, gsz(v->l));
  74. }
  75. }
  76.  
  77. Treap *merge(Treap *l, Treap *r) {
  78. if (!l || !r) return l ? l : r;
  79. if (l->pr > r->pr) {
  80. l->r = merge(l->r, r);
  81. upd(l);
  82. return l;
  83. } else {
  84. r->l = merge(l, r->l);
  85. upd(r);
  86. return r;
  87. }
  88. }
  89.  
  90. bool eq(Treap *l, Treap *r) {
  91. if (!l || !r) return false;
  92. return l->hsh.val == r->hsh.val && l->hsh2.val == r->hsh2.val;
  93. }
  94.  
  95. pair<Treap *, Treap *> split(Treap *v, int sz) {
  96. if (!v || sz >= gsz(v)) return {v, nullptr};
  97. if (gsz(v->l) + 1 <= sz) {
  98. auto[l, r] = split(v->r, sz - 1 - gsz(v->l));
  99. v->r = l;
  100. upd(v);
  101. return {v, r};
  102. } else {
  103. auto[l, r] = split(v->l, sz);
  104. v->l = r;
  105. upd(v);
  106. return {l, v};
  107. }
  108. }
  109.  
  110. struct SegmentTree {
  111. vector<int> t;
  112.  
  113. explicit SegmentTree(int n) : t(n * 4, -1) {}
  114.  
  115. void Update(int v, int tl, int tr, int i, int val) {
  116. if (tl + 1 == tr) {
  117. t[v] = val;
  118. } else {
  119. int tm = (tl + tr) >> 1;
  120. if (i < tm) {
  121. Update(v * 2 + 1, tl, tm, i, val);
  122. } else {
  123. Update(v * 2 + 2, tm, tr, i, val);
  124. }
  125. t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
  126. }
  127. }
  128.  
  129. int Get(int v, int tl, int tr, int l, int r) {
  130. if (tl >= r || tr <= l) return -1;
  131. if (tl >= l && tr <= r) {
  132. return t[v];
  133. } else {
  134. int tm = (tl + tr) >> 1;
  135. return max(Get(v * 2 + 1, tl, tm, l, r), Get(v * 2 + 2, tm, tr, l, r));
  136. }
  137. }
  138.  
  139. };
  140.  
  141. vector<pair<int, int>> prep(vector<int> &a) {
  142. set<int> st(all(a));
  143. int itr = 0;
  144. map<int, int> mp;
  145. for (auto &x: st) mp[x] = itr++;
  146. for (auto &x: a) x = mp[x];
  147. int sz = (int) st.size();
  148. SegmentTree tree(sz);
  149. vector<pair<int, int>> res(a.size(), {-1, a.size()});
  150. int n = (int) a.size();
  151. for (int i = 0; i < n; i++) {
  152. int f = res[i].first = tree.Get(0, 0, sz, a[i], sz);
  153. if (f != -1) {
  154. res[f].second = min(res[f].second, i);
  155. }
  156. tree.Update(0, 0, sz, a[i], i);
  157. }
  158. for (int i = 0; i < n; i++) {
  159. auto&[l, r] = res[i];
  160. l -= i;
  161. r -= i;
  162. }
  163. return res;
  164. }
  165.  
  166. ll getKey(int l, int r, int m) {
  167. l += m;
  168. r += m;
  169. return ll(1e9) * l + r;
  170. }
  171.  
  172. ll getKey(pair<int, int> p, int m) {
  173. return getKey(p.first, p.second, m);
  174. }
  175.  
  176.  
  177. void solve() {
  178. int m, n;
  179. cin >> m >> n;
  180. vector<int> a(m), b(n);
  181. for (auto &x: a) cin >> x;
  182. for (auto &x: b) cin >> x;
  183.  
  184. auto arr = prep(a);
  185. auto brr = prep(b);
  186.  
  187.  
  188. vector<vector<int>> rem(brr.size()), add(brr.size());
  189.  
  190. for (int i = 0; i < n; i++) {
  191. auto[l, r] = brr[i];
  192. l += i;
  193. r += i;
  194. if (l != -1) {
  195. rem[l].push_back(i);
  196. }
  197. if (r != n) {
  198. add[r].push_back(i);
  199. }
  200. }
  201.  
  202. Treap *at = nullptr;
  203. for (int i = 0; i < m; i++) {
  204. cout << "a = " << arr[i].first << ' ' << arr[i].second << endl;
  205. at = merge(at, new Treap(getKey(arr[i].first, arr[i].second, m)));
  206. }
  207. Treap *br = nullptr;
  208. int cl = 0;
  209. int cr = m - 1;
  210. for (int i = 0; i < m - 1; i++) {
  211. auto[l, r] = brr[i];
  212. r = min(r, cr + 1);
  213. cout << "b= " << l << ' ' << r << endl;
  214. br = merge(br, new Treap(getKey(l, r, m)));
  215. }
  216. vector<int> ans;
  217. for (int i = m - 1; i < n; i++) {
  218. auto[l, r] = brr[i];
  219. r = min(r, 1);
  220. l = max(l, -m);
  221. br = merge(br, new Treap(getKey(l, r, m)));
  222.  
  223. if (i > m - 1) {
  224. auto[tl, tr] = split(br, 1);
  225. br = tr;
  226. }
  227. /*
  228.  
  229. 5 10
  230. 500 560 600 680 580
  231. 40 60 70 90 65 30 35 50 55 40
  232.  
  233.  
  234. */
  235. for (auto ind: add[i]) {
  236. if (cl <= ind && ind <= cr) {
  237. auto[xl, xr] = brr[ind];
  238. auto pos = ind - cl;
  239. auto[kekl, kek] = split(br, pos);
  240. auto[ffl, kekr] = split(kek, 1);
  241. int mxl = -pos - 1;
  242. int mxr = cr - ind + 1;
  243. auto *nxt = new Treap(getKey(max(mxl, xl), min(xr, mxr), m));
  244. br = merge(kekl, nxt);
  245. br = merge(br, kekr);
  246. }
  247. }
  248.  
  249. cout << at->hsh.val << ' ' << at->hsh2.val << endl;
  250. cout << br->hsh.val << ' ' << br->hsh2.val << endl;
  251. if (eq(at, br)) {
  252. ans.push_back(i - m + 2);
  253. }
  254. for (auto ind: rem[i]) {
  255. if (cl <= ind && ind <= cr) {
  256. auto[xl, xr] = brr[ind];
  257. auto pos = ind - cl;
  258. auto[kekl, kek] = split(br, pos);
  259. auto[ffl, kekr] = split(kek, 1);
  260. int mxl = -pos - 1;
  261. int mxr = cr - ind + 1;
  262. auto *nxt = new Treap(getKey(max(mxl, xl), min(xr, mxr), m));
  263. br = merge(kekl, nxt);
  264. br = merge(br, kekr);
  265. }
  266. }
  267.  
  268. cl++;
  269. cr++;
  270. }
  271. if (ans.empty()) ans = {0};
  272. for (auto val: ans) cout << val << ' ';
  273.  
  274. cout << '\n';
  275. }
  276.  
  277. int main() {
  278. ios_base::sync_with_stdio(false);
  279. cin.tie(nullptr);
  280. cout.tie(nullptr);
  281. solve();
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement