Guest User

Untitled

a guest
Apr 6th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pb push_back
  4. #define ll long long
  5. #define ld long double
  6. #define f first
  7. #define s second
  8. #define mp make_pair
  9.  
  10. const int maxn = 1e6 + 1, maxm = 24, maxk = 61;
  11. ll inf = 1000000000, mod = inf + 7, mod2 = 998244353;
  12. const ll pa = 997, pa2 = 113, block = 400;
  13.  
  14. using namespace std;
  15.  
  16. int n, q;
  17. int t[maxn * 4], t2[maxn * 4], lp[maxn], was[maxn], l[maxn], r[maxn];
  18. set<int> setik[maxn];
  19. void upd(int pos, int val, int v = 1, int tl = 1, int tr = n){
  20. if (tl == tr){
  21. t[v] = val;
  22. return;
  23. }
  24. int tm = (tl + tr) / 2;
  25. if (pos <= tm)
  26. upd(pos, val, v + v, tl, tm);
  27. else
  28. upd(pos, val, v + v + 1, tm + 1, tr);
  29. t[v] = min(t[v + v], t[v + v + 1]);
  30. }
  31. void upd2(int pos, int val, int v = 1, int tl = 1, int tr = n){
  32. if (tl == tr){
  33. t2[v] = val;
  34. return;
  35. }
  36. int tm = (tl + tr) / 2;
  37. if (pos <= tm)
  38. upd2(pos, val, v + v, tl, tm);
  39. else
  40. upd2(pos, val, v + v + 1, tm + 1, tr);
  41. t2[v] = max(t2[v + v], t2[v + v + 1]);
  42. }
  43. int get(int l, int r, int v = 1, int tl = 1, int tr = n){
  44. if (l <= tl && tr <= r)
  45. return t[v];
  46. if (l > tr || r < tl)
  47. return inf;
  48. int tm = (tl + tr) / 2;
  49. return min(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
  50. }
  51. int get2(int l, int r, int v = 1, int tl = 1, int tr = n){
  52. if (l <= tl && tr <= r)
  53. return t2[v];
  54. if (l > tr || r < tl)
  55. return 0;
  56. int tm = (tl + tr) / 2;
  57. return max(get2(l, r, v + v, tl, tm), get2(l, r, v + v + 1, tm + 1, tr));
  58. }
  59. int main(){
  60. // ios_base::sync_with_stdio(false);
  61. // cin.tie(0), cout.tie(0);
  62. for (int i = 2; i <= maxn - 1; ++i){
  63. for (int j = i; j <= maxn - 1; j += i){
  64. if (lp[j] == 0){
  65. lp[j] = i;
  66. }
  67. }
  68. }
  69. cin >> n >> q;
  70. for (int i = 1; i <= n; ++i){
  71. l[i] = -1;
  72. r[i] = inf;
  73. upd(i, r[i]);
  74. upd2(i, l[i]);
  75. }
  76. for (int i = 1; i <= q; ++i){
  77. char c;
  78. cin >> c;
  79. if (c == 'S'){
  80. int x;
  81. cin >> x;
  82. if (was[x] == 0){
  83. vector<int> v;
  84. int x2 = x;
  85. while (x2 != 1){
  86. int y = lp[x2];
  87. v.pb(y);
  88. while (x2 % y == 0){
  89. x2 /= y;
  90. }
  91. }
  92. l[x] = -1;
  93. r[x] = inf;
  94. for (auto j : v){
  95. auto it = setik[j].upper_bound(x);
  96. if (it != setik[j].end()){
  97. r[x] = min(r[x], *it);
  98. }
  99. if (it != setik[j].begin()){
  100. it--;
  101. l[x] = max(l[x], *it);
  102. }
  103. setik[j].insert(x);
  104. }
  105. upd(x, r[x]);
  106. upd2(x, l[x]);
  107. was[x] = 1;
  108. }
  109. else{
  110. vector<int> v;
  111. int x2 = x;
  112. while (x2 != 1){
  113. int y = lp[x2];
  114. v.pb(y);
  115. while (x2 % y == 0){
  116. x2 /= y;
  117. }
  118. }
  119. for (auto p : v){
  120. setik[p].erase(x);
  121. }
  122. if (l[x] != -1){
  123. v.clear();
  124. x2 = l[x];
  125. while (x2 != 1){
  126. int y = lp[x2];
  127. v.pb(y);
  128. while (x2 % y == 0){
  129. x2 /= y;
  130. }
  131. }
  132. r[x] = inf;
  133. for (auto j : v){
  134. auto it = setik[j].upper_bound(x);
  135. if (it != setik[j].end()){
  136. r[x] = min(r[x], *it);
  137. }
  138. }
  139. upd(x, r[x]);
  140. }
  141. if (r[x] != inf){
  142. v.clear();
  143. x2 = l[x];
  144. while (x2 != 1){
  145. int y = lp[x2];
  146. v.pb(y);
  147. while (x2 % y == 0){
  148. x2 /= y;
  149. }
  150. }
  151. l[x] = -1;
  152. for (auto j : v){
  153. auto it = setik[j].upper_bound(x);
  154. if (it != setik[j].begin()){
  155. it--;
  156. l[x] = max(l[x], *it);
  157. }
  158. }
  159. upd2(x, l[x]);
  160. }
  161. }
  162. }
  163. else{
  164. int l, r;
  165. cin >> l >> r;
  166. int z = get(l, r), w = get2(l, r);
  167. if (z <= r || w >= l){
  168. cout << "DA\n";
  169. }
  170. else{
  171. cout << "NE\n";
  172. }
  173. }
  174. }
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment