Advertisement
limimage

DO with segment assignment

May 6th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define endl "\n"
  3.  
  4. using namespace std;
  5. using ll = long long;
  6. using pii = pair<int, int>;
  7.  
  8. constexpr int N = 1e5+1;
  9. constexpr int MOD = 1e9+7;
  10. constexpr int INF = 1e9+9;
  11.  
  12. int n, q, ql, qr, qk, tr[26][4*N], temp[26], cp, ln;
  13. char ch;
  14.  
  15. void push(int id, int i)
  16. {
  17. if (tr[i][id]!=-1)
  18. {
  19. tr[i][(id<<1)+1]=tr[i][id];
  20. tr[i][(id<<1)+2]=tr[i][id];
  21. tr[i][id] = -1;
  22. }
  23. }
  24.  
  25. /*void build(int i, int id = 0, int l = 0, int r = n)
  26. {
  27. if (l==r)
  28. return;
  29. if (l+1==r)
  30. {
  31. tr[i][id]=arr[i][l];
  32. return;
  33. }
  34. int mid = (r+l)>>1;
  35. build(i, (id<<1)+1, l, mid);
  36. build(i, (id<<1)+2, mid, r);
  37. tr[i][id]=tr[i][(id<<1)+1]+tr[i][(id<<1)+2];
  38. }*/
  39.  
  40. void upd(int i, int val, int l, int r, int id = 0, int cl = 0, int cr = n)
  41. {
  42. if (l>cr||r<cl||l==r)
  43. return;
  44. if (l==cl&&r==cr)
  45. {
  46. tr[i][id]=val;
  47. return;
  48. }
  49. push(id, i);
  50. int mid = (cl+cr)>>1;
  51. upd(i, val, max(l,cl), min(r, mid), (id<<1)+1, cl, mid);
  52. upd(i, val, max(mid,l), min(r, cr), (id<<1)+2, mid, cr);
  53. }
  54.  
  55. int get(int i, int l, int r, int id = 0, int cl = 0, int cr = n)
  56. {
  57. if (l>cr||r<cl||l==r)
  58. return 0;
  59. if (tr[i][id]!=-1)
  60. return (r-l)*tr[i][id];
  61. int mid = (cl+cr)>>1;
  62. return get(i, max(l, cl), min(r, mid), (id<<1)+1, cl, mid)+
  63. get(i, max(l, mid), min(r, cr), (id<<1)+2, mid, cr);
  64. }
  65.  
  66. int get_sum(int i, int l, int r)
  67. {
  68. l--;
  69. return get(i, l, r);
  70. }
  71.  
  72. int close(int n)
  73. {
  74. int res = 1;
  75. while(res<n)
  76. {
  77. res<<=1;
  78. }
  79. return res;
  80. }
  81.  
  82. void Solve()
  83. {
  84. cin >> n >> q;
  85. ln=n;
  86. n=close(n);
  87. for (int i = 0; i < ln; i++)
  88. {
  89. cin>>ch;
  90. upd(ch-'a', 1, i, i+1);
  91. }
  92. while(q--)
  93. {
  94. cin >> ql >> qr >> qk;
  95. cp = ql-1;
  96. for (int i = 0; i < 26; i++)
  97. {
  98. temp[i] = get_sum(i, ql, qr);
  99. upd(i, 0, ql-1, qr);
  100. }
  101. if (qk)
  102. {
  103. for (int i = 0; i < 26; i++)
  104. {
  105. upd(i, 1, cp, cp+temp[i]);
  106. cp+=temp[i];
  107. }
  108. }
  109. else
  110. {
  111. for (int i = 25; i >= 0; i--)
  112. {
  113. upd(i, 1, cp, cp+temp[i]);
  114. cp+=temp[i];
  115. }
  116. }
  117. }
  118. for (int i = 1; i <= n; i++)
  119. {
  120. for (int j = 0; j < 26; j++)
  121. {
  122. if (get_sum(j, i, i)==1)
  123. {
  124. cout << static_cast<char>('a'+j);
  125. break;
  126. }
  127. }
  128. }
  129. }
  130.  
  131. int main()
  132. {
  133. ios_base::sync_with_stdio(false);
  134. cin.tie(nullptr);
  135. cout.tie(nullptr);
  136. Solve();
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement