Advertisement
Galebickosikasa

Untitled

Aug 27th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <set>
  7. #include <map>
  8. #include <bitset>
  9. #include <string>
  10. #include <deque>
  11. #include <queue>
  12. #include <unordered_set>
  13. #include <unordered_map>
  14.  
  15. #define pb push_back
  16. #define ll long long
  17. #define ld long double
  18. #define all(a) a.begin(), a.end()
  19.  
  20. using namespace std;
  21.  
  22. //tg: @galebickosikasa
  23.  
  24. vector<string> STRINGS_BEACH;
  25.  
  26. struct Tree{
  27. Tree* left;
  28. Tree* right;
  29. int priority;
  30. uint32_t size;
  31. // vector<int> assign;
  32. // vector<int> goo;
  33. bitset<1000001> assign, goo;
  34.  
  35. Tree(){
  36. left = right = nullptr;
  37. size = 1;
  38. priority = rand();
  39. }
  40.  
  41. };
  42.  
  43. uint32_t get_size(Tree* t){
  44. if (t == nullptr){
  45. return 0;
  46. }
  47. return t->size;
  48. }
  49.  
  50. void push(Tree* t){
  51. if (!t->assign.count()){
  52. return;
  53. }
  54. t->goo |= t->assign;
  55. if (t->left != nullptr){
  56. t->left->assign |= t->assign;
  57. }
  58. if (t->right != nullptr){
  59. t->right->assign |= t->assign;
  60. }
  61. t->assign.reset();
  62. }
  63.  
  64. void render(Tree* t){
  65. if (t == nullptr){
  66. return;
  67. }
  68. t->size = 1 + get_size(t->left) + get_size(t->right);
  69. }
  70.  
  71. pair<Tree*, Tree*> split(Tree* t, int k){
  72. pair<Tree*, Tree*> ptt = {nullptr, nullptr};
  73. if (t == nullptr){
  74. return ptt;
  75. }
  76. push(t);
  77. if (get_size(t->left) >= k){
  78. ptt = split(t->left, k);
  79. t->left = ptt.second;
  80. ptt.second = t;
  81. } else{
  82. ptt = split(t->right, k - get_size(t->left) - 1);
  83. t->right = ptt.first;
  84. ptt.first = t;
  85. }
  86. render(t);
  87. return ptt;
  88. }
  89.  
  90. Tree* merge(Tree* l, Tree* r){
  91. if (l == nullptr){
  92. return r;
  93. } else if (r == nullptr){
  94. return l;
  95. } else if (l->priority >= r->priority){
  96. push(l);
  97. l->right = merge(l->right, r);
  98. render(l);
  99. return l;
  100. } else{
  101. push(r);
  102. r->left = merge(l, r->left);
  103. render(r);
  104. return r;
  105. }
  106. }
  107.  
  108. Tree* add(Tree* t, Tree* a, int i){
  109. auto ptt = split(t, i - 1);
  110. ptt.first = merge(ptt.first, a);
  111. return merge(ptt.first, ptt.second);
  112. }
  113.  
  114. Tree* add(Tree* t, int i){
  115. Tree* a = new Tree();
  116. return add(t, a, i);
  117. }
  118.  
  119. void collect_num(Tree* t, int i, bitset<1000001>& a){
  120. if (get_size(t->left) + 1 == i){
  121. a |= t->goo;
  122. a |= t->assign;
  123. return;
  124. }
  125. if (get_size(t->left) >= i){
  126. collect_num(t->left, i, a);
  127. } else{
  128. collect_num(t->right, i - get_size(t->left) - 1, a);
  129. }
  130. a |= t->assign;
  131. }
  132.  
  133. int main(){
  134. ios_base::sync_with_stdio(false);
  135. cout.tie(nullptr);
  136. cin.tie(nullptr);
  137. Tree* root = nullptr;
  138. int n, m;
  139. cin >> n >> m;
  140. bitset<1000001> tmp;
  141. for (int i = 0; i < n; ++i){
  142. root = add(root, i + 1);
  143. }
  144. for (int i = 0; i < m; ++i){
  145. int w;
  146. cin >> w;
  147. if (w == 1){
  148. int l, r;
  149. string s;
  150. cin >> l >> r >> s;
  151. STRINGS_BEACH.pb(s);
  152. auto ptt1 = split(root, l - 1);
  153. auto ptt2 = split(ptt1.second, r - l + 1);
  154. ptt2.first->assign[STRINGS_BEACH.size() - 1] = true;
  155. ptt1.second = merge(ptt2.first, ptt2.second);
  156. root = merge(ptt1.first, ptt1.second);
  157. } else{
  158. int a, x, y;
  159. cin >> a >> x >> y;
  160. collect_num(root, a, tmp);
  161. int j = tmp._Find_first();
  162. while (STRINGS_BEACH[j].size() < x){
  163. x -= STRINGS_BEACH[j].size(), y -= STRINGS_BEACH[j].size();
  164. tmp[j] = false;
  165. j = tmp._Find_first();
  166. }
  167. if (y <= STRINGS_BEACH[j].size()){
  168. for (int k = x - 1; k < y; ++k){
  169. cout << STRINGS_BEACH[j][k];
  170. }
  171. } else{
  172. for (int k = x - 1; k < STRINGS_BEACH[j].size(); ++k){
  173. cout << STRINGS_BEACH[j][k];
  174. }
  175. y -= STRINGS_BEACH[j].size();
  176. tmp[j] = false;
  177. j = tmp._Find_first();
  178. while (STRINGS_BEACH[j].size() < y){
  179. cout << STRINGS_BEACH[j];
  180. y -= STRINGS_BEACH[j].size();
  181. tmp[j] = false;
  182. j = tmp._Find_first();
  183. }
  184. for (int k = 0; k < y; ++k){
  185. cout << STRINGS_BEACH[j][k];
  186. }
  187. }
  188. cout << '\n';
  189. tmp.reset();
  190. }
  191. }
  192. }
  193. /*
  194. 4 4
  195. 1 2 2 two
  196. 1 2 3 aa
  197. 2 3 1 1
  198. 2 2 1 4
  199.  
  200. 3 6
  201. 1 1 2 ab
  202. 1 2 3 cd
  203. 2 2 2 4
  204. 2 3 1 2
  205. 1 1 3 xyzu
  206. 2 1 2 5
  207.  
  208. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement